annotate src/xs_support.c @ 206:8b896d461fdb

Trivial fixes and added strncpy replacement.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 15 Dec 2004 10:22:05 +0000
parents fe684a2ccdc7
children 575686094eb1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 XMMS-SID - SIDPlay input plugin for X MultiMedia System (XMMS)
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 Miscellaneous support functions
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 Written by Matti "ccr" Hamalainen <ccr@tnsp.org>
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 This program is free software; you can redistribute it and/or modify
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 the Free Software Foundation; either version 2 of the License, or
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 (at your option) any later version.
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 GNU General Public License for more details.
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 along with this program; if not, write to the Free Software
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 */
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
206
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
23 #include <string.h>
49
3518ca5c8b0f Support for compile-time big/little-endian optimizations in audio output
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
24 #include "xs_support.h"
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25
202
fe684a2ccdc7 Reworking code.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
26
206
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
27 /* Copy a string
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 */
206
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
29 gchar *xs_strncpy(gchar *pDest, gchar *pSource, size_t n)
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 {
206
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
31 gchar *s, *d;
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
32 size_t i;
38
dec37e16136e Minor changes
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
33
206
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
34 /* Check the string pointers */
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
35 if (!pSource || !pDest) return pDest;
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36
38
dec37e16136e Minor changes
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
37 /* Copy to the destination */
206
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
38 i = n; s = pSource; d = pDest;
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
39 while (*s && (i > 0)) { *(d++) = *(s++); i--; }
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
40
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
41 /* Fill rest of space with zeros */
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
42 while (i > 0) { *(d++) = 0; i--; }
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
43
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
44 /* Ensure that last is always zero */
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
45 pDest[n - 1] = 0;
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
46
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
47 return pDest;
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
48 }
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
49
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
50
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
51 /* Copy a given string over in *ppResult.
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
52 */
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
53 gint xs_pstrcpy(gchar **ppResult, const gchar *pStr)
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
54 {
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
55 /* Check the string pointers */
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
56 if (!ppResult || !pStr) return -1;
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
57
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
58 /* Allocate memory for destination */
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
59 if (*ppResult) g_free(*ppResult);
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
60 *ppResult = (gchar *) g_malloc(strlen(pStr) + 1);
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
61 if (!*ppResult) return -2;
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
62
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
63 /* Copy to the destination */
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
64 strcpy(*ppResult, pStr);
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65
38
dec37e16136e Minor changes
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
66 return 0;
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 }
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69
206
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
70 /* Concatenates a given string into string pointed by *ppResult.
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
71 */
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
72 gint th_pstrcat(t_char **ppResult, const gchar *pStr)
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 {
206
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
74 /* Check the string pointers */
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
75 if (!ppResult || !pStr) return -1;
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
76
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
77 if (*ppResult != NULL)
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
78 {
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
79 *ppResult = g_realloc(*ppResult, strlen(*ppResult) + strlen(pStr) + 1);
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
80 if (*ppResult == NULL) return -1;
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
206
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
82 strcat(*ppResult, pStr);
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
83 } else
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
84 {
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
85 *ppResult = g_malloc(strlen(pStr) + 1);
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
86 if (*ppResult == NULL) return -1;
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
87
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
88 strcpy(*ppResult, pStr);
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
89 }
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90
38
dec37e16136e Minor changes
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
91 return 0;
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 }
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
206
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
95 /* Locate character in string
8b896d461fdb Trivial fixes and added strncpy replacement.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
96 */
56
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
97 gchar *xs_strrchr(gchar *pcStr, gchar ch)
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
98 {
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
99 gchar *lastPos = NULL;
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
100
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
101 while (*pcStr)
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
102 {
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
103 if (*pcStr == ch) lastPos = pcStr;
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
104 pcStr++;
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
105 }
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
106
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
107 return lastPos;
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
108 }
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
109
6a3ce7260ae1 Changed some standard functions to glib ones
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
110
109
578b71b62eeb Fileinfo now basically working, STIL COMMENT-fields are not yet parsed or shown.
Matti Hamalainen <ccr@tnsp.org>
parents: 87
diff changeset
111 inline void xs_findnext(gchar *pcStr, guint *piPos)
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 {
109
578b71b62eeb Fileinfo now basically working, STIL COMMENT-fields are not yet parsed or shown.
Matti Hamalainen <ccr@tnsp.org>
parents: 87
diff changeset
113 while (pcStr[*piPos] && isspace(pcStr[*piPos])) (*piPos)++;
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 }
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116
109
578b71b62eeb Fileinfo now basically working, STIL COMMENT-fields are not yet parsed or shown.
Matti Hamalainen <ccr@tnsp.org>
parents: 87
diff changeset
117 inline void xs_findeol(gchar *pcStr, guint *piPos)
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 {
109
578b71b62eeb Fileinfo now basically working, STIL COMMENT-fields are not yet parsed or shown.
Matti Hamalainen <ccr@tnsp.org>
parents: 87
diff changeset
119 while (pcStr[*piPos] && (pcStr[*piPos] != '\n') && (pcStr[*piPos] != '\r')) (*piPos)++;
4
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 }
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121
4bb09e405eab Added new files for 0.8
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122
109
578b71b62eeb Fileinfo now basically working, STIL COMMENT-fields are not yet parsed or shown.
Matti Hamalainen <ccr@tnsp.org>
parents: 87
diff changeset
123 inline void xs_findnum(gchar *pcStr, guint *piPos)
578b71b62eeb Fileinfo now basically working, STIL COMMENT-fields are not yet parsed or shown.
Matti Hamalainen <ccr@tnsp.org>
parents: 87
diff changeset
124 {
578b71b62eeb Fileinfo now basically working, STIL COMMENT-fields are not yet parsed or shown.
Matti Hamalainen <ccr@tnsp.org>
parents: 87
diff changeset
125 while (pcStr[*piPos] && isdigit(pcStr[*piPos])) (*piPos)++;
578b71b62eeb Fileinfo now basically working, STIL COMMENT-fields are not yet parsed or shown.
Matti Hamalainen <ccr@tnsp.org>
parents: 87
diff changeset
126 }