string.cpp 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2004 KiCad Developers, see change_log.txt for contributors.
 *
 * 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; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

24 25 26 27
/**
 * @file string.cpp
 * @brief Some useful functions to handle strings.
 */
plyatov's avatar
plyatov committed
28

29 30
#include <fctsys.h>
#include <macros.h>
31
#include <richio.h>                        // StrPrintf
32
#include <kicad_string.h>
33 34


35 36 37 38 39 40 41 42
/**
 * Illegal file name characters used to insure file names will be valid on all supported
 * platforms.  This is the list of illegal file name characters for Windows which includes
 * the illegal file name characters for Linux and OSX.
 */
static const char illegalFileNameChars[] = "\\/:\"<>|";


43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
int ReadDelimitedText( wxString* aDest, const char* aSource )
{
    std::string utf8;               // utf8 but without escapes and quotes.
    bool        inside = false;
    const char* start = aSource;
    char        cc;

    while( (cc = *aSource++) != 0  )
    {
        if( cc == '"' )
        {
            if( inside )
                break;          // 2nd double quote is end of delimited text

            inside = true;      // first delimiter found, make note, do not copy
        }

        else if( inside )
        {
            if( cc == '\\' )
            {
                cc = *aSource++;
65

66 67 68 69 70 71 72 73 74 75
                if( !cc )
                    break;

                // do no copy the escape byte if it is followed by \ or "
                if( cc != '"' && cc != '\\' )
                    utf8 += '\\';

                utf8 += cc;
            }
            else
76
            {
77
                utf8 += cc;
78
            }
79 80 81 82 83 84 85 86 87
        }
    }

    *aDest = FROM_UTF8( utf8.c_str() );

    return aSource - start;
}


88
int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize )
89 90 91 92
{
    if( aDestSize <= 0 )
        return 0;

93 94 95 96
    bool        inside = false;
    const char* start = aSource;
    char*       limit = aDest + aDestSize - 1;
    char        cc;
97 98 99

    while( (cc = *aSource++) != 0 && aDest < limit )
    {
100
        if( cc == '"' )
101 102 103 104 105 106 107 108 109
        {
            if( inside )
                break;          // 2nd double quote is end of delimited text

            inside = true;      // first delimiter found, make note, do not copy
        }

        else if( inside )
        {
110 111 112
            if( cc == '\\' )
            {
                cc = *aSource++;
113

114 115
                if( !cc )
                    break;
116 117 118 119 120 121 122 123 124

                // do no copy the escape byte if it is followed by \ or "
                if( cc != '"' && cc != '\\' )
                    *aDest++ = '\\';

                if( aDest < limit )
                    *aDest++ = cc;
            }
            else
125
            {
126
                *aDest++ = cc;
127
            }
128 129 130 131 132
        }
    }

    *aDest = 0;

133
    return aSource - start;
134 135 136 137
}


std::string EscapedUTF8( const wxString& aString )
plyatov's avatar
plyatov committed
138
{
139
    std::string utf8 = TO_UTF8( aString );
140

141 142
    std::string ret;

Dick Hollenbeck's avatar
Dick Hollenbeck committed
143
    ret += '"';
144 145

    for( std::string::const_iterator it = utf8.begin();  it!=utf8.end();  ++it )
146
    {
147 148
        // this escaping strategy is designed to be compatible with ReadDelimitedText():
        if( *it == '"' )
149
        {
150 151
            ret += '\\';
            ret += '"';
152
        }
153
        else if( *it == '\\' )
154
        {
155 156
            ret += '\\';    // double it up
            ret += '\\';
157
        }
158
        else
159
        {
160
            ret += *it;
161
        }
162 163
    }

Dick Hollenbeck's avatar
Dick Hollenbeck committed
164
    ret += '"';
165 166

    return ret;
plyatov's avatar
plyatov committed
167 168 169
}


170
char* StrPurge( char* text )
plyatov's avatar
plyatov committed
171
{
172
    static const char whitespace[] = " \t\n\r\f\v";
173

174 175
    if( text )
    {
176
        while( *text && strchr( whitespace, *text ) )
177
            ++text;
178

179
        char* cp = text + strlen( text ) - 1;
180

181 182
        while( cp >= text && strchr( whitespace, *cp ) )
            *cp-- = '\0';
183 184 185
    }

    return text;
plyatov's avatar
plyatov committed
186 187 188
}


189
char* GetLine( FILE* File, char* Line, int* LineNum, int SizeLine )
plyatov's avatar
plyatov committed
190
{
191
    do {
192 193
        if( fgets( Line, SizeLine, File ) == NULL )
            return NULL;
194

195 196
        if( LineNum )
            *LineNum += 1;
197 198

    } while( Line[0] == '#' || Line[0] == '\n' ||  Line[0] == '\r' || Line[0] == 0 );
199 200 201

    strtok( Line, "\n\r" );
    return Line;
plyatov's avatar
plyatov committed
202 203 204
}


205
wxString DateAndTime()
plyatov's avatar
plyatov committed
206
{
207 208 209
    wxDateTime datetime = wxDateTime::Now();

    datetime.SetCountry( wxDateTime::Country_Default );
210
    return datetime.Format( wxDefaultDateTimeFormat, wxDateTime::Local );
plyatov's avatar
plyatov committed
211 212 213
}


214
int StrNumCmp( const wxString& aString1, const wxString& aString2, int aLength, bool aIgnoreCase )
plyatov's avatar
plyatov committed
215
{
216 217 218
    int i;
    int nb1 = 0, nb2 = 0;

219 220 221
    wxString::const_iterator str1 = aString1.begin(), str2 = aString2.begin();

    if( ( str1 == aString1.end() ) || ( str2 == aString2.end() ) )
222 223
        return 0;

224
    for( i = 0; i < aLength; i++ )
225
    {
226
        if( isdigit( *str1 ) && isdigit( *str2 ) ) /* digit found */
227
        {
228 229 230
            nb1 = 0;
            nb2 = 0;

231
            while( isdigit( *str1 ) )
232
            {
233
                nb1 = nb1 * 10 + (int) *str1 - '0';
234
                ++str1;
235 236
            }

237
            while( isdigit( *str2 ) )
238
            {
239
                nb2 = nb2 * 10 + (int) *str2 - '0';
240
                ++str2;
241 242 243 244
            }

            if( nb1 < nb2 )
                return -1;
245

246 247 248 249
            if( nb1 > nb2 )
                return 1;
        }

250
        if( aIgnoreCase )
251
        {
252
            if( toupper( *str1 ) < toupper( *str2 ) )
253
                return -1;
254

255
            if( toupper( *str1 ) > toupper( *str2 ) )
256
                return 1;
257

258
            if( ( *str1 == 0 ) && ( *str2 == 0 ) )
259 260 261 262
                return 0;
        }
        else
        {
263
            if( *str1 < *str2 )
264
                return -1;
265

266
            if( *str1 > *str2 )
267
                return 1;
268

269
            if( ( str1 == aString1.end() ) && ( str2 == aString2.end() ) )
270
                return 0;
271 272
        }

273 274
        ++str1;
        ++str2;
275 276 277
    }

    return 0;
plyatov's avatar
plyatov committed
278 279 280
}


281 282
bool WildCompareString( const wxString& pattern, const wxString& string_to_tst,
                        bool case_sensitive )
plyatov's avatar
plyatov committed
283
{
284 285 286 287 288 289
    const wxChar* cp = NULL, * mp = NULL;
    const wxChar* wild, * string;
    wxString      _pattern, _string_to_tst;

    if( case_sensitive )
    {
290 291
        wild   = pattern.GetData();
        string = string_to_tst.GetData();
292 293 294
    }
    else
    {
295 296 297 298 299 300
        _pattern = pattern;
        _pattern.MakeUpper();
        _string_to_tst = string_to_tst;
        _string_to_tst.MakeUpper();
        wild   = _pattern.GetData();
        string = _string_to_tst.GetData();
301 302
    }

303
    while( ( *string ) && ( *wild != '*' ) )
304
    {
305
        if( ( *wild != *string ) && ( *wild != '?' ) )
306 307
            return false;

308 309 310 311 312 313 314
        wild++; string++;
    }

    while( *string )
    {
        if( *wild == '*' )
        {
315
            if( !*++wild )
316 317 318 319
                return 1;
            mp = wild;
            cp = string + 1;
        }
320
        else if( ( *wild == *string ) || ( *wild == '?' ) )
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
        {
            wild++;
            string++;
        }
        else
        {
            wild   = mp;
            string = cp++;
        }
    }

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

337
    return !*wild;
plyatov's avatar
plyatov committed
338 339 340
}


341 342 343 344 345 346 347 348 349 350 351
int RefDesStringCompare( const wxString& strFWord, const wxString& strSWord )
{
    // The different sections of the first string
    wxString strFWordBeg, strFWordMid, strFWordEnd;

    // The different sections of the second string
    wxString strSWordBeg, strSWordMid, strSWordEnd;

    int isEqual = 0;            // The numerical results of a string compare
    int iReturn = 0;            // The variable that is being returned

352 353
    long lFirstDigit  = 0;      // The converted middle section of the first string
    long lSecondDigit = 0;      // The converted middle section of the second string
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

    // Split the two strings into separate parts
    SplitString( strFWord, &strFWordBeg, &strFWordMid, &strFWordEnd );
    SplitString( strSWord, &strSWordBeg, &strSWordMid, &strSWordEnd );

    // Compare the Beginning section of the strings
    isEqual = strFWordBeg.CmpNoCase( strSWordBeg );

    if( isEqual > 0 )
        iReturn = 1;
    else if( isEqual < 0 )
        iReturn = -1;
    else
    {
        // If the first sections are equal compare their digits
        strFWordMid.ToLong( &lFirstDigit );
        strSWordMid.ToLong( &lSecondDigit );

        if( lFirstDigit > lSecondDigit )
            iReturn = 1;
        else if( lFirstDigit < lSecondDigit )
            iReturn = -1;
        else
        {
            // If the first two sections are equal compare the endings
            isEqual = strFWordEnd.CmpNoCase( strSWordEnd );

            if( isEqual > 0 )
                iReturn = 1;
            else if( isEqual < 0 )
                iReturn = -1;
            else
                iReturn = 0;
        }
    }

    return iReturn;
}


int SplitString( wxString  strToSplit,
                 wxString* strBeginning,
                 wxString* strDigits,
                 wxString* strEnd )
{
    // Clear all the return strings
    strBeginning->Empty();
    strDigits->Empty();
    strEnd->Empty();

    // There no need to do anything if the string is empty
    if( strToSplit.length() == 0 )
        return 0;

    // Starting at the end of the string look for the first digit
    int ii;
410

411 412 413 414 415 416 417 418
    for( ii = (strToSplit.length() - 1); ii >= 0; ii-- )
    {
        if( isdigit( strToSplit[ii] ) )
            break;
    }

    // If there were no digits then just set the single string
    if( ii < 0 )
419
    {
420
        *strBeginning = strToSplit;
421
    }
422 423 424 425 426 427 428
    else
    {
        // Since there is at least one digit this is the trailing string
        *strEnd = strToSplit.substr( ii + 1 );

        // Go to the end of the digits
        int position = ii + 1;
429

430 431 432 433 434 435 436 437 438 439 440
        for( ; ii >= 0; ii-- )
        {
            if( !isdigit( strToSplit[ii] ) )
                break;
        }

        // If all that was left was digits, then just set the digits string
        if( ii < 0 )
            *strDigits = strToSplit.substr( 0, position );

        /* We were only looking for the last set of digits everything else is
441
         * part of the preamble */
442 443 444 445 446 447 448 449 450
        else
        {
            *strDigits    = strToSplit.substr( ii + 1, position - ii - 1 );
            *strBeginning = strToSplit.substr( 0, ii + 1 );
        }
    }

    return 0;
}
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481


wxString GetIllegalFileNameWxChars()
{
    return FROM_UTF8( illegalFileNameChars );
}


bool ReplaceIllegalFileNameChars( std::string* aName )
{
    bool              changed = false;
    std::string       result;

    for( std::string::iterator it = aName->begin();  it != aName->end();  ++it )
    {
        if( strchr( illegalFileNameChars, *it ) )
        {
            StrPrintf( &result, "%%%02x", *it );
            changed = true;
        }
        else
        {
            result += *it;
        }
    }

    if( changed )
        *aName =  result;

    return changed;
}