kicad/common/string.cpp

376 lines
9.0 KiB
C++
Raw Normal View History

2007-05-06 16:03:28 +00:00
/****************************************************************************/
/* MODULE: string.cpp */
/* ROLE: fonctions complementaires de traitement de chaines de caracteres */
/****************************************************************************/
#include "fctsys.h"
#include <time.h>
#include "common.h"
2007-05-06 16:03:28 +00:00
/*********************************************************************/
2007-12-01 05:53:52 +00:00
int ReadDelimitedText( char* dest, char* source, int NbMaxChar )
2007-05-06 16:03:28 +00:00
/*********************************************************************/
2007-12-01 05:53:52 +00:00
2007-05-06 16:03:28 +00:00
/* lit et place dans dest la chaine de caractere trouvee dans source,
2007-12-01 05:53:52 +00:00
* delimitee par " .
* transfere NbMaxChar max
* retourne le nombre de codes lus dans source
* dest est termine par NULL
*/
2007-05-06 16:03:28 +00:00
{
2007-12-01 05:53:52 +00:00
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;
2007-05-06 16:03:28 +00:00
}
/********************************/
2007-12-01 05:53:52 +00:00
char* StrPurge( char* text )
2007-05-06 16:03:28 +00:00
/********************************/
2007-12-01 05:53:52 +00:00
2007-05-06 16:03:28 +00:00
/* Supprime les caracteres Space en debut de la ligne text
2007-12-01 05:53:52 +00:00
* retourne un pointeur sur le 1er caractere non Space de text
*/
2007-05-06 16:03:28 +00:00
{
2007-12-01 05:53:52 +00:00
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;
2007-05-06 16:03:28 +00:00
}
/*****************************************************************/
2007-12-01 05:53:52 +00:00
char* GetLine( FILE* File, char* Line, int* LineNum, int SizeLine )
2007-05-06 16:03:28 +00:00
/*****************************************************************/
2007-12-01 05:53:52 +00:00
2007-05-06 16:03:28 +00:00
/* Routine de lecture de 1 ligne utile
2007-12-01 05:53:52 +00:00
* retourne la 1ere ligne utile lue.
* elimine lignes vides et commentaires
* incremente *LineNum a chaque ligne lue
*/
2007-05-06 16:03:28 +00:00
{
2007-12-01 05:53:52 +00:00
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;
2007-05-06 16:03:28 +00:00
}
/*******************************/
2007-12-01 05:53:52 +00:00
char* DateAndTime( char* Line )
2007-05-06 16:03:28 +00:00
/*******************************/
/* Retourne la chaine de caractere donnant date+heure */
{
2007-12-01 05:53:52 +00:00
time_t Time_Buf;
struct tm* Date;
2007-05-06 16:03:28 +00:00
2007-12-01 05:53:52 +00:00
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 );
2007-05-06 16:03:28 +00:00
2007-12-01 05:53:52 +00:00
return Line;
2007-05-06 16:03:28 +00:00
}
2007-12-01 05:53:52 +00:00
2007-05-06 16:03:28 +00:00
/*******************************/
wxString DateAndTime()
2007-05-06 16:03:28 +00:00
/*******************************/
/* Retourne la chaine de caractere donnant date+heure */
{
2007-12-01 05:53:52 +00:00
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;
2007-05-06 16:03:28 +00:00
}
/************************************************************/
2007-12-01 05:53:52 +00:00
int StrLenNumCmp( const wxChar* str1, const wxChar* str2, int NbMax )
2007-05-06 16:03:28 +00:00
/************************************************************/
2007-12-01 05:53:52 +00:00
2007-05-06 16:03:28 +00:00
/*
2007-12-01 05:53:52 +00:00
* routine (compatible qsort() ) de comparaison pour classement alphab<EFBFBD>tique
* Analogue a strncmp() mais les nombres sont compar<EFBFBD>s selon leur valeur num<EFBFBD>rique
* et non pas par leur code ascii
*/
2007-05-06 16:03:28 +00:00
{
2007-12-01 05:53:52 +00:00
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;
2007-05-06 16:03:28 +00:00
}
/***********************************************/
2007-12-01 05:53:52 +00:00
int StrNumICmp( const wxChar* str1, const wxChar* str2 )
2007-05-06 16:03:28 +00:00
/***********************************************/
2007-12-01 05:53:52 +00:00
2007-05-06 16:03:28 +00:00
/*
2007-12-01 05:53:52 +00:00
* routine (compatible qsort() ) de comparaison pour classement alphab<EFBFBD>tique,
* avec lower case == upper case.
* Analogue a stricmp() mais les nombres sont compar<EFBFBD>s selon leur valeur num<EFBFBD>rique
* et non pas par leur code ascii
*/
2007-05-06 16:03:28 +00:00
{
2007-12-01 05:53:52 +00:00
return StrLenNumICmp( str1, str2, 32735 );
2007-05-06 16:03:28 +00:00
}
/**************************************************************/
2007-12-01 05:53:52 +00:00
int StrLenNumICmp( const wxChar* str1, const wxChar* str2, int NbMax )
2007-05-06 16:03:28 +00:00
/**************************************************************/
2007-12-01 05:53:52 +00:00
2007-05-06 16:03:28 +00:00
/*
2007-12-01 05:53:52 +00:00
* 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
*/
2007-05-06 16:03:28 +00:00
{
2007-12-01 05:53:52 +00:00
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;
2007-05-06 16:03:28 +00:00
}
/***********************************************************************/
2007-12-01 05:53:52 +00:00
bool WildCompareString( const wxString& pattern, const wxString& string_to_tst,
bool case_sensitive )
2007-05-06 16:03:28 +00:00
/***********************************************************************/
2007-12-01 05:53:52 +00:00
2007-05-06 16:03:28 +00:00
/* compare 2 noms de composants, selon regles usuelles
2007-12-01 05:53:52 +00:00
* ( Jokers * , ? , autoris<EFBFBD>s).
* la chaine de reference est "pattern"
* si case_sensitive == TRUE, comparaison exacte
* retourne TRUE si match
* retourne FALSE si differences
*/
2007-05-06 16:03:28 +00:00
{
2007-12-01 05:53:52 +00:00
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;
2007-05-06 16:03:28 +00:00
}
2007-12-01 05:53:52 +00:00
2007-05-06 16:03:28 +00:00
/***********************************************/
2007-12-01 05:53:52 +00:00
void ChangeSpaces( char* Text, int NewChar )
2007-05-06 16:03:28 +00:00
/***********************************************/
/* Change dans un texte les espaces en NewChar */
{
2007-12-01 05:53:52 +00:00
if( Text == NULL )
return;
while( *Text )
{
if( *Text == ' ' )
*Text = (char) NewChar;
Text++;
}
2007-05-06 16:03:28 +00:00
}
/***************************/
2007-12-01 05:53:52 +00:00
char* to_point( char* Text )
2007-05-06 16:03:28 +00:00
/**************************/
2007-12-01 05:53:52 +00:00
2007-05-06 16:03:28 +00:00
/* convertit les , en . dans une chaine. utilis<69> pour compenser
2007-12-01 05:53:52 +00:00
* l'internalisationde la fct printf
* qui genere les flottants avec une virgule au lieu du point
*/
2007-05-06 16:03:28 +00:00
{
2007-12-01 05:53:52 +00:00
char* line = Text;
2007-05-06 16:03:28 +00:00
2007-12-01 05:53:52 +00:00
if( Text == NULL )
return NULL;
for( ; *Text != 0; Text++ )
{
if( *Text == ',' )
*Text = '.';
}
2007-05-06 16:03:28 +00:00
2007-12-01 05:53:52 +00:00
return line;
2007-05-06 16:03:28 +00:00
}
/********************************/
2007-12-01 05:53:52 +00:00
char* strupper( char* Text )
/********************************/
2007-12-01 05:53:52 +00:00
/* Change les caracteres 'a' ... 'z' en 'A' ... 'Z'. dans la chaine Text.
2007-12-01 05:53:52 +00:00
* Retourne Text
*/
{
2007-12-01 05:53:52 +00:00
char* code = Text;
if( Text )
{
while( *code )
{
if( (*code >= 'a') && (*code <= 'z') )
*code += 'A' - 'a';
code++;
}
}
return Text;
}
2007-12-22 07:17:22 +00:00