File: libstring.c

package info (click to toggle)
geda 20060123-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,596 kB
  • ctags: 1,743
  • sloc: ansic: 15,268; sh: 4,244; makefile: 559; yacc: 316; sed: 16
file content (148 lines) | stat: -rw-r--r-- 4,637 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* $Id: libstring.c,v 1.3 2005/02/12 22:06:03 danmc Exp $ */

/*******************************************************************************/
/*                                                                             */
/* gEDA Suite Project Manager                                                  */
/*                                                                             */
/* Copyright (C) 2002 Piotr Miarecki, sp9rve@radioam.net                       */
/*                                                                             */
/* This program is free software; you can redistribute it and/or               */
/* modify it under the terms of the GNU General Public License                 */
/* as published by the Free Software Foundation version 2.                     */
/*                                                                             */
/* This program is distributed in the hope that it will be useful,             */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of              */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               */
/* GNU General Public License for more details.                                */
/*                                                                             */
/* You should have received a copy of the GNU General Public License           */
/* along with this program; if not, email to the author                        */
/*                                                                             */
/*******************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include "libstring.h"



/********************************************************************************

	Create a new string

********************************************************************************/

char *StringCreate(void)
{
	char *pString = NULL;

	pString = (char *) malloc(sizeof(char));
	if (pString != NULL)
		strcpy(pString, "");

	return pString;
}



/********************************************************************************

	Copy a string

********************************************************************************/

void StringCopy(char **pString, const char *pSource)
{
	*pString = (char *) realloc(*pString, strlen(pSource) + sizeof(char));
		strcpy(*pString, pSource);
}



/********************************************************************************

	Cat to a string

********************************************************************************/

void StringCat(char **pString, const char *pSource)
{
	*pString = (char *) realloc(*pString, strlen(*pString) + strlen(pSource) + sizeof(char));
		strcat(*pString, pSource);
}



/*******************************************************************************

	Copy a string (limited number of characters)

********************************************************************************/

void StringCopyNum(char **pString, const char *pSource, const int iNumber)
{
	*pString = (char *) realloc(*pString, (strlen(pSource) > iNumber ? iNumber : strlen(pSource)) + sizeof(char));
	strncpy(*pString, pSource, iNumber);
	(*pString)[strlen(pSource) > iNumber ? iNumber : strlen(pSource)] = 0;
}



/*******************************************************************************/
/*	Length of a string                                                     */
/*******************************************************************************/

int StringLength(char **pString)
{
	return strlen(*pString);
}



/*******************************************************************************/
/*	Convert String to float                                                */
/*******************************************************************************/

float String2Float(char *pString)
{
	float f;

	sscanf(pString, "%f", &f);

	return f;
}



/*******************************************************************************/
/*	Convert String to integer                                              */
/*******************************************************************************/

int String2Integer(char *pString)
{
	int i;

	sscanf(pString, "%d", &i);

	return i;
}



/*******************************************************************************/
/*	Destroy a string                                                       */
/*******************************************************************************/

void StringDestroy(char **pString)
{
	free((void *) *pString);
}