File: string.cpp

package info (click to toggle)
kicad 0.0.20060829-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 56,544 kB
  • ctags: 9,194
  • sloc: cpp: 88,990; ansic: 4,790; makefile: 88; sh: 39
file content (342 lines) | stat: -rw-r--r-- 8,295 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/****************************************************************************/
/*	 MODULE:					 string.cpp									*/
/*	 ROLE: fonctions complementaires de traitement de chaines de caracteres */
/****************************************************************************/

#include "fctsys.h"
#include <time.h>
#include "common.h"


/*********************************************************************/
int ReadDelimitedText(char * dest, char * source, int NbMaxChar )
/*********************************************************************/
/* lit et place dans dest la chaine de caractere trouvee dans source,
	delimitee par " .
	transfere NbMaxChar max
	retourne le nombre de codes lus dans source
	dest est termine par NULL
*/
{
int ii, jj, flag = 0;

	 for ( ii = 0, jj = 0; ii < NbMaxChar - 1 ; jj++, source++)
		{
		if ( * source == 0 ) break; /* fin de ligne */
		if ( * source == '"' )			/* delimiteur trouve */
			{
			if ( flag ) break;			/* Fin de texte delimite */
			flag = 1;					/* Marque 1er delimiteur trouve */
			}
		else if ( flag )
			{
			* dest = * source; dest++; ii++;
			}
		}
	*dest = 0;  /* Null terminaison */
	return (jj);
}


/********************************/
char * StrPurge(char * text)
/********************************/
/* Supprime les caracteres Space en debut de la ligne text
	retourne un pointeur sur le 1er caractere non Space de text
*/
{
char * ptspace;
	if ( text == NULL ) return NULL;

	while( (*text <= ' ') && *text ) text++;
	ptspace = text + strlen(text) -1;
	while( (*ptspace <= ' ') && *ptspace && (ptspace >= text) )
		{
		*ptspace = 0; ptspace--;
		}
	return(text);
}


/*****************************************************************/
char * GetLine(FILE *File, char *Line, int *LineNum, int SizeLine)
/*****************************************************************/
/* Routine de lecture de 1 ligne utile
	retourne la 1ere ligne utile lue.
	elimine lignes vides et commentaires
	incremente *LineNum a chaque ligne lue
*/
{
	do  {
		if (fgets(Line, SizeLine, File) == NULL) return NULL;
		if( LineNum ) *LineNum += 1;
		} while (Line[0] == '#' || Line[0] == '\n' ||  Line[0] == '\r' ||
				 Line[0] == 0);

	strtok(Line,"\n\r");
	return Line;
}


/*******************************/
char * DateAndTime(char * Line)
/*******************************/
/* Retourne la chaine de caractere donnant date+heure */
{
time_t Time_Buf;
struct tm * Date;

	time(&Time_Buf);
	Date = gmtime(&Time_Buf);
	sprintf(Line,"%d/%d/%d-%2.2d:%2.2d:%2.2d",
			Date->tm_mday, Date->tm_mon + 1, Date->tm_year + 1900,
			Date->tm_hour, Date->tm_min, Date->tm_sec );

	return(Line);
}

/*******************************/
wxString DateAndTime(void)
/*******************************/
/* Retourne la chaine de caractere donnant date+heure */
{
time_t Time_Buf;
struct tm * Date;
wxString Line;

	time(&Time_Buf);
	Date = gmtime(&Time_Buf);
	Line.Printf( wxT("%d/%d/%d-%2.2d:%2.2d:%2.2d"),
			Date->tm_mday, Date->tm_mon + 1, Date->tm_year + 1900,
			Date->tm_hour, Date->tm_min, Date->tm_sec );

	return(Line);
}


/************************************************************/
int StrLenNumCmp(const wxChar *str1,const wxChar *str2, int NbMax)
/************************************************************/
/*
routine (compatible qsort() ) de comparaison pour classement alphabtique
Analogue a strncmp() mais les nombres sont compars selon leur valeur numrique
et non pas par leur code ascii
*/
{
int i;
int nb1 = 0 , nb2 = 0;

	if( (str1 == NULL) || (str2 == NULL) ) return(0);

	for ( i = 0 ; i < NbMax ; i++ )
		{
		if (isdigit(*str1) && isdigit(*str2) ) /* nombres en jeu */
			{
			nb1 = 0 ; nb2 = 0 ;
			while (isdigit(*str1))
				{
				nb1 = nb1*10 + *str1 -'0'; str1++;
				}
			while (isdigit(*str2))
				{
				nb2 = nb2*10 + *str2 -'0'; str2++;
				}
			if ( nb1 < nb2 ) return(-1) ;
			if ( nb1 > nb2 ) return(1) ;
			}

		if( *str1 < *str2 ) return(-1) ;
		if( *str1 > *str2 ) return(1) ;
		if( (*str1 == 0 ) && ( *str2 == 0 ) ) return(0) ;
		str1++ ; str2++ ;
		}

	return(0);
}


/***********************************************/
int StrNumICmp(const wxChar *str1,const wxChar *str2)
/***********************************************/
/*
routine (compatible qsort() ) de comparaison pour classement alphabtique,
avec lower case == upper case.
Analogue a stricmp() mais les nombres sont compars selon leur valeur numrique
et non pas par leur code ascii
*/
{
	return StrLenNumICmp( str1, str2, 32735);
}


/**************************************************************/
int StrLenNumICmp(const wxChar *str1,const wxChar *str2, int NbMax)
/**************************************************************/
/*
routine (compatible qsort() ) de comparaison pour classement alphabetique,
avec lower case == upper case.
Analogue a stricmp() mais les nombres sont compares selon leur valeur numerique
et non pas par leur code ascii
*/
{
int i;
int nb1 = 0 , nb2 = 0;

	if( (str1 == NULL) || (str2 == NULL) ) return(0);

	for ( i = 0 ; i < NbMax ; i++ )
		{
		if (isdigit(*str1) && isdigit(*str2) ) /* nombres en jeu */
			{
			nb1 = 0 ; nb2 = 0 ;
			while (isdigit(*str1))
				{
				nb1 = nb1*10 + *str1 -'0'; str1++;
				}
			while (isdigit(*str2))
				{
				nb2 = nb2*10 + *str2 -'0'; str2++;
				}
			if ( nb1 < nb2 ) return(-1) ;
			if ( nb1 > nb2 ) return(1) ;
			}

		if( toupper(*str1) < toupper(*str2) ) return(-1) ;
		if( toupper(*str1) > toupper(*str2) ) return(1) ;
		if( (*str1 == 0 ) && ( *str2 == 0 ) ) return(0) ;
		str1++ ; str2++ ;
		}

	return(0);
}


/***********************************************************************/
bool WildCompareString(const wxString & pattern, const wxString & string_to_tst,
			bool case_sensitive )
/***********************************************************************/
/* compare 2 noms de composants, selon regles usuelles
	( Jokers * , ? , autoriss).
	la chaine de reference est "pattern"
	si case_sensitive == TRUE, comparaison exacte
	retourne TRUE si match
	retourne FALSE si differences
*/
{
const wxChar *cp = NULL, *mp = NULL;
const wxChar * wild, * string;
wxString _pattern, _string_to_tst;

	if ( case_sensitive )
	{
		wild = pattern.GetData(); string = string_to_tst.GetData();
	}
	else
	{
		_pattern = pattern; _pattern.MakeUpper();
		_string_to_tst = string_to_tst; _string_to_tst.MakeUpper();
		wild = _pattern.GetData(); string = _string_to_tst.GetData();
	}

	while ( (*string) && (*wild != '*') )
	{
		if ( (*wild != *string) && (*wild != '?') ) return FALSE;
		wild++; string++;
	}

	while (*string)
	{
		if (*wild == '*')
		{
			if (!*++wild) return 1;
			mp = wild;
			cp = string+1;
		}
		else if ((*wild == *string) || (*wild == '?'))
		{
			wild++;
			string++;
		}
		else
		{
			wild = mp;
			string = cp++;
		}
	}

	while (*wild == '*') {
		wild++;
	}
	return !*wild;
}

/***********************************************/
void ChangeSpaces(char * Text, int NewChar)
/***********************************************/
/* Change dans un texte les espaces en NewChar */
{
	if ( Text == NULL ) return;
	while( *Text )
		{
		if( *Text == ' ') *Text = (char) NewChar;
		Text++;
		}
}



/***************************/
char * to_point(char * Text)
/**************************/
/* convertit les , en . dans une chaine. utilis pour compenser
l'internalisation imbecile de la fct printf
qui genere les flottants avec une virgule au lieu du point
*/
{
char * line = Text;

	if ( Text == NULL ) return NULL;
	for ( ; *Text != 0; Text++ )
		if (*Text == g_FloatSeparator) *Text = '.';

	return line;
}

/****************************/
char * from_point(char * Text)
/****************************/
/* convertit les . en , dans une chaine. utilis pour compenser
l'internalisation imbecile de la fct scanf
qui lit les flottants avec une virgule au lieu du point
*/
{
char * line = Text;
	if ( Text == NULL ) return NULL;

	for ( ; *Text != 0; Text++ )
		if (*Text == '.') *Text = g_FloatSeparator;

	return line;
}

/********************************/
char * strupper(char * Text)
/********************************/
/* Change les caracteres 'a' ... 'z' en 'A' ... 'Z'. dans la chaine Text.
Retourne Text
*/
{
char * code = Text;

	if( Text )
		{
		while( * code)
			{
			if( (*code >= 'a') && (*code <= 'z') ) *code += 'A' - 'a';
			code ++;
			}
		}

	return(Text);
}