269
|
1 /*
|
|
2 XMMS-SID - SIDPlay input plugin for X MultiMedia System (XMMS)
|
|
3
|
|
4 Miscellaneous support functions
|
|
5
|
|
6 Programmed and designed by Matti 'ccr' Hamalainen <ccr@tnsp.org>
|
|
7 (C) Copyright 1999-2005 Tecnic Software productions (TNSP)
|
|
8
|
|
9 This program is free software; you can redistribute it and/or modify
|
|
10 it under the terms of the GNU General Public License as published by
|
|
11 the Free Software Foundation; either version 2 of the License, or
|
|
12 (at your option) any later version.
|
|
13
|
|
14 This program is distributed in the hope that it will be useful,
|
|
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 GNU General Public License for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with this program; if not, write to the Free Software
|
1459
|
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
269
|
22 */
|
|
23 #include "xmms-sid.h"
|
|
24 #include "xs_support.h"
|
|
25 #include <ctype.h>
|
|
26
|
|
27
|
|
28 /* Bigendian file reading functions
|
|
29 */
|
|
30 guint16 xs_rd_be16(FILE * f)
|
|
31 {
|
|
32 return (((guint16) fgetc(f)) << 8) | ((guint16) fgetc(f));
|
|
33 }
|
|
34
|
|
35
|
|
36 guint32 xs_rd_be32(FILE * f)
|
|
37 {
|
|
38 return (((guint32) fgetc(f)) << 24) |
|
|
39 (((guint32) fgetc(f)) << 16) | (((guint32) fgetc(f)) << 8) | ((guint32) fgetc(f));
|
|
40 }
|
|
41
|
|
42
|
|
43 size_t xs_rd_str(FILE * f, gchar * s, size_t l)
|
|
44 {
|
|
45 return fread(s, sizeof(gchar), l, f);
|
|
46 }
|
|
47
|
|
48
|
|
49 /* Copy a string
|
|
50 */
|
|
51 gchar *xs_strncpy(gchar * pDest, gchar * pSource, size_t n)
|
|
52 {
|
|
53 gchar *s, *d;
|
|
54 size_t i;
|
|
55
|
|
56 /* Check the string pointers */
|
|
57 if (!pSource || !pDest)
|
|
58 return pDest;
|
|
59
|
|
60 /* Copy to the destination */
|
|
61 i = n;
|
|
62 s = pSource;
|
|
63 d = pDest;
|
|
64 while (*s && (i > 0)) {
|
|
65 *(d++) = *(s++);
|
|
66 i--;
|
|
67 }
|
|
68
|
|
69 /* Fill rest of space with zeros */
|
|
70 while (i > 0) {
|
|
71 *(d++) = 0;
|
|
72 i--;
|
|
73 }
|
|
74
|
|
75 /* Ensure that last is always zero */
|
|
76 pDest[n - 1] = 0;
|
|
77
|
|
78 return pDest;
|
|
79 }
|
|
80
|
|
81
|
|
82 /* Copy a given string over in *ppResult.
|
|
83 */
|
|
84 gint xs_pstrcpy(gchar ** ppResult, const gchar * pStr)
|
|
85 {
|
|
86 /* Check the string pointers */
|
|
87 if (!ppResult || !pStr)
|
|
88 return -1;
|
|
89
|
|
90 /* Allocate memory for destination */
|
|
91 if (*ppResult)
|
|
92 g_free(*ppResult);
|
|
93 *ppResult = (gchar *) g_malloc(strlen(pStr) + 1);
|
|
94 if (!*ppResult)
|
|
95 return -2;
|
|
96
|
|
97 /* Copy to the destination */
|
|
98 strcpy(*ppResult, pStr);
|
|
99
|
|
100 return 0;
|
|
101 }
|
|
102
|
|
103
|
|
104 /* Concatenates a given string into string pointed by *ppResult.
|
|
105 */
|
|
106 gint xs_pstrcat(gchar ** ppResult, const gchar * pStr)
|
|
107 {
|
|
108 /* Check the string pointers */
|
|
109 if (!ppResult || !pStr)
|
|
110 return -1;
|
|
111
|
|
112 if (*ppResult != NULL) {
|
|
113 *ppResult = (gchar *) g_realloc(*ppResult, strlen(*ppResult) + strlen(pStr) + 1);
|
|
114 if (*ppResult == NULL)
|
|
115 return -1;
|
|
116 strcat(*ppResult, pStr);
|
|
117 } else {
|
|
118 *ppResult = (gchar *) g_malloc(strlen(pStr) + 1);
|
|
119 if (*ppResult == NULL)
|
|
120 return -1;
|
|
121 strcpy(*ppResult, pStr);
|
|
122 }
|
|
123
|
|
124 return 0;
|
|
125 }
|
|
126
|
|
127
|
|
128 /* Concatenate a given string up to given dest size or \n.
|
|
129 * If size max is reached, change the end to "..."
|
|
130 */
|
|
131 void xs_pnstrcat(gchar * pDest, size_t iSize, gchar * pStr)
|
|
132 {
|
|
133 size_t i, n;
|
|
134 gchar *s, *d;
|
|
135
|
|
136 d = pDest;
|
|
137 i = 0;
|
|
138 while (*d && (i < iSize)) {
|
|
139 i++;
|
|
140 d++;
|
|
141 }
|
|
142
|
|
143 s = pStr;
|
|
144 while (*s && (*s != '\n') && (i < iSize)) {
|
|
145 *d = *s;
|
|
146 d++;
|
|
147 s++;
|
|
148 i++;
|
|
149 }
|
|
150
|
|
151 *d = 0;
|
|
152
|
|
153 if (i >= iSize) {
|
|
154 i--;
|
|
155 d--;
|
|
156 n = 3;
|
|
157 while ((i > 0) && (n > 0)) {
|
|
158 *d = '.';
|
|
159 d--;
|
|
160 i--;
|
|
161 n--;
|
|
162 }
|
|
163 }
|
|
164 }
|
|
165
|
|
166
|
|
167 /* Locate character in string
|
|
168 */
|
|
169 gchar *xs_strrchr(gchar * pcStr, gchar ch)
|
|
170 {
|
|
171 gchar *lastPos = NULL;
|
|
172
|
|
173 while (*pcStr) {
|
|
174 if (*pcStr == ch)
|
|
175 lastPos = pcStr;
|
|
176 pcStr++;
|
|
177 }
|
|
178
|
|
179 return lastPos;
|
|
180 }
|
|
181
|
|
182
|
|
183 void xs_findnext(gchar * pcStr, guint * piPos)
|
|
184 {
|
|
185 while (pcStr[*piPos] && isspace(pcStr[*piPos]))
|
|
186 (*piPos)++;
|
|
187 }
|
|
188
|
|
189
|
|
190 void xs_findeol(gchar * pcStr, guint * piPos)
|
|
191 {
|
|
192 while (pcStr[*piPos] && (pcStr[*piPos] != '\n') && (pcStr[*piPos] != '\r'))
|
|
193 (*piPos)++;
|
|
194 }
|
|
195
|
|
196
|
|
197 void xs_findnum(gchar * pcStr, guint * piPos)
|
|
198 {
|
|
199 while (pcStr[*piPos] && isdigit(pcStr[*piPos]))
|
|
200 (*piPos)++;
|
|
201 }
|
|
202
|
|
203
|
|
204 #ifndef HAVE_MEMSET
|
|
205 void *xs_memset(void *p, int c, size_t n)
|
|
206 {
|
|
207 gchar *dp;
|
|
208
|
|
209 dp = (gchar *) p;
|
|
210 while (n--) {
|
|
211 *dp = (gchar) c;
|
|
212 n--;
|
|
213 }
|
|
214
|
|
215 return p;
|
|
216 }
|
|
217 #endif
|