Mercurial > hg > xmms-sid
comparison src/xs_support.c @ 660:b0743dc9165d
Change tabs to 4 spaces, everywhere.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 02 Apr 2008 22:10:05 +0300 |
parents | acaba070cf49 |
children | 180d7a0250d8 |
comparison
equal
deleted
inserted
replaced
659:04ea91a61225 | 660:b0743dc9165d |
---|---|
24 #include <ctype.h> | 24 #include <ctype.h> |
25 | 25 |
26 | 26 |
27 guint16 xs_fread_be16(xs_file_t *f) | 27 guint16 xs_fread_be16(xs_file_t *f) |
28 { | 28 { |
29 return (((guint16) xs_fgetc(f)) << 8) | ((guint16) xs_fgetc(f)); | 29 return (((guint16) xs_fgetc(f)) << 8) | ((guint16) xs_fgetc(f)); |
30 } | 30 } |
31 | 31 |
32 | 32 |
33 guint32 xs_fread_be32(xs_file_t *f) | 33 guint32 xs_fread_be32(xs_file_t *f) |
34 { | 34 { |
35 return (((guint32) xs_fgetc(f)) << 24) | | 35 return (((guint32) xs_fgetc(f)) << 24) | |
36 (((guint32) xs_fgetc(f)) << 16) | | 36 (((guint32) xs_fgetc(f)) << 16) | |
37 (((guint32) xs_fgetc(f)) << 8) | | 37 (((guint32) xs_fgetc(f)) << 8) | |
38 ((guint32) xs_fgetc(f)); | 38 ((guint32) xs_fgetc(f)); |
39 } | 39 } |
40 | 40 |
41 | 41 |
42 /* Load a file to a buffer, return 0 on success, negative value on error | 42 /* Load a file to a buffer, return 0 on success, negative value on error |
43 */ | 43 */ |
44 gint xs_fload_buffer(const gchar *filename, guint8 **buf, size_t *bufSize) | 44 gint xs_fload_buffer(const gchar *filename, guint8 **buf, size_t *bufSize) |
45 { | 45 { |
46 xs_file_t *f; | 46 xs_file_t *f; |
47 glong seekPos; | 47 glong seekPos; |
48 | 48 |
49 /* Open file, get file size */ | 49 /* Open file, get file size */ |
50 if ((f = xs_fopen(filename, "rb")) == NULL) | 50 if ((f = xs_fopen(filename, "rb")) == NULL) |
51 return -1; | 51 return -1; |
52 | 52 |
53 xs_fseek(f, 0, SEEK_END); | 53 xs_fseek(f, 0, SEEK_END); |
54 seekPos = xs_ftell(f); | 54 seekPos = xs_ftell(f); |
55 | 55 |
56 if (seekPos > 0) { | 56 if (seekPos > 0) { |
57 size_t readSize = seekPos; | 57 size_t readSize = seekPos; |
58 if (readSize >= *bufSize || *buf == NULL) { | 58 if (readSize >= *bufSize || *buf == NULL) { |
59 /* Only re-allocate if the required size > current */ | 59 /* Only re-allocate if the required size > current */ |
60 if (*buf != NULL) { | 60 if (*buf != NULL) { |
61 g_free(*buf); | 61 g_free(*buf); |
62 *buf = NULL; | 62 *buf = NULL; |
63 } | 63 } |
64 | 64 |
65 *bufSize = seekPos; | 65 *bufSize = seekPos; |
66 | 66 |
67 *buf = (guint8 *) g_malloc(*bufSize * sizeof(guint8)); | 67 *buf = (guint8 *) g_malloc(*bufSize * sizeof(guint8)); |
68 if (*buf == NULL) { | 68 if (*buf == NULL) { |
69 xs_fclose(f); | 69 xs_fclose(f); |
70 return -2; | 70 return -2; |
71 } | 71 } |
72 } | 72 } |
73 | 73 |
74 /* Read data */ | 74 /* Read data */ |
75 xs_fseek(f, 0, SEEK_SET); | 75 xs_fseek(f, 0, SEEK_SET); |
76 readSize = xs_fread(*buf, sizeof(guint8), *bufSize, f); | 76 readSize = xs_fread(*buf, sizeof(guint8), *bufSize, f); |
77 xs_fclose(f); | 77 xs_fclose(f); |
78 | 78 |
79 if (readSize != *bufSize) | 79 if (readSize != *bufSize) |
80 return -3; | 80 return -3; |
81 else | 81 else |
82 return 0; | 82 return 0; |
83 } else { | 83 } else { |
84 xs_fclose(f); | 84 xs_fclose(f); |
85 return -4; | 85 return -4; |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 | 89 |
90 /* Copy a string | 90 /* Copy a string |
91 */ | 91 */ |
92 gchar *xs_strncpy(gchar *pDest, const gchar *pSource, size_t n) | 92 gchar *xs_strncpy(gchar *pDest, const gchar *pSource, size_t n) |
93 { | 93 { |
94 const gchar *s; | 94 const gchar *s; |
95 gchar *d; | 95 gchar *d; |
96 size_t i; | 96 size_t i; |
97 | 97 |
98 /* Check the string pointers */ | 98 /* Check the string pointers */ |
99 if (!pSource || !pDest) | 99 if (!pSource || !pDest) |
100 return pDest; | 100 return pDest; |
101 | 101 |
102 /* Copy to the destination */ | 102 /* Copy to the destination */ |
103 i = n; | 103 i = n; |
104 s = pSource; | 104 s = pSource; |
105 d = pDest; | 105 d = pDest; |
106 while (*s && (i > 0)) { | 106 while (*s && (i > 0)) { |
107 *(d++) = *(s++); | 107 *(d++) = *(s++); |
108 i--; | 108 i--; |
109 } | 109 } |
110 | 110 |
111 /* Fill rest of space with zeros */ | 111 /* Fill rest of space with zeros */ |
112 while (i > 0) { | 112 while (i > 0) { |
113 *(d++) = 0; | 113 *(d++) = 0; |
114 i--; | 114 i--; |
115 } | 115 } |
116 | 116 |
117 /* Ensure that last is always zero */ | 117 /* Ensure that last is always zero */ |
118 pDest[n - 1] = 0; | 118 pDest[n - 1] = 0; |
119 | 119 |
120 return pDest; | 120 return pDest; |
121 } | 121 } |
122 | 122 |
123 | 123 |
124 /* Copy a given string over in *ppResult. | 124 /* Copy a given string over in *ppResult. |
125 */ | 125 */ |
126 gint xs_pstrcpy(gchar **ppResult, const gchar *pStr) | 126 gint xs_pstrcpy(gchar **ppResult, const gchar *pStr) |
127 { | 127 { |
128 /* Check the string pointers */ | 128 /* Check the string pointers */ |
129 if (!ppResult || !pStr) | 129 if (!ppResult || !pStr) |
130 return -1; | 130 return -1; |
131 | 131 |
132 /* Allocate memory for destination */ | 132 /* Allocate memory for destination */ |
133 if (*ppResult) | 133 if (*ppResult) |
134 g_free(*ppResult); | 134 g_free(*ppResult); |
135 *ppResult = (gchar *) g_malloc(strlen(pStr) + 1); | 135 *ppResult = (gchar *) g_malloc(strlen(pStr) + 1); |
136 if (!*ppResult) | 136 if (!*ppResult) |
137 return -2; | 137 return -2; |
138 | 138 |
139 /* Copy to the destination */ | 139 /* Copy to the destination */ |
140 strcpy(*ppResult, pStr); | 140 strcpy(*ppResult, pStr); |
141 | 141 |
142 return 0; | 142 return 0; |
143 } | 143 } |
144 | 144 |
145 | 145 |
146 /* Concatenates a given string into string pointed by *ppResult. | 146 /* Concatenates a given string into string pointed by *ppResult. |
147 */ | 147 */ |
148 gint xs_pstrcat(gchar **ppResult, const gchar *pStr) | 148 gint xs_pstrcat(gchar **ppResult, const gchar *pStr) |
149 { | 149 { |
150 /* Check the string pointers */ | 150 /* Check the string pointers */ |
151 if (!ppResult || !pStr) | 151 if (!ppResult || !pStr) |
152 return -1; | 152 return -1; |
153 | 153 |
154 if (*ppResult != NULL) { | 154 if (*ppResult != NULL) { |
155 *ppResult = (gchar *) g_realloc(*ppResult, strlen(*ppResult) + strlen(pStr) + 1); | 155 *ppResult = (gchar *) g_realloc(*ppResult, strlen(*ppResult) + strlen(pStr) + 1); |
156 if (*ppResult == NULL) | 156 if (*ppResult == NULL) |
157 return -1; | 157 return -1; |
158 strcat(*ppResult, pStr); | 158 strcat(*ppResult, pStr); |
159 } else { | 159 } else { |
160 *ppResult = (gchar *) g_malloc(strlen(pStr) + 1); | 160 *ppResult = (gchar *) g_malloc(strlen(pStr) + 1); |
161 if (*ppResult == NULL) | 161 if (*ppResult == NULL) |
162 return -1; | 162 return -1; |
163 strcpy(*ppResult, pStr); | 163 strcpy(*ppResult, pStr); |
164 } | 164 } |
165 | 165 |
166 return 0; | 166 return 0; |
167 } | 167 } |
168 | 168 |
169 | 169 |
170 /* Concatenate a given string up to given dest size or \n. | 170 /* Concatenate a given string up to given dest size or \n. |
171 * If size max is reached, change the end to "..." | 171 * If size max is reached, change the end to "..." |
172 */ | 172 */ |
173 void xs_pnstrcat(gchar *pDest, size_t iSize, const gchar *pStr) | 173 void xs_pnstrcat(gchar *pDest, size_t iSize, const gchar *pStr) |
174 { | 174 { |
175 size_t i, n; | 175 size_t i, n; |
176 const gchar *s; | 176 const gchar *s; |
177 gchar *d; | 177 gchar *d; |
178 | 178 |
179 d = pDest; | 179 d = pDest; |
180 i = 0; | 180 i = 0; |
181 while (*d && (i < iSize)) { | 181 while (*d && (i < iSize)) { |
182 i++; | 182 i++; |
183 d++; | 183 d++; |
184 } | 184 } |
185 | 185 |
186 s = pStr; | 186 s = pStr; |
187 while (*s && (*s != '\n') && (i < iSize)) { | 187 while (*s && (*s != '\n') && (i < iSize)) { |
188 *d = *s; | 188 *d = *s; |
189 d++; | 189 d++; |
190 s++; | 190 s++; |
191 i++; | 191 i++; |
192 } | 192 } |
193 | 193 |
194 *d = 0; | 194 *d = 0; |
195 | 195 |
196 if (i >= iSize) { | 196 if (i >= iSize) { |
197 i--; | 197 i--; |
198 d--; | 198 d--; |
199 n = 3; | 199 n = 3; |
200 while ((i > 0) && (n > 0)) { | 200 while ((i > 0) && (n > 0)) { |
201 *d = '.'; | 201 *d = '.'; |
202 d--; | 202 d--; |
203 i--; | 203 i--; |
204 n--; | 204 n--; |
205 } | 205 } |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 | 209 |
210 /* Locate character in string | 210 /* Locate character in string |
211 */ | 211 */ |
212 gchar *xs_strrchr(gchar *pcStr, const gchar ch) | 212 gchar *xs_strrchr(gchar *pcStr, const gchar ch) |
213 { | 213 { |
214 gchar *lastPos = NULL; | 214 gchar *lastPos = NULL; |
215 | 215 |
216 while (*pcStr) { | 216 while (*pcStr) { |
217 if (*pcStr == ch) | 217 if (*pcStr == ch) |
218 lastPos = pcStr; | 218 lastPos = pcStr; |
219 pcStr++; | 219 pcStr++; |
220 } | 220 } |
221 | 221 |
222 return lastPos; | 222 return lastPos; |
223 } | 223 } |
224 | 224 |
225 | 225 |
226 void xs_findnext(const gchar *pcStr, size_t *piPos) | 226 void xs_findnext(const gchar *pcStr, size_t *piPos) |
227 { | 227 { |
228 while (pcStr[*piPos] && isspace(pcStr[*piPos])) | 228 while (pcStr[*piPos] && isspace(pcStr[*piPos])) |
229 (*piPos)++; | 229 (*piPos)++; |
230 } | 230 } |
231 | 231 |
232 | 232 |
233 void xs_findeol(const gchar *pcStr, size_t *piPos) | 233 void xs_findeol(const gchar *pcStr, size_t *piPos) |
234 { | 234 { |
235 while (pcStr[*piPos] && (pcStr[*piPos] != '\n') && (pcStr[*piPos] != '\r')) | 235 while (pcStr[*piPos] && (pcStr[*piPos] != '\n') && (pcStr[*piPos] != '\r')) |
236 (*piPos)++; | 236 (*piPos)++; |
237 } | 237 } |
238 | 238 |
239 | 239 |
240 void xs_findnum(const gchar *pcStr, size_t *piPos) | 240 void xs_findnum(const gchar *pcStr, size_t *piPos) |
241 { | 241 { |
242 while (pcStr[*piPos] && isdigit(pcStr[*piPos])) | 242 while (pcStr[*piPos] && isdigit(pcStr[*piPos])) |
243 (*piPos)++; | 243 (*piPos)++; |
244 } | 244 } |
245 | 245 |