savecmp.cpp 4.89 KB
Newer Older
1 2 3
/****************/
/* savecmp.cpp  */
/****************/
4

5 6 7 8 9 10 11 12 13 14 15 16
#include <fctsys.h>
#include <wxstruct.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <macros.h>
#include <appl_wxstruct.h>

#include <cvpcb.h>
#include <cvpcb_mainframe.h>

#include <build_version.h>
17

18

19
/* File header. */
20
char EnteteCmpMod[] = { "Cmp-Mod V01" };
21

22
#define titleComponentLibErr _( "Component Library Error" )
23

24

25
int CVPCB_MAINFRAME::SaveComponentList( const wxString& aFullFileName )
26
{
27
    FILE*       dest;
28
    wxFileName  fn( aFullFileName );
29
    wxString    Title = wxGetApp().GetTitle() + wxT( " " ) + GetBuildVersion();
30

31
    fn.SetExt( ComponentFileExtension );
32

33
    dest = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
34

35
    if( dest == NULL )
36
        return 0;
37 38

    fprintf( dest, "%s", EnteteCmpMod );
39
    fprintf( dest, " Created by %s", TO_UTF8( Title ) );
40
    fprintf( dest, " date = %s\n", TO_UTF8( DateAndTime() ) );
41

42
    BOOST_FOREACH( COMPONENT& component, m_components )
43 44
    {
        fprintf( dest, "\nBeginCmp\n" );
45 46 47 48
        fprintf( dest, "TimeStamp = %s;\n", TO_UTF8( component.m_TimeStamp ) );
        fprintf( dest, "Reference = %s;\n", TO_UTF8( component.m_Reference ) );
        fprintf( dest, "ValeurCmp = %s;\n", TO_UTF8( component.m_Value ) );
        fprintf( dest, "IdModule  = %s;\n", TO_UTF8( component.m_Module ) );
49 50 51 52 53 54
        fprintf( dest, "EndCmp\n" );
    }

    fprintf( dest, "\nEndListe\n" );
    fclose( dest );
    return 1;
55 56
}

57

58
bool CVPCB_MAINFRAME::LoadComponentFile( const wxString& aFileName )
59
{
60
    wxString    timestamp, valeur, ilib, namecmp, msg;
61
    bool        read_cmp_data = false, eof = false;
62 63
    char        Line[1024], * ident, * data;
    FILE*       source;
64
    wxFileName  fn = aFileName;
65

66
    fn.SetExt( ComponentFileExtension );
67

68
    source = wxFopen( fn.GetFullPath(), wxT( "rt" ) );
69 70
    if( source == NULL )
    {
71
        msg.Printf( _( "Cannot open CvPcb component file <%s>." ),
72
                    GetChars( fn.GetFullPath() ) );
73
        msg << wxT( "\n" ) << _( "This is normal if you are opening a new netlist file" );
74 75
        wxMessageBox( msg, titleComponentLibErr, wxOK | wxICON_ERROR );
        return false;
76 77
    }

78
    /* Identification of the type of file CmpMod */
79
    if( fgets( Line, 79, source ) == 0 )
80
    {
81
        msg.Printf( _( " <%s> does not appear to be a valid KiCad component library." ),
82
                    GetChars( fn.GetFullPath() ) );
83 84 85 86 87
        wxMessageBox( msg, titleComponentLibErr, wxOK | wxICON_ERROR );
        fclose( source );
        return false;
    }

88 89
    if( strnicmp( Line, EnteteCmpMod, 11 ) != 0 ) /* old file version*/
    {
90 91
        msg.Printf( _( "<%s> is an old version component file." ) );
        wxMessageBox( msg, titleComponentLibErr, wxOK | wxICON_ERROR );
92
        fclose( source );
93
        return false;
94 95 96 97 98 99 100
    }

    while( !eof && fgets( Line, 79, source ) != 0 )
    {
        if( strnicmp( Line, "EndListe", 8 ) == 0 )
            break;

101
        /* Search the beginning of the component description. */
102 103
        if( strnicmp( Line, "BeginCmp", 8 ) != 0 )
            continue;
104

105 106 107 108
        timestamp.Empty();
        valeur.Empty();
        ilib.Empty();
        namecmp.Empty();
109
        read_cmp_data = true;
110 111 112 113 114

        while( !eof && read_cmp_data )
        {
            if( fgets( Line, 1024, source ) == 0 )
            {
115
                eof = true;
116
                break;
117 118 119 120
            }

            if( strnicmp( Line, "EndCmp", 6 ) == 0 )
            {
121
                read_cmp_data = true;
122
                break;
123 124 125 126
            }

            ident = strtok( Line, "=;\n\r" );
            data  = strtok( NULL, ";\n\r" );
127

128 129
            if( strnicmp( ident, "TimeStamp", 9 ) == 0 )
            {
130
                timestamp = FROM_UTF8( data );
131 132
                timestamp.Trim( true );
                timestamp.Trim( false );
133 134 135 136 137
                continue;
            }

            if( strnicmp( ident, "Reference", 9 ) == 0 )
            {
138
                namecmp = FROM_UTF8( data );
139 140
                namecmp.Trim( true );
                namecmp.Trim( false );
141 142 143 144 145
                continue;
            }

            if( strnicmp( ident, "ValeurCmp", 9 ) == 0 )
            {
146
                valeur = FROM_UTF8( data );
147 148
                valeur.Trim( true );
                valeur.Trim( false );
149 150 151 152 153
                continue;
            }

            if( strnicmp( ident, "IdModule", 8 ) == 0 )
            {
154
                ilib = FROM_UTF8( data );
155 156
                ilib.Trim( true );
                ilib.Trim( false );
157 158
                continue;
            }
159
        } /* End reading component description. */
160

161
        /* Search corresponding component and NetList Update its parameters. */
162
        BOOST_FOREACH( COMPONENT& component, m_components )
163
        {
164
            if( namecmp != component.m_Reference )
165
                continue;
166

167
            /* Copy the name of the corresponding module. */
168
            component.m_Module = ilib;
169 170 171 172
        }
    }

    fclose( source );
173
    return true;
174
}