comparison 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
comparison
equal deleted inserted replaced
205:de47d797ee33 206:8b896d461fdb
18 You should have received a copy of the GNU General Public License 18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software 19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */ 21 */
22 22
23 #include <string.h>
23 #include "xs_support.h" 24 #include "xs_support.h"
24 #include <string.h>
25 #include <ctype.h>
26 25
27 26
28 /* 27 /* Copy a string
29 * Utility routines
30 */ 28 */
31 gint xs_strcalloc(gchar **ppcResult, const gchar *pcStr) 29 gchar *xs_strncpy(gchar *pDest, gchar *pSource, size_t n)
32 { 30 {
33 assert(ppcResult); 31 gchar *s, *d;
34 assert(pcStr); 32 size_t i;
35 33
36 /* Allocate memory for destination */ 34 /* Check the string pointers */
37 *ppcResult = (gchar *) g_realloc(*ppcResult, strlen(pcStr) + 1); 35 if (!pSource || !pDest) return pDest;
38 if (*ppcResult == NULL) return -1;
39 36
40 /* Copy to the destination */ 37 /* Copy to the destination */
41 strcpy(*ppcResult, pcStr); 38 i = n; s = pSource; d = pDest;
39 while (*s && (i > 0)) { *(d++) = *(s++); i--; }
40
41 /* Fill rest of space with zeros */
42 while (i > 0) { *(d++) = 0; i--; }
43
44 /* Ensure that last is always zero */
45 pDest[n - 1] = 0;
46
47 return pDest;
48 }
49
50
51 /* Copy a given string over in *ppResult.
52 */
53 gint xs_pstrcpy(gchar **ppResult, const gchar *pStr)
54 {
55 /* Check the string pointers */
56 if (!ppResult || !pStr) return -1;
57
58 /* Allocate memory for destination */
59 if (*ppResult) g_free(*ppResult);
60 *ppResult = (gchar *) g_malloc(strlen(pStr) + 1);
61 if (!*ppResult) return -2;
62
63 /* Copy to the destination */
64 strcpy(*ppResult, pStr);
42 65
43 return 0; 66 return 0;
44 } 67 }
45 68
46 69
47 int xs_strcat(gchar **ppcResult, const gchar *pcStr) 70 /* Concatenates a given string into string pointed by *ppResult.
71 */
72 gint th_pstrcat(t_char **ppResult, const gchar *pStr)
48 { 73 {
49 assert(ppcResult); 74 /* Check the string pointers */
50 assert(pcStr); 75 if (!ppResult || !pStr) return -1;
51
52 /* Re-allocate memory for destination */
53 *ppcResult = (gchar *) g_realloc(*ppcResult, strlen(*ppcResult) + strlen(pcStr) + 1);
54 if (*ppcResult == NULL) return -1;
55 76
56 /* Cat to the destination */ 77 if (*ppResult != NULL)
57 strcat(*ppcResult, pcStr); 78 {
79 *ppResult = g_realloc(*ppResult, strlen(*ppResult) + strlen(pStr) + 1);
80 if (*ppResult == NULL) return -1;
81
82 strcat(*ppResult, pStr);
83 } else
84 {
85 *ppResult = g_malloc(strlen(pStr) + 1);
86 if (*ppResult == NULL) return -1;
87
88 strcpy(*ppResult, pStr);
89 }
58 90
59 return 0; 91 return 0;
60 } 92 }
61 93
62 94
95 /* Locate character in string
96 */
63 gchar *xs_strrchr(gchar *pcStr, gchar ch) 97 gchar *xs_strrchr(gchar *pcStr, gchar ch)
64 { 98 {
65 gchar *lastPos = NULL; 99 gchar *lastPos = NULL;
66 100
67 while (*pcStr) 101 while (*pcStr)