summaryrefslogtreecommitdiff
path: root/eeschema/class_library.cpp
blob: 93a5331e9829370e271a110196ee525d651a1d82 (plain)
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
343
344
345
346
347
348
349
350
351
352
353
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2004-2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 2008-2015 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 2004-2015 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
 */

/**
 * @file class_library.cpp
 */

#include <fctsys.h>
#include <kiface_i.h>
#include <gr_basic.h>
#include <macros.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <eda_doc.h>
#include <wxstruct.h>
#include <richio.h>
#include <config_params.h>
#include <wildcards_and_files_ext.h>
#include <project_rescue.h>

#include <general.h>
#include <class_library.h>

#include <boost/foreach.hpp>

#include <wx/tokenzr.h>
#include <wx/regex.h>

#define duplicate_name_msg  \
    _(  "Library '%s' has duplicate entry name '%s'.\n" \
        "This may cause some unexpected behavior when loading components into a schematic." )


PART_LIB::PART_LIB( int aType, const wxString& aFileName ) :
    // start @ != 0 so each additional library added
    // is immediately detectable, zero would not be.
    m_mod_hash( PART_LIBS::s_modify_generation )
{
    type = aType;
    isModified = false;
    timeStamp = 0;
    isCache = false;
    timeStamp = wxDateTime::Now();
    versionMajor = 0;       // Will be updated after reading the lib file
    versionMinor = 0;       // Will be updated after reading the lib file

    fileName = aFileName;

    if( !fileName.IsOk() )
        fileName = wxT( "unnamed.lib" );
}


PART_LIB::~PART_LIB()
{
    // When the library is destroyed, all of the alias objects on the heap should be deleted.
    for( LIB_ALIAS_MAP::iterator it = m_amap.begin();  it != m_amap.end();  ++it )
    {
        wxLogTrace( traceSchLibMem, wxT( "Removing alias %s from library %s." ),
                    GetChars( it->second->GetName() ), GetChars( GetLogicalName() ) );
        LIB_PART* part = it->second->GetPart();
        LIB_ALIAS* alias = it->second;
        delete alias;

        // When the last alias of a part is destroyed, the part is no longer required and it
        // too is destroyed.
        if( part && part->GetAliasCount() == 0 )
            delete part;
    }

    m_amap.clear();
}


void PART_LIB::GetEntryNames( wxArrayString& aNames, bool aSort, bool aMakeUpperCase )
{
    for( LIB_ALIAS_MAP::iterator it = m_amap.begin();  it!=m_amap.end();  it++ )
    {
        if( aMakeUpperCase )
        {
            wxString tmp = (*it).first;
            tmp.MakeUpper();
            aNames.Add( tmp );
        }
        else
        {
            aNames.Add( (*it).first );
        }
    }

    if( aSort )
        aNames.Sort();
}


void PART_LIB::GetEntryTypePowerNames( wxArrayString& aNames, bool aSort, bool aMakeUpperCase )
{
    for( LIB_ALIAS_MAP::iterator it = m_amap.begin();  it!=m_amap.end();  it++ )
    {
        LIB_ALIAS* alias = it->second;
        LIB_PART* root = alias->GetPart();

        if( !root || !root->IsPower() )
            continue;

        if( aMakeUpperCase )
        {
            wxString tmp = (*it).first;
            tmp.MakeUpper();
            aNames.Add( tmp );
        }
        else
        {
            aNames.Add( (*it).first );
        }
    }

    if( aSort )
        aNames.Sort();
}


/**
 * Function sortFunction
 * simple function used as comparator to sort a std::vector<wxArrayString>&.
 *
 * @param aItem1 is the first comparison parameter.
 * @param aItem2 is the second.
 * @return bool - which item should be put first in the sorted list.
 */
bool sortFunction( wxArrayString aItem1, wxArrayString aItem2 )
{
    return( aItem1.Item( 0 ) < aItem2.Item( 0 ) );
}


void PART_LIB::SearchEntryNames( std::vector<wxArrayString>& aNames,
                                    const wxString& aNameSearch,
                                    const wxString& aKeySearch,
                                    bool aSort )
{
    for( LIB_ALIAS_MAP::iterator it = m_amap.begin();  it != m_amap.end();  ++it )
    {
        if( !!aKeySearch && KeyWordOk( aKeySearch, it->second->GetKeyWords() ) )
        {
            wxArrayString item;

            item.Add( it->first );
            item.Add( GetLogicalName() );
            aNames.push_back( item );
        }

        if( !aNameSearch.IsEmpty() &&
                WildCompareString( aNameSearch, it->second->GetName(), false ) )
        {
            wxArrayString item;

            item.Add( it->first );
            item.Add( GetLogicalName() );
            aNames.push_back( item );
        }
    }

    if( aSort )
        std::sort( aNames.begin(), aNames.end(), sortFunction );
}


void PART_LIB::SearchEntryNames( wxArrayString& aNames, const wxRegEx& aRe, bool aSort )
{
    if( !aRe.IsValid() )
        return;

    LIB_ALIAS_MAP::iterator it;

    for( it = m_amap.begin();  it!=m_amap.end();  it++ )
    {
        if( aRe.Matches( it->second->GetKeyWords() ) )
            aNames.Add( it->first );
    }

    if( aSort )
        aNames.Sort();
}


bool PART_LIB::Conflicts( LIB_PART* aPart )
{
    wxCHECK_MSG( aPart != NULL, false,
                 wxT( "Cannot test NULL component for conflicts in library " ) + GetName() );

    for( size_t i=0;  i<aPart->m_aliases.size();  i++ )
    {
        LIB_ALIAS_MAP::iterator it = m_amap.find( aPart->m_aliases[i]->GetName() );

        if( it != m_amap.end() )
            return true;
    }

    return false;
}


LIB_ALIAS* PART_LIB::FindEntry( const wxString& aName )
{
    LIB_ALIAS_MAP::iterator it = m_amap.find( aName );

    if( it != m_amap.end() )
        return it->second;

    return NULL;
}


LIB_ALIAS* PART_LIB::GetFirstEntry()
{
    if( m_amap.size() )
        return m_amap.begin()->second;
    else
        return NULL;
}


LIB_PART* PART_LIB::FindPart( const wxString& aName )
{
#if 0 && defined(DEBUG)
    if( !aName.Cmp( wxT( "TI_STELLARIS_BOOSTERPACK" ) ) )
    {
        int breakhere = 1;
        (void) breakhere;
    }
#endif

    if( LIB_ALIAS* alias = FindEntry( aName ) )
    {
        return alias->GetPart();
    }

    return NULL;
}


bool PART_LIB::HasPowerParts()
{
    // return true if at least one power part is found in lib
    for( LIB_ALIAS_MAP::iterator it = m_amap.begin();  it!=m_amap.end();  it++ )
    {
        LIB_ALIAS* alias = it->second;
        LIB_PART* root = alias->GetPart();

        if( root && root->IsPower() )
            return true;
    }

    return false;
}


bool PART_LIB::AddAlias( LIB_ALIAS* aAlias )
{
    wxASSERT( aAlias );

#if defined(DEBUG) && 0
    if( !aAlias->GetName().Cmp( wxT( "TI_STELLARIS_BOOSTERPACK" ) ) )
    {
        int breakhere = 1;
        (void) breakhere;
    }
#endif

    LIB_ALIAS_MAP::iterator it = m_amap.find( aAlias->GetName() );

    if( it != m_amap.end() )
    {
        wxString msg;

        msg.Printf( _( "Cannot add duplicate alias '%s' to library '%s'." ),
                    GetChars( aAlias->GetName() ),
                    GetChars( fileName.GetName() ) );
        return false;
    }

    wxString name = aAlias->GetName();

    m_amap[ name ] = aAlias;
    isModified = true;
    ++m_mod_hash;

    return true;
}


bool PART_LIB::AddPart( LIB_PART* aPart )
{
    // Conflict detection: See if already existing aliases exist,
    // and if yes, ask user for continue or abort
    // Special case: if the library is the library cache of the project,
    // old aliases are always removed to avoid conflict,
    //      and user is not prompted )
    if( Conflicts( aPart ) && !IsCache() )
    {
        wxFAIL_MSG( wxT( "Cannot add component <" ) + aPart->GetName() +
                    wxT( "> to library <" ) + GetName() + wxT( "> due to name conflict." ) );
        return false;
    }

    // add a clone, not the caller's copy
    LIB_PART* my_part = new LIB_PART( *aPart, this );

    for( size_t i = 0; i < my_part->m_aliases.size(); i++ )
    {
        wxString aliasname = my_part->m_aliases[i]->GetName();

        if( LIB_ALIAS* alias = FindAlias( aliasname ) )
            RemoveEntry( alias );

        m_amap[ aliasname ] = my_part->m_aliases[i];
    }

    isModified = true;
    ++m_mod_hash;

    return true;
}


LIB_ALIAS* PART_LIB::RemoveEntry( LIB_ALIAS* aEntry )
{
    wxCHECK_MSG( aEntry != NULL, NULL, wxT( "NULL pointer cannot be removed from library." ) );

    LIB_ALIAS_MAP::iterator it = m_amap.find( aEntry->GetName() );

    if( it == m_amap.end() )
        return NULL;

    // If the entry pointer doesn't match the name it is mapped to in the library, we
    // have done something terribly wrong.
    wxCHECK_MSG( *it->second == aEntry, NULL,
                 wxT( "Pointer mismatch while attempting to remove entry <" ) +
                 aEntry->GetName() + wxT( "> from library <" ) + GetName() + wxT( ">." ) );

    LIB_ALIAS*  alias = aEntry;
    LIB_PART*   part = alias->GetPart();

    alias = part->RemoveAlias( alias );

    if( !alias )
    {
        delete part;

        if( m_amap.size() > 1 )
        {
            LIB_ALIAS_MAP::iterator next = it;
            next++;

            if( next == m_amap.end() )
                next = m_amap.begin();

            alias = next->second;
        }
    }

    m_amap.erase( it );
    isModified = true;
    ++m_mod_hash;
    return alias;
}


LIB_PART* PART_LIB::ReplacePart( LIB_PART* aOldPart, LIB_PART* aNewPart )
{
    wxASSERT( aOldPart != NULL );
    wxASSERT( aNewPart != NULL );

    /* Remove the old root component.  The component will automatically be deleted
     * when all it's aliases are deleted.  Do not place any code that accesses
     * aOldPart inside this loop that gets evaluated after the last alias is
     * removed in RemoveEntry().  Failure to heed this warning will result in a
     * segfault.
     */
    size_t i = aOldPart->m_aliases.size();

    while( i > 0 )
    {
        i -= 1;
        RemoveEntry( aOldPart->m_aliases[ i ] );
    }

    LIB_PART* my_part = new LIB_PART( *aNewPart, this );

    // Add new aliases to library alias map.
    for( i = 0; i < my_part->m_aliases.size(); i++ )
    {
        wxString aname = my_part->m_aliases[ i ]->GetName();
        m_amap[ aname ] = my_part->m_aliases[ i ];
    }

    isModified = true;
    ++m_mod_hash;
    return my_part;
}


LIB_ALIAS* PART_LIB::GetNextEntry( const wxString& aName )
{
    if( m_amap.empty() )
        return NULL;

    LIB_ALIAS_MAP::iterator it = m_amap.find( aName );

    it++;

    if( it == m_amap.end() )
        it = m_amap.begin();

    return it->second;
}


LIB_ALIAS* PART_LIB::GetPreviousEntry( const wxString& aName )
{
    if( m_amap.empty() )
        return NULL;

    LIB_ALIAS_MAP::iterator it = m_amap.find( aName );

    if( it == m_amap.begin() )
        it = m_amap.end();

    it--;

    return it->second;
}


bool PART_LIB::Load( wxString& aErrorMsg )
{
    FILE*          file;
    char*          line;
    wxString       msg;

    if( fileName.GetFullPath().IsEmpty() )
    {
        aErrorMsg = _( "The component library file name is not set." );
        return false;
    }

    file = wxFopen( fileName.GetFullPath(), wxT( "rt" ) );

    if( file == NULL )
    {
        aErrorMsg = _( "The file could not be opened." );
        return false;
    }

    FILE_LINE_READER reader( file, fileName.GetFullPath() );

    if( !reader.ReadLine() )
    {
        aErrorMsg = _( "The file is empty!" );
        return false;
    }

    // There is no header if this is a symbol library.
    if( type == LIBRARY_TYPE_EESCHEMA )
    {
        line = reader.Line();

        header = FROM_UTF8( line );

        wxStringTokenizer tkn( header );

        /*
         * The file header (first line) in library versions 2.0 and lower
         * apparently started with EESchema-LIB.  Sometime after 2.0, it
         * was changed to EESchema-LIBRARY.  Therefore, the test for
         * EESchema-LIB will work in both cases.  Don't change this unless
         * backwards compatibility is no longer required.
         */
        if( !tkn.HasMoreTokens()
            || !tkn.GetNextToken().Upper().StartsWith(wxT( "EESCHEMA-LIB" ) ) )
        {
            aErrorMsg = _( "The file is NOT an Eeschema library!" );
            return false;
        }

        if( !tkn.HasMoreTokens() )
        {
            aErrorMsg = _( "The file header is missing version and time stamp information." );
            return false;
        }

        if( tkn.GetNextToken() != wxT( "Version" ) || !tkn.HasMoreTokens() )
        {
            aErrorMsg = wxT( "The file header version information is invalid." );
            return false;
        }

        long major, minor;
        wxStringTokenizer vers( tkn.GetNextToken(), wxT( "." ) );

        if( !vers.HasMoreTokens() || !vers.GetNextToken().ToLong( &major )
            || major < 1L || !vers.HasMoreTokens()
            || !vers.GetNextToken().ToLong( & minor ) || minor < 0L
            || minor > 99 )
        {
#if 0       // Note for developers:
            // Not sure this warning is very useful: old designs *must* be always loadable
            wxLogWarning( wxT(
                "The component library '%s' header version "
                "number is invalid.\n\nIn future versions of Eeschema this library may not "
                "load correctly.  To resolve this problem open the library in the library "
                "editor and save it.  If this library is the project cache library, save "
                "the current schematic." ),
                GetChars( GetName() ) );
#endif
        }
        else
        {
            versionMajor = (int) major;
            versionMinor = (int) minor;
        }
    }

    while( reader.ReadLine() )
    {
        line = reader.Line();

        if( type == LIBRARY_TYPE_EESCHEMA && strnicmp( line, "$HEADER", 7 ) == 0 )
        {
            if( !LoadHeader( reader ) )
            {
                aErrorMsg = _( "An error occurred attempting to read the header." );
                return false;
            }

            continue;
        }

        if( strnicmp( line, "DEF", 3 ) == 0 )
        {
            // Read one DEF/ENDDEF part entry from library:
            LIB_PART* part = new LIB_PART( wxEmptyString, this );

            if( part->Load( reader, msg ) )
            {
                // Check for duplicate entry names and warn the user about
                // the potential conflict.
                if( FindEntry( part->GetName() ) != NULL )
                {
                    wxString msg = duplicate_name_msg;

                    wxLogWarning( msg,
                                  GetChars( fileName.GetName() ),
                                  GetChars( part->GetName() ) );
                }

                LoadAliases( part );
            }
            else
            {
                wxLogWarning( _( "Library '%s' component load error %s." ),
                              GetChars( fileName.GetName() ),
                              GetChars( msg ) );
                msg.Clear();
                delete part;
            }
        }
    }

    ++m_mod_hash;

    return true;
}


void PART_LIB::LoadAliases( LIB_PART* aPart )
{
    wxCHECK_RET( aPart, wxT( "Cannot load aliases of NULL part.  Bad programmer!" ) );

    for( size_t i = 0; i < aPart->m_aliases.size(); i++ )
    {
        if( FindEntry( aPart->m_aliases[i]->GetName() ) != NULL )
        {
            wxString msg = duplicate_name_msg;

            wxLogError( msg,
                        GetChars( fileName.GetName() ),
                        GetChars( aPart->m_aliases[i]->GetName() ) );
        }

        wxString aname = aPart->m_aliases[i]->GetName();
        m_amap[ aname ] = aPart->m_aliases[i];
    }
}


bool PART_LIB::LoadHeader( LINE_READER& aLineReader )
{
    char* line, * text, * data;

    while( aLineReader.ReadLine() )
    {
        line = (char*) aLineReader;

        text = strtok( line, " \t\r\n" );
        data = strtok( NULL, " \t\r\n" );

        if( stricmp( text, "TimeStamp" ) == 0 )
            timeStamp = atol( data );

        if( stricmp( text, "$ENDHEADER" ) == 0 )
            return true;
    }

    return false;
}


bool PART_LIB::LoadDocs( wxString& aErrorMsg )
{
    int        lineNumber = 0;
    char       line[8000], * name, * text;
    LIB_ALIAS* entry;
    FILE*      file;
    wxFileName fn = fileName;

    fn.SetExt( DOC_EXT );

    file = wxFopen( fn.GetFullPath(), wxT( "rt" ) );

    if( file == NULL )
    {
        aErrorMsg.Printf( _( "Could not open component document library file '%s'." ),
                          GetChars( fn.GetFullPath() ) );
        return false;
    }

    if( GetLine( file, line, &lineNumber, sizeof(line) ) == NULL )
    {
        aErrorMsg.Printf( _( "Part document library file '%s' is empty." ),
                          GetChars( fn.GetFullPath() ) );
        fclose( file );
        return false;
    }

    if( strnicmp( line, DOCFILE_IDENT, 10 ) != 0 )
    {
        aErrorMsg.Printf( _( "File '%s' is not a valid component library document file." ),
                          GetChars( fn.GetFullPath() ) );
        fclose( file );
        return false;
    }

    while( GetLine( file, line, &lineNumber, sizeof(line) ) )
    {
        if( strncmp( line, "$CMP", 4 ) != 0 )
        {
            aErrorMsg.Printf( wxT( "$CMP command expected in line %d, aborted." ), lineNumber );
            fclose( file );
            return false;
        }

        // Read one $CMP/$ENDCMP part entry from library:
        name = strtok( line + 5, "\n\r" );

        wxString cmpname = FROM_UTF8( name );

        entry = FindEntry( cmpname );

        while( GetLine( file, line, &lineNumber, sizeof(line) ) )
        {
            if( strncmp( line, "$ENDCMP", 7 ) == 0 )
                break;

            text = strtok( line + 2, "\n\r" );

            if( entry )
            {
                switch( line[0] )
                {
                case 'D':
                    entry->SetDescription( FROM_UTF8( text ) );
                    break;

                case 'K':
                    entry->SetKeyWords( FROM_UTF8( text ) );
                    break;

                case 'F':
                    entry->SetDocFileName( FROM_UTF8( text ) );
                    break;
                }
            }
        }
    }

    fclose( file );
    return true;
}


bool PART_LIB::Save( OUTPUTFORMATTER& aFormatter )
{
    if( isModified )
    {
        timeStamp = GetNewTimeStamp();
        isModified = false;
    }

    bool success = true;

    try
    {
        SaveHeader( aFormatter );

        for( LIB_ALIAS_MAP::iterator it=m_amap.begin();  it!=m_amap.end();  it++ )
        {
            if( !it->second->IsRoot() )
                continue;

            it->second->GetPart()->Save( aFormatter );
        }

        aFormatter.Print( 0, "#\n#End Library\n" );
    }
    catch( const IO_ERROR& ioe )
    {
        success = false;
    }

    return success;
}


bool PART_LIB::SaveDocs( OUTPUTFORMATTER& aFormatter )
{
    bool success = true;

    try
    {
        aFormatter.Print( 0, "%s\n", DOCFILE_IDENT );

        for( LIB_ALIAS_MAP::iterator it=m_amap.begin();  it!=m_amap.end();  it++ )
        {
            if( !it->second->SaveDoc( aFormatter ) )
                success = false;
        }

        aFormatter.Print( 0, "#\n#End Doc Library\n" );
    }
    catch( const IO_ERROR& ioe )
    {
        success = false;
    }

    return success;
}


bool PART_LIB::SaveHeader( OUTPUTFORMATTER& aFormatter )
{
    aFormatter.Print( 0, "%s %d.%d\n", LIBFILE_IDENT,
                      LIB_VERSION_MAJOR, LIB_VERSION_MINOR );

    aFormatter.Print( 0, "#encoding utf-8\n");

#if 0
    aFormatter.Print( 0, "$HEADER\n" );
    aFormatter.Print( 0, "TimeStamp %8.8lX\n", m_TimeStamp );
    aFormatter.Print( 0, "Parts %d\n", m_amap.size() );
    aFormatter.Print( 0, "$ENDHEADER\n" ) != 1 );
#endif

    return true;
}


PART_LIB* PART_LIB::LoadLibrary( const wxString& aFileName ) throw( IO_ERROR, boost::bad_pointer )
{
    std::auto_ptr<PART_LIB> lib( new PART_LIB( LIBRARY_TYPE_EESCHEMA, aFileName ) );

    wxBusyCursor ShowWait;

    wxString errorMsg;

    if( !lib->Load( errorMsg ) )
        THROW_IO_ERROR( errorMsg );

    if( USE_OLD_DOC_FILE_FORMAT( lib->versionMajor, lib->versionMinor ) )
    {
#if 1
        // not fatal if error here.
        lib->LoadDocs( errorMsg );
#else
        if( !lib->LoadDocs( errorMsg ) )
            THROW_IO_ERROR( errorMsg );
#endif
    }

    PART_LIB* ret = lib.release();

    return ret;
}


PART_LIB* PART_LIBS::AddLibrary( const wxString& aFileName ) throw( IO_ERROR, boost::bad_pointer )
{
    PART_LIB* lib;

#if 1
    wxFileName fn = aFileName;
    // Don't reload the library if it is already loaded.
    lib = FindLibrary( fn.GetName() );
    if( lib )
        return lib;
#endif

    lib = PART_LIB::LoadLibrary( aFileName );

    push_back( lib );

    return lib;
}


PART_LIB* PART_LIBS::AddLibrary( const wxString& aFileName, PART_LIBS::iterator& aIterator )
    throw( IO_ERROR, boost::bad_pointer )
{
#if 1
    // Don't reload the library if it is already loaded.
    wxFileName fn( aFileName );
    PART_LIB* lib = FindLibrary( fn.GetName() );

    if( lib )
        return lib;
#endif

    lib = PART_LIB::LoadLibrary( aFileName );

    if( aIterator >= begin() && aIterator < end() )
        insert( aIterator, lib );
    else
        push_back( lib );

    return lib;
}


void PART_LIBS::RemoveLibrary( const wxString& aName )
{
    if( aName.IsEmpty() )
        return;

    for( PART_LIBS::iterator it = begin(); it < end();  ++it )
    {
        if( it->GetName().CmpNoCase( aName ) == 0 )
        {
            erase( it );
            return;
        }
    }
}


PART_LIB* PART_LIBS::FindLibrary( const wxString& aName )
{
    for( PART_LIBS::iterator it = begin();  it!=end();  ++it )
    {
        if( it->GetName() == aName )
            return &*it;
    }

    return NULL;
}


wxArrayString PART_LIBS::GetLibraryNames( bool aSorted )
{
    wxArrayString cacheNames;
    wxArrayString names;

    BOOST_FOREACH( PART_LIB& lib, *this )
    {
        if( lib.IsCache() && aSorted )
            cacheNames.Add( lib.GetName() );
        else
            names.Add( lib.GetName() );
    }

    // Even sorted, the cache library is always at the end of the list.
    if( aSorted )
        names.Sort();

    for( unsigned int i = 0; i<cacheNames.Count(); i++ )
        names.Add( cacheNames.Item( i ) );

    return names;
}


LIB_PART* PART_LIBS::FindLibPart( const wxString& aPartName, const wxString& aLibraryName )
{
    LIB_PART* part = NULL;

    BOOST_FOREACH( PART_LIB& lib, *this )
    {
        if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
            continue;

        part = lib.FindPart( aPartName );

        if( part )
            break;
    }

    return part;
}


LIB_ALIAS* PART_LIBS::FindLibraryEntry( const wxString& aEntryName, const wxString& aLibraryName )
{
    LIB_ALIAS* entry = NULL;

    BOOST_FOREACH( PART_LIB& lib, *this )
    {
        if( !!aLibraryName && lib.GetName() != aLibraryName )
            continue;

        entry = lib.FindEntry( aEntryName );

        if( entry )
            break;
    }

    return entry;
}

void PART_LIBS::FindLibraryEntries( const wxString& aEntryName, std::vector<LIB_ALIAS*>& aEntries )
{
    BOOST_FOREACH( PART_LIB& lib, *this )
    {
        LIB_ALIAS* entry = lib.FindEntry( aEntryName );

        if( entry )
            aEntries.push_back( entry );
    }
}

/* searches all libraries in the list for an entry, using a case insensitive comparison.
 * Used to find an entry, when the normal (case sensitive) search fails.
  */
void PART_LIBS::FindLibraryNearEntries( std::vector<LIB_ALIAS*>& aCandidates,
                                        const wxString& aEntryName,
                                        const wxString& aLibraryName )
{
    BOOST_FOREACH( PART_LIB& lib, *this )
    {
        if( !!aLibraryName && lib.GetName() != aLibraryName )
            continue;

        LIB_ALIAS* entry = lib.GetFirstEntry();

        if( ! entry )
            continue;

        wxString first_entry_name = entry->GetName();
        wxString entry_name = first_entry_name;

        for( ;; )
        {
            if( entry_name.CmpNoCase( aEntryName ) == 0 )
                aCandidates.push_back( entry );

            entry = lib.GetNextEntry( entry_name );
            entry_name = entry->GetName();

            if( first_entry_name == entry_name )
                break;
        }
    }
}


int PART_LIBS::s_modify_generation = 1;     // starts at 1 and goes up


int PART_LIBS::GetModifyHash()
{
    int hash = 0;

    for( PART_LIBS::const_iterator it = begin();  it != end();  ++it )
    {
        hash += it->m_mod_hash;
    }

    return hash;
}


/*
void PART_LIBS::RemoveCacheLibrary()
{
    for( PART_LIBS::iterator it = begin(); it < end();  ++it )
    {
        if( it->IsCache() )
            erase( it-- );
    }
}
*/


void PART_LIBS::LibNamesAndPaths( PROJECT* aProject, bool doSave,
                                  wxString* aPaths, wxArrayString* aNames )
    throw( IO_ERROR, boost::bad_pointer )
{
    wxString pro = aProject->GetProjectFullName();

    PARAM_CFG_ARRAY ca;

    if( aPaths )
        ca.push_back( new PARAM_CFG_FILENAME( wxT( "LibDir" ), aPaths ) );

    if( aNames )
        ca.push_back( new PARAM_CFG_LIBNAME_LIST( wxT( "LibName" ),  aNames, GROUP_SCH_LIBS ) );

    if( doSave )
    {
        aProject->ConfigSave( Kiface().KifaceSearch(), GROUP_SCH, ca );

        /*
        {
            wxString msg = wxString::Format( _(
                "Unable save project's '%s' file" ),
                GetChars( pro )
                );
            THROW_IO_ERROR( msg );
        }
        */
    }
    else
    {
        if( !aProject->ConfigLoad( Kiface().KifaceSearch(), GROUP_SCH, ca ) )
        {
            wxString msg = wxString::Format( _(
                "Unable to load project's '%s' file" ),
                GetChars( pro )
                );
            THROW_IO_ERROR( msg );
        }
    }
}


const wxString PART_LIBS::CacheName( const wxString& aFullProjectFilename )
{
    /* until apr 2009 the project cache lib was named: <root_name>.cache.lib,
     * and after: <root_name>-cache.lib.  So if the <name>-cache.lib is not found,
     * the old file will be renamed and returned.
     */
    wxFileName  new_name = aFullProjectFilename;

    new_name.SetName( new_name.GetName() + wxT( "-cache" ) );
    new_name.SetExt( SchematicLibraryFileExtension );

    if( new_name.FileExists() )
        return new_name.GetFullPath();
    else
    {
        wxFileName old_name = aFullProjectFilename;
        old_name.SetExt( wxT( "cache.lib" ) );

        if( old_name.FileExists() )
        {
            wxRenameFile( old_name.GetFullPath(), new_name.GetFullPath() );
            return new_name.GetFullPath();
        }
    }
    return wxEmptyString;
}


void PART_LIBS::LoadAllLibraries( PROJECT* aProject ) throw( IO_ERROR, boost::bad_pointer )
{
    wxFileName      fn;
    wxString        filename;
    wxString        libs_not_found;
    SEARCH_STACK*   lib_search = aProject->SchSearchS();

#if defined(DEBUG) && 1
    lib_search->Show( __func__ );
#endif

    wxArrayString   lib_names;

    LibNamesAndPaths( aProject, false, NULL, &lib_names );

    // If the list is empty, force loading the standard power symbol library.
    if( !lib_names.GetCount() )
        lib_names.Add( wxT( "power" ) );

    wxASSERT( !size() );    // expect to load into "this" empty container.

    for( unsigned i = 0; i < lib_names.GetCount();  ++i )
    {
        fn.Clear();
        fn.SetName( lib_names[i] );
        fn.SetExt( SchematicLibraryFileExtension );

        // Skip if the file name is not valid..
        if( !fn.IsOk() )
            continue;

        if( !fn.FileExists() )
        {
            filename = lib_search->FindValidPath( fn.GetFullPath() );

            if( !filename )
            {
                libs_not_found += fn.GetName();
                libs_not_found += wxT( '\n' );
                continue;
            }
        }
        else
        {
            filename = fn.GetFullPath();
        }

        try
        {
            AddLibrary( filename );
        }
        catch( const IO_ERROR& ioe )
        {
            wxString msg = wxString::Format( _(
                    "Part library '%s' failed to load. Error:\n"
                    "%s" ),
                    GetChars( filename ),
                    GetChars( ioe.errorText )
                    );

            THROW_IO_ERROR( msg );
        }
    }

    // add the special cache library.
    wxString cache_name = CacheName( aProject->GetProjectFullName() );
    PART_LIB* cache_lib;

    if( !!cache_name )
    {
        try
        {
            cache_lib = AddLibrary( cache_name );
            if( cache_lib )
                cache_lib->SetCache();
        }
        catch( const IO_ERROR& ioe )
        {
            wxString msg = wxString::Format( _(
                    "Part library '%s' failed to load.\nError: %s" ),
                    GetChars( cache_name ),
                    GetChars( ioe.errorText )
                    );

            THROW_IO_ERROR( msg );
        }
    }

    // Print the libraries not found
    if( !!libs_not_found )
    {
        // Use a different exception type so catch()er can route to proper use
        // of the HTML_MESSAGE_BOX.
        THROW_PARSE_ERROR( wxEmptyString, UTF8( __func__ ),
            UTF8( libs_not_found ), 0, 0 );
    }

#if defined(DEBUG) && 1
    printf( "%s: lib_names:\n", __func__ );

    for( PART_LIBS::const_iterator it = begin(); it < end(); ++it )
        printf( " %s\n", TO_UTF8( it->GetName() ) );
#endif
}