Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenstringbuf.c
Go to the documentation of this file.
00001 /* 00002 stringbuf: mimicking a bit of C++ to more safely handle strings 00003 00004 copyright 2006-8 by the mpg123 project - free software under the terms of the LGPL 2.1 00005 see COPYING and AUTHORS files in distribution or http://mpg123.org 00006 initially written by Thomas Orgis 00007 */ 00008 00009 #include "mpg123lib_intern.h" 00010 #include "config.h" 00011 #include "mpg123.h" 00012 #include "compat.h" 00013 #include <string.h> 00014 #include "debug.h" 00015 00016 void attribute_align_arg mpg123_init_string(mpg123_string* sb) 00017 { 00018 sb->p = NULL; 00019 sb->size = 0; 00020 sb->fill = 0; 00021 } 00022 00023 void attribute_align_arg mpg123_free_string(mpg123_string* sb) 00024 { 00025 if(sb->p != NULL) free(sb->p); 00026 mpg123_init_string(sb); 00027 } 00028 00029 int attribute_align_arg mpg123_grow_string(mpg123_string* sb, size_t new) 00030 { 00031 if(sb->size < new) return mpg123_resize_string(sb, new); 00032 else return 1; 00033 } 00034 00035 int attribute_align_arg mpg123_resize_string(mpg123_string* sb, size_t new) 00036 { 00037 debug3("resizing string pointer %p from %lu to %lu", (void*) sb->p, (unsigned long)sb->size, (unsigned long)new); 00038 if(new == 0) 00039 { 00040 if(sb->size && sb->p != NULL) free(sb->p); 00041 mpg123_init_string(sb); 00042 return 1; 00043 } 00044 if(sb->size != new) 00045 { 00046 char* t; 00047 debug("really!"); 00048 t = (char*) safe_realloc(sb->p, new*sizeof(char)); 00049 debug1("safe_realloc returned %p", (void*) t); 00050 if(t != NULL) 00051 { 00052 sb->p = t; 00053 sb->size = new; 00054 return 1; 00055 } 00056 else return 0; 00057 } 00058 else return 1; /* success */ 00059 } 00060 00061 int attribute_align_arg mpg123_copy_string(mpg123_string* from, mpg123_string* to) 00062 { 00063 size_t fill; 00064 char *text; 00065 if(to == NULL) return -1; 00066 00067 debug2("called copy_string with %p -> %p", (void*)from, (void*)to); 00068 if(from == NULL) 00069 { 00070 fill = 0; 00071 text = NULL; 00072 } 00073 else 00074 { 00075 fill = from->fill; 00076 text = from->p; 00077 } 00078 00079 if(mpg123_resize_string(to, fill)) 00080 { 00081 memcpy(to->p, text, fill); 00082 to->fill = fill; 00083 return 1; 00084 } 00085 else return 0; 00086 } 00087 00088 int attribute_align_arg mpg123_add_string(mpg123_string* sb, const char* stuff) 00089 { 00090 debug1("adding %s", stuff); 00091 return mpg123_add_substring(sb, stuff, 0, strlen(stuff)); 00092 } 00093 00094 int attribute_align_arg mpg123_add_substring(mpg123_string *sb, const char *stuff, size_t from, size_t count) 00095 { 00096 debug("adding a substring"); 00097 if(sb->fill) /* includes zero byte... */ 00098 { 00099 if( (SIZE_MAX - sb->fill >= count) /* Avoid overflow. */ 00100 && (sb->size >= sb->fill+count || mpg123_grow_string(sb, sb->fill+count)) ) 00101 { 00102 memcpy(sb->p+sb->fill-1, stuff+from, count); 00103 sb->fill += count; 00104 sb->p[sb->fill-1] = 0; /* Terminate! */ 00105 } 00106 else return 0; 00107 } 00108 else 00109 { 00110 if( count < SIZE_MAX && mpg123_grow_string(sb, count+1) ) 00111 { 00112 memcpy(sb->p, stuff+from, count); 00113 sb->fill = count+1; 00114 sb->p[sb->fill-1] = 0; /* Terminate! */ 00115 } 00116 else return 0; 00117 } 00118 return 1; 00119 } 00120 00121 int attribute_align_arg mpg123_set_substring(mpg123_string* sb, const char* stuff, size_t from, size_t count) 00122 { 00123 sb->fill = 0; 00124 return mpg123_add_substring(sb, stuff, from, count); 00125 } 00126 00127 int attribute_align_arg mpg123_set_string(mpg123_string* sb, const char* stuff) 00128 { 00129 sb->fill = 0; 00130 return mpg123_add_string(sb, stuff); 00131 } Generated on Sun May 27 2012 04:34:10 for ReactOS by
1.7.6.1
|