summaryrefslogtreecommitdiff
path: root/legacy
diff options
context:
space:
mode:
authorCarsten Haitzler <raster@rasterman.com>2010-12-11 04:05:52 +0000
committerCarsten Haitzler <raster@rasterman.com>2010-12-11 04:05:52 +0000
commitc6471b1e14aafd66d8475ae534fda6dd976bb910 (patch)
tree771059b6c4df6de0f968013c9dac9bdfe1702107 /legacy
parent37a7c5bce9147f400e53d4f3161cd129aad7d689 (diff)
formatting fixups.
SVN revision: 55485
Diffstat (limited to 'legacy')
-rw-r--r--legacy/eina/src/lib/eina_strbuf_common.c105
1 files changed, 27 insertions, 78 deletions
diff --git a/legacy/eina/src/lib/eina_strbuf_common.c b/legacy/eina/src/lib/eina_strbuf_common.c
index 8f632bb0dd..91a37b16b5 100644
--- a/legacy/eina/src/lib/eina_strbuf_common.c
+++ b/legacy/eina/src/lib/eina_strbuf_common.c
@@ -86,8 +86,8 @@ _eina_strbuf_common_init(size_t csize, Eina_Strbuf *buf)
86 buf->len = 0; 86 buf->len = 0;
87 buf->size = EINA_STRBUF_INIT_SIZE; 87 buf->size = EINA_STRBUF_INIT_SIZE;
88 buf->step = EINA_STRBUF_INIT_STEP; 88 buf->step = EINA_STRBUF_INIT_STEP;
89 89
90 eina_error_set(0); 90 eina_error_set(0);
91 buf->buf = calloc(csize, buf->size); 91 buf->buf = calloc(csize, buf->size);
92 if (EINA_UNLIKELY(!buf->buf)) 92 if (EINA_UNLIKELY(!buf->buf))
93 { 93 {
@@ -115,24 +115,18 @@ _eina_strbuf_common_resize(size_t csize, Eina_Strbuf *buf, size_t size)
115 115
116 size += 1; // Add extra space for '\0' 116 size += 1; // Add extra space for '\0'
117 117
118 if (size == buf->size) 118 /* nothing to do */
119 /* nothing to do */ 119 if (size == buf->size) return EINA_TRUE;
120 return EINA_TRUE; 120 else if (size > buf->size) delta = size - buf->size;
121 else if (size > buf->size) 121 else delta = buf->size - size;
122 delta = size - buf->size;
123 else
124 delta = buf->size - size;
125 122
126 /* check if should keep the same step (just used while growing) */ 123 /* check if should keep the same step (just used while growing) */
127 if ((delta <= buf->step) && (size > buf->size)) 124 if ((delta <= buf->step) && (size > buf->size)) new_step = buf->step;
128 new_step = buf->step;
129 else 125 else
130 { 126 {
131 new_step = (((delta / EINA_STRBUF_INIT_STEP) + 1) 127 new_step = (((delta / EINA_STRBUF_INIT_STEP) + 1)
132 * EINA_STRBUF_INIT_STEP); 128 * EINA_STRBUF_INIT_STEP);
133 129 if (new_step > EINA_STRBUF_MAX_STEP) new_step = EINA_STRBUF_MAX_STEP;
134 if (new_step > EINA_STRBUF_MAX_STEP)
135 new_step = EINA_STRBUF_MAX_STEP;
136 } 130 }
137 131
138 new_size = (((size / new_step) + 1) * new_step); 132 new_size = (((size / new_step) + 1) * new_step);
@@ -148,7 +142,7 @@ _eina_strbuf_common_resize(size_t csize, Eina_Strbuf *buf, size_t size)
148 buf->buf = buffer; 142 buf->buf = buffer;
149 buf->size = new_size; 143 buf->size = new_size;
150 buf->step = new_step; 144 buf->step = new_step;
151 eina_error_set(0); 145 eina_error_set(0);
152 return EINA_TRUE; 146 return EINA_TRUE;
153} 147}
154 148
@@ -165,9 +159,7 @@ _eina_strbuf_common_resize(size_t csize, Eina_Strbuf *buf, size_t size)
165Eina_Bool 159Eina_Bool
166_eina_strbuf_common_grow(size_t csize, Eina_Strbuf *buf, size_t size) 160_eina_strbuf_common_grow(size_t csize, Eina_Strbuf *buf, size_t size)
167{ 161{
168 if ((size + 1) < buf->size) 162 if ((size + 1) < buf->size) return EINA_TRUE;
169 return EINA_TRUE;
170
171 return _eina_strbuf_common_resize(csize, buf, size); 163 return _eina_strbuf_common_resize(csize, buf, size);
172} 164}
173 165
@@ -229,20 +221,18 @@ eina_strbuf_common_new(size_t csize)
229{ 221{
230 Eina_Strbuf *buf; 222 Eina_Strbuf *buf;
231 223
232 eina_error_set(0); 224 eina_error_set(0);
233 buf = malloc(sizeof(Eina_Strbuf)); 225 buf = malloc(sizeof(Eina_Strbuf));
234 if (EINA_UNLIKELY(!buf)) 226 if (EINA_UNLIKELY(!buf))
235 { 227 {
236 eina_error_set(EINA_ERROR_OUT_OF_MEMORY); 228 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
237 return NULL; 229 return NULL;
238 } 230 }
239
240 if (EINA_UNLIKELY(!_eina_strbuf_common_init(csize, buf))) 231 if (EINA_UNLIKELY(!_eina_strbuf_common_init(csize, buf)))
241 { 232 {
242 eina_strbuf_common_free(buf); 233 eina_strbuf_common_free(buf);
243 return NULL; 234 return NULL;
244 } 235 }
245
246 return buf; 236 return buf;
247} 237}
248 238
@@ -274,7 +264,6 @@ eina_strbuf_common_reset(size_t csize, Eina_Strbuf *buf)
274{ 264{
275 buf->len = 0; 265 buf->len = 0;
276 buf->step = EINA_STRBUF_INIT_STEP; 266 buf->step = EINA_STRBUF_INIT_STEP;
277
278 memset(buf->buf, 0, csize); 267 memset(buf->buf, 0, csize);
279} 268}
280 269
@@ -300,12 +289,10 @@ eina_strbuf_common_append(size_t csize,
300 const void *str, 289 const void *str,
301 size_t len) 290 size_t len)
302{ 291{
303
304 EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE); 292 EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE);
305 293
306 if (EINA_UNLIKELY(!_eina_strbuf_common_grow(csize, buf, buf->len + len))) 294 if (EINA_UNLIKELY(!_eina_strbuf_common_grow(csize, buf, buf->len + len)))
307 return EINA_FALSE; 295 return EINA_FALSE;
308
309 memcpy(((unsigned char *)(buf->buf)) + (buf->len * csize), str, 296 memcpy(((unsigned char *)(buf->buf)) + (buf->len * csize), str,
310 (len + 1) * csize); 297 (len + 1) * csize);
311 buf->len += len; 298 buf->len += len;
@@ -342,12 +329,9 @@ eina_strbuf_common_append_n(size_t csize,
342{ 329{
343 EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE); 330 EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE);
344 331
345 if (len > maxlen) 332 if (len > maxlen) len = maxlen;
346 len = maxlen;
347
348 if (EINA_UNLIKELY(!_eina_strbuf_common_grow(csize, buf, buf->len + len))) 333 if (EINA_UNLIKELY(!_eina_strbuf_common_grow(csize, buf, buf->len + len)))
349 return EINA_FALSE; 334 return EINA_FALSE;
350
351 memcpy(((unsigned char *)(buf->buf)) + (buf->len * csize), str, 335 memcpy(((unsigned char *)(buf->buf)) + (buf->len * csize), str,
352 len * csize); 336 len * csize);
353 buf->len += len; 337 buf->len += len;
@@ -384,7 +368,6 @@ eina_strbuf_common_append_length(size_t csize,
384 368
385 if (EINA_UNLIKELY(!_eina_strbuf_common_grow(csize, buf, buf->len + length))) 369 if (EINA_UNLIKELY(!_eina_strbuf_common_grow(csize, buf, buf->len + length)))
386 return EINA_FALSE; 370 return EINA_FALSE;
387
388 memcpy(((unsigned char *)(buf->buf)) + (buf->len * csize), str, 371 memcpy(((unsigned char *)(buf->buf)) + (buf->len * csize), str,
389 length * csize); 372 length * csize);
390 buf->len += length; 373 buf->len += length;
@@ -415,9 +398,7 @@ eina_strbuf_common_insert(size_t csize,
415{ 398{
416 EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE); 399 EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE);
417 400
418 if (pos >= buf->len) 401 if (pos >= buf->len) return eina_strbuf_common_append(csize, buf, str, len);
419 return eina_strbuf_common_append(csize, buf, str, len);
420
421 return _eina_strbuf_common_insert_length(csize, buf, str, len, pos); 402 return _eina_strbuf_common_insert_length(csize, buf, str, len, pos);
422} 403}
423 404
@@ -451,10 +432,7 @@ eina_strbuf_common_insert_n(size_t csize,
451 432
452 if (pos >= buf->len) 433 if (pos >= buf->len)
453 return eina_strbuf_common_append_n(csize, buf, str, len, maxlen); 434 return eina_strbuf_common_append_n(csize, buf, str, len, maxlen);
454 435 if (len > maxlen) len = maxlen;
455 if (len > maxlen)
456 len = maxlen;
457
458 return _eina_strbuf_common_insert_length(csize, buf, str, len, pos); 436 return _eina_strbuf_common_insert_length(csize, buf, str, len, pos);
459} 437}
460 438
@@ -489,7 +467,6 @@ eina_strbuf_common_insert_length(size_t csize,
489 467
490 if (pos >= buf->len) 468 if (pos >= buf->len)
491 return eina_strbuf_common_append_length(csize, buf, str, length); 469 return eina_strbuf_common_append_length(csize, buf, str, length);
492
493 return _eina_strbuf_common_insert_length(csize, buf, str, length, pos); 470 return _eina_strbuf_common_insert_length(csize, buf, str, length, pos);
494} 471}
495 472
@@ -507,7 +484,6 @@ eina_strbuf_common_insert_length(size_t csize,
507Eina_Bool 484Eina_Bool
508eina_strbuf_common_append_char(size_t csize, Eina_Strbuf *buf, const void *c) 485eina_strbuf_common_append_char(size_t csize, Eina_Strbuf *buf, const void *c)
509{ 486{
510
511 if (EINA_UNLIKELY(!_eina_strbuf_common_grow(csize, buf, buf->len + 1))) 487 if (EINA_UNLIKELY(!_eina_strbuf_common_grow(csize, buf, buf->len + 1)))
512 return EINA_FALSE; 488 return EINA_FALSE;
513 489
@@ -535,10 +511,8 @@ eina_strbuf_common_insert_char(size_t csize,
535 const void *c, 511 const void *c,
536 size_t pos) 512 size_t pos)
537{ 513{
538
539 if (pos >= buf->len) 514 if (pos >= buf->len)
540 return eina_strbuf_common_append_char(csize, buf, c); 515 return eina_strbuf_common_append_char(csize, buf, c);
541
542 return _eina_strbuf_common_insert_length(csize, buf, c, 1, pos); 516 return _eina_strbuf_common_insert_length(csize, buf, c, 1, pos);
543} 517}
544 518
@@ -564,11 +538,8 @@ eina_strbuf_common_remove(size_t csize,
564{ 538{
565 size_t remove_len, tail_len; 539 size_t remove_len, tail_len;
566 540
567 if (end >= buf->len) 541 if (end >= buf->len) end = buf->len;
568 end = buf->len; 542 if (end <= start) return EINA_TRUE;
569
570 if (end <= start)
571 return EINA_TRUE;
572 543
573 remove_len = end - start; 544 remove_len = end - start;
574 if (remove_len == buf->len) 545 if (remove_len == buf->len)
@@ -723,19 +694,14 @@ eina_strbuf_replace(Eina_Strbuf *buf,
723 EINA_SAFETY_ON_NULL_RETURN_VAL( str, EINA_FALSE); 694 EINA_SAFETY_ON_NULL_RETURN_VAL( str, EINA_FALSE);
724 EINA_SAFETY_ON_NULL_RETURN_VAL(with, EINA_FALSE); 695 EINA_SAFETY_ON_NULL_RETURN_VAL(with, EINA_FALSE);
725 EINA_MAGIC_CHECK_STRBUF(buf, 0); 696 EINA_MAGIC_CHECK_STRBUF(buf, 0);
726 697 if (n == 0) return EINA_FALSE;
727 if (n == 0)
728 return EINA_FALSE;
729 698
730 spos = buf->buf; 699 spos = buf->buf;
731 while (n--) 700 while (n--)
732 { 701 {
733 spos = strstr(spos, str); 702 spos = strstr(spos, str);
734 if (!spos || *spos == '\0') 703 if (!spos || *spos == '\0') return EINA_FALSE;
735 return EINA_FALSE; 704 if (n) spos++;
736
737 if (n)
738 spos++;
739 } 705 }
740 706
741 pos = spos - (const char *)buf->buf; 707 pos = spos - (const char *)buf->buf;
@@ -746,21 +712,15 @@ eina_strbuf_replace(Eina_Strbuf *buf,
746 /* resize the buffer if necessary */ 712 /* resize the buffer if necessary */
747 if (EINA_UNLIKELY(!_eina_strbuf_common_grow(_STRBUF_CSIZE, buf, 713 if (EINA_UNLIKELY(!_eina_strbuf_common_grow(_STRBUF_CSIZE, buf,
748 buf->len - len1 + len2))) 714 buf->len - len1 + len2)))
749 { 715 return EINA_FALSE; /* move the existing text */
750 return EINA_FALSE; /* move the existing text */
751
752 }
753
754 memmove(((unsigned char *)(buf->buf)) + pos + len2, 716 memmove(((unsigned char *)(buf->buf)) + pos + len2,
755 ((unsigned char *)(buf->buf)) + pos + len1, 717 ((unsigned char *)(buf->buf)) + pos + len1,
756 buf->len - pos - len1); 718 buf->len - pos - len1);
757 } 719 }
758
759 /* and now insert the given string */ 720 /* and now insert the given string */
760 memcpy(((unsigned char *)(buf->buf)) + pos, with, len2); 721 memcpy(((unsigned char *)(buf->buf)) + pos, with, len2);
761 buf->len += len2 - len1; 722 buf->len += len2 - len1;
762 memset(((unsigned char *)(buf->buf)) + buf->len, 0, 1); 723 memset(((unsigned char *)(buf->buf)) + buf->len, 0, 1);
763
764 return EINA_TRUE; 724 return EINA_TRUE;
765} 725}
766 726
@@ -791,12 +751,9 @@ eina_strbuf_replace_all(Eina_Strbuf *buf, const char *str, const char *with)
791 EINA_MAGIC_CHECK_STRBUF(buf, 0); 751 EINA_MAGIC_CHECK_STRBUF(buf, 0);
792 752
793 spos = strstr(buf->buf, str); 753 spos = strstr(buf->buf, str);
794 if (!spos || *spos == '\0') 754 if (!spos || *spos == '\0') return 0;
795 return 0;
796
797 len1 = strlen(str); 755 len1 = strlen(str);
798 len2 = strlen(with); 756 len2 = strlen(with);
799
800 /* if the size of the two string is equal, it is fairly easy to replace them 757 /* if the size of the two string is equal, it is fairly easy to replace them
801 * we don't need to resize the buffer or doing other calculations */ 758 * we don't need to resize the buffer or doing other calculations */
802 if (len1 == len2) 759 if (len1 == len2)
@@ -809,7 +766,6 @@ eina_strbuf_replace_all(Eina_Strbuf *buf, const char *str, const char *with)
809 } 766 }
810 return n; 767 return n;
811 } 768 }
812
813 pos = pos_tmp = spos - (const char *)buf->buf; 769 pos = pos_tmp = spos - (const char *)buf->buf;
814 tmp_buf = buf->buf; 770 tmp_buf = buf->buf;
815 buf->buf = malloc(buf->size); 771 buf->buf = malloc(buf->size);
@@ -818,10 +774,8 @@ eina_strbuf_replace_all(Eina_Strbuf *buf, const char *str, const char *with)
818 buf->buf = tmp_buf; 774 buf->buf = tmp_buf;
819 return 0; 775 return 0;
820 } 776 }
821
822 start = start_tmp = 0; 777 start = start_tmp = 0;
823 len = buf->len; 778 len = buf->len;
824
825 while (spos) 779 while (spos)
826 { 780 {
827 n++; 781 n++;
@@ -834,14 +788,11 @@ eina_strbuf_replace_all(Eina_Strbuf *buf, const char *str, const char *with)
834 len = (len + len1) - len2; 788 len = (len + len1) - len2;
835 break; 789 break;
836 } 790 }
837
838 /* copy the untouched text */ 791 /* copy the untouched text */
839 memcpy(((unsigned char *)(buf->buf)) + start, 792 memcpy(((unsigned char *)(buf->buf)) + start, tmp_buf + start_tmp,
840 tmp_buf + start_tmp, pos - start); 793 pos - start);
841 /* copy the new string */ 794 /* copy the new string */
842 memcpy(((unsigned char *)(buf->buf)) + pos, 795 memcpy(((unsigned char *)(buf->buf)) + pos, with, len2);
843 with, len2);
844
845 /* calculate the next positions */ 796 /* calculate the next positions */
846 start_tmp = pos_tmp + len1; 797 start_tmp = pos_tmp + len1;
847 start = pos + len2; 798 start = pos + len2;
@@ -853,13 +804,11 @@ eina_strbuf_replace_all(Eina_Strbuf *buf, const char *str, const char *with)
853 pos = start + pos_tmp - start_tmp; 804 pos = start + pos_tmp - start_tmp;
854 } 805 }
855 /* and now copy the rest of the text */ 806 /* and now copy the rest of the text */
856 memcpy(((unsigned char *)(buf->buf)) + start, 807 memcpy(((unsigned char *)(buf->buf)) + start, tmp_buf + start_tmp,
857 tmp_buf + start_tmp, len - start); 808 len - start);
858 buf->len = len; 809 buf->len = len;
859 memset(((unsigned char *)(buf->buf)) + buf->len, 0, 1); 810 memset(((unsigned char *)(buf->buf)) + buf->len, 0, 1);
860
861 free(tmp_buf); 811 free(tmp_buf);
862
863 return n; 812 return n;
864} 813}
865 814