Commit c3924e6f authored by Dick Hollenbeck's avatar Dick Hollenbeck
Browse files

fix bug lp:663929

parent 8b4bc768
Loading
Loading
Loading
Loading
+70 −98
Original line number Diff line number Diff line
@@ -10,26 +10,19 @@
#include "general.h"
#include "protos.h"
#include "class_marker_sch.h"
#include "richio.h"


/* in read_from_file_schematic_items_description.cpp */
SCH_ITEM* ReadTextDescr( FILE* aFile, wxString& aMsgDiag, char* aLine,
                         int aBufsize, int* aLineNum,
                         int aSchematicFileVersion );
SCH_ITEM* ReadTextDescr( LINE_READER* aLine, wxString& aMsgDiag, int aSchematicFileVersion );

extern int ReadSheetDescr( wxWindow* frame, char* Line, FILE* f,
                           wxString& aMsgDiag, int* aLineNum,
                           BASE_SCREEN* Window );
int ReadSheetDescr( wxWindow* frame, LINE_READER* aLine, wxString& aMsgDiag, BASE_SCREEN* Window );

extern bool ReadSchemaDescr( wxWindow* frame, char* Line, FILE* f,
                             wxString& aMsgDiag, int* aLineNum,
                             BASE_SCREEN* Window );
bool ReadSchemaDescr( wxWindow* frame, LINE_READER* aLine, wxString& aMsgDiag, BASE_SCREEN* Window );

extern int ReadPartDescr( wxWindow* frame, char* Line, FILE* f,
                          wxString& aMsgDiag, int* aLineNum,
                          BASE_SCREEN* Window );
int ReadPartDescr( wxWindow* frame, LINE_READER* aLine, wxString& aMsgDiag, BASE_SCREEN* Window );

static void LoadLayers( FILE* f, int* linecnt );
static void LoadLayers( LINE_READER* aLine );


/**
@@ -39,7 +32,6 @@ static void LoadLayers( FILE* f, int* linecnt );
bool WinEDA_SchematicFrame::LoadOneEEFile( SCH_SCREEN* screen,
                                           const wxString& FullFileName )
{
    char            Line[1024], * SLine;
    char            Name1[256],
                    Name2[256];
    int             ii, layer;
@@ -51,10 +43,9 @@ bool WinEDA_SchematicFrame::LoadOneEEFile( SCH_SCREEN* screen,
    SCH_LINE*       SegmentStruct;
    SCH_BUS_ENTRY*  busEntry;
    SCH_NO_CONNECT* NoConnectStruct;
    int             LineCount;
    wxString        MsgDiag;            // Error and log messages

    FILE*           f;
#define line        ((char*)reader)

    if( screen == NULL )
        return FALSE;
@@ -67,7 +58,7 @@ bool WinEDA_SchematicFrame::LoadOneEEFile( SCH_SCREEN* screen,

    // D(printf("LoadOneEEFile:%s\n", CONV_TO_UTF8( FullFileName ) ); )

    LineCount = 1;
    FILE*           f;
    if( ( f = wxFopen( FullFileName, wxT( "rt" ) ) ) == NULL )
    {
        MsgDiag = _( "Failed to open " ) + FullFileName;
@@ -75,21 +66,23 @@ bool WinEDA_SchematicFrame::LoadOneEEFile( SCH_SCREEN* screen,
        return FALSE;
    }

    // reader now owns the open FILE.
    FILE_LINE_READER    reader( f, FullFileName );

    MsgDiag = _( "Loading " ) + screen->m_FileName;
    PrintMsg( MsgDiag );

    if( fgets( Line, sizeof(Line), f ) == NULL
        || strncmp( Line + 9, SCHEMATIC_HEAD_STRING,
    if( !reader.ReadLine()
        || strncmp( line + 9, SCHEMATIC_HEAD_STRING,
                    sizeof(SCHEMATIC_HEAD_STRING) - 1 ) != 0 )
    {
        MsgDiag = FullFileName + _( " is NOT an EESchema file!" );
        DisplayError( this, MsgDiag );
        fclose( f );
        return FALSE;
    }

    // get the file version here. TODO: Support version numbers > 9
    char version = Line[9 + sizeof(SCHEMATIC_HEAD_STRING)];
    char version = line[9 + sizeof(SCHEMATIC_HEAD_STRING)];
    int  ver     = version - '0';
    if( ver > EESCHEMA_VERSION )
    {
@@ -110,50 +103,37 @@ again." );
    }
#endif

    LineCount++;
    if( fgets( Line, sizeof(Line), f ) == NULL || strncmp( Line, "LIBS:", 5 ) != 0 )
    if( !reader.ReadLine() || strncmp( line, "LIBS:", 5 ) != 0 )
    {
        MsgDiag = FullFileName + _( " is NOT an EESchema file!" );
        DisplayError( this, MsgDiag );
        fclose( f );
        return FALSE;
    }

    // Read the rest of a potentially very long line.  fgets() puts a '\n' into
    // the buffer if the end of line was reached.  Read until end of line if
    // necessary.
    if( Line[ strlen( Line )-1 ] != '\n' )
    {
        int c;
        while( !feof( f ) && (c = fgetc( f )) != '\n' )
            ;
    }

    LoadLayers( f, &LineCount );
    LoadLayers( &reader );

    while( !feof( f ) && GetLine( f, Line, &LineCount, sizeof(Line) ) != NULL )
    while( reader.ReadLine() )
    {
        SLine = Line;
        while( (*SLine != ' ' ) && *SLine )
            SLine++;
        char* sline = line;
        while( (*sline != ' ' ) && *sline )
            sline++;

        switch( Line[0] )
        switch( line[0] )
        {
        case '$':           // identification block
            if( Line[1] == 'C' )
                Failed = ReadPartDescr( this, Line, f, MsgDiag, &LineCount, screen );
            if( line[1] == 'C' )
                Failed = ReadPartDescr( this, &reader, MsgDiag, screen );

            else if( Line[1] == 'S' )
                Failed = ReadSheetDescr( this, Line, f, MsgDiag, &LineCount, screen );
            else if( line[1] == 'S' )
                Failed = ReadSheetDescr( this, &reader, MsgDiag, screen );

            else if( Line[1] == 'D' )
                Failed = ReadSchemaDescr( this, Line, f, MsgDiag, &LineCount, screen );
            else if( line[1] == 'D' )
                Failed = ReadSchemaDescr( this, &reader, MsgDiag, screen );

            else if( Line[1] == 'T' ) // text part
            else if( line[1] == 'T' ) // text part
            {
                printf( "**** TEXT PART\n" );
                SCH_ITEM* Struct;
                Struct = ReadTextDescr( f, MsgDiag, Line, sizeof(Line), &LineCount, version );
                SCH_ITEM* Struct = ReadTextDescr( &reader, MsgDiag, version );

                if( Struct )
                {
@@ -168,15 +148,15 @@ again." );
            break;

        case 'L':        // Its a library item.
            Failed = ReadPartDescr( this, Line, f, MsgDiag, &LineCount, screen );
            Failed = ReadPartDescr( this, &reader, MsgDiag, screen );
            break;

        case 'W':        // Its a Segment (WIRE or BUS) item.
            if( sscanf( SLine, "%s %s", Name1, Name2 ) != 2  )
            if( sscanf( sline, "%s %s", Name1, Name2 ) != 2  )
            {
                MsgDiag.Printf( wxT( "EESchema file segment error at line %d, aborted" ),
                                LineCount );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( Line );
                                reader.LineNumber() );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( line );
                Failed = true;
                break;
            }
@@ -190,15 +170,14 @@ again." );

            SegmentStruct = new SCH_LINE( wxPoint( 0, 0 ), layer );

            LineCount++;
            if( fgets( Line, 256 - 1, f ) == NULL
                || sscanf( Line, "%d %d %d %d ", &SegmentStruct->m_Start.x,
            if( !reader.ReadLine()
                || sscanf( line, "%d %d %d %d ", &SegmentStruct->m_Start.x,
                           &SegmentStruct->m_Start.y, &SegmentStruct->m_End.x,
                           &SegmentStruct->m_End.y ) != 4 )
            {
                MsgDiag.Printf( wxT( "EESchema file Segment struct error at line %d, aborted" ),
                                LineCount );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( Line );
                                reader.LineNumber() );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( line );
                Failed = true;
                SAFE_DELETE( SegmentStruct );
                break;
@@ -212,11 +191,11 @@ again." );
            break;

        case 'E':        // Its a WIRE or BUS item.
            if( sscanf( SLine, "%s %s", Name1, Name2 ) != 2  )
            if( sscanf( sline, "%s %s", Name1, Name2 ) != 2  )
            {
                MsgDiag.Printf( wxT( "EESchema file record struct error at line %d, aborted" ),
                                LineCount );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( Line );
                                reader.LineNumber() );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( line );
                Failed = true;
                break;
            }
@@ -224,17 +203,16 @@ again." );
            ii = WIRE_TO_BUS;
            if( Name1[0] == 'B' )
                ii = BUS_TO_BUS;
            busEntry = new SCH_BUS_ENTRY( wxPoint( 0, 0 ), '\\', ii );

            LineCount++;
            busEntry = new SCH_BUS_ENTRY( wxPoint( 0, 0 ), '\\', ii );

            if( fgets( Line, 256 - 1, f ) == NULL
                || sscanf( Line, "%d %d %d %d ", &busEntry->m_Pos.x, &busEntry->m_Pos.y,
            if( !reader.ReadLine()
                || sscanf( line, "%d %d %d %d ", &busEntry->m_Pos.x, &busEntry->m_Pos.y,
                           &busEntry->m_Size.x, &busEntry->m_Size.y ) != 4 )
            {
                MsgDiag.Printf( wxT( "EESchema file Bus Entry struct error at line %d, aborted" ),
                                LineCount );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( Line );
                                reader.LineNumber() );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( line );
                Failed = true;
                SAFE_DELETE( busEntry );
                break;
@@ -250,11 +228,11 @@ again." );
            break;

        case 'P':        // Its a polyline item.
            if( sscanf( SLine, "%s %s %d", Name1, Name2, &ii ) != 3 )
            if( sscanf( sline, "%s %s %d", Name1, Name2, &ii ) != 3 )
            {
                MsgDiag.Printf( wxT( "EESchema file polyline struct error at line %d, aborted" ),
                                LineCount );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( Line );
                                reader.LineNumber() );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( line );
                Failed = true;
                break;
            }
@@ -265,17 +243,17 @@ again." );
                layer = LAYER_BUS;

            PolylineStruct = new SCH_POLYLINE( layer );

            for( unsigned jj = 0; jj < (unsigned)ii; jj++ )
            {
                LineCount++;
                wxPoint point;
                if( fgets( Line, 256 - 1, f ) == NULL
                    || sscanf( Line, "%d %d", &point.x, &point.y ) != 2 )

                if( !reader.ReadLine()
                    || sscanf( line, "%d %d", &point.x, &point.y ) != 2 )
                {
                    MsgDiag.Printf( wxT( "EESchema file polyline struct error \
at line %d, aborted" ),
                                    LineCount );
                    MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( Line );
                    MsgDiag.Printf( wxT( "EESchema file polyline struct error at line %d, aborted" ),
                                    reader.LineNumber() );
                    MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( line );
                    Failed = true;
                    SAFE_DELETE( PolylineStruct );
                    break;
@@ -294,13 +272,12 @@ at line %d, aborted" ),
        case 'C':                       // It is a connection item.
            ConnectionStruct = new SCH_JUNCTION( wxPoint( 0, 0 ) );

            if( sscanf( SLine, "%s %d %d", Name1, &ConnectionStruct->m_Pos.x,
            if( sscanf( sline, "%s %d %d", Name1, &ConnectionStruct->m_Pos.x,
                        &ConnectionStruct->m_Pos.y ) != 3 )
            {
                MsgDiag.Printf( wxT( "EESchema file connection struct error \
at line %d, aborted" ),
                                LineCount );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( Line );
                MsgDiag.Printf( wxT( "EESchema file connection struct error at line %d, aborted" ),
                                reader.LineNumber() );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( line );
                Failed = true;
                SAFE_DELETE( ConnectionStruct );
            }
@@ -312,11 +289,11 @@ at line %d, aborted" ),
            break;

        case 'N':                       // It is a NoConnect item.
            if( sscanf( SLine, "%s %d %d", Name1, &pos.x, &pos.y ) != 3 )
            if( sscanf( sline, "%s %d %d", Name1, &pos.x, &pos.y ) != 3 )
            {
                MsgDiag.Printf( wxT( "EESchema file NoConnect struct error at line %d, aborted" ),
                                LineCount );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( Line );
                                reader.LineNumber() );
                MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( line );
                Failed = true;
            }
            else
@@ -334,9 +311,7 @@ at line %d, aborted" ),

        case 'T':                       // It is a text item.
            {
                SCH_ITEM* Struct;
                Struct = ReadTextDescr( f, MsgDiag, Line, sizeof(Line), &LineCount, version);

                SCH_ITEM* Struct = ReadTextDescr( &reader, MsgDiag, version);
                if( Struct )
                {
                    Struct->SetNext( screen->EEDrawList );
@@ -350,8 +325,8 @@ at line %d, aborted" ),
        default:
            Failed = true;
            MsgDiag.Printf( wxT( "EESchema file undefined object at line %d, aborted" ),
                            LineCount );
            MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( Line );
                            reader.LineNumber() );
            MsgDiag << wxT( "\n" ) << CONV_FROM_UTF8( line );
            break;
        }

@@ -378,8 +353,6 @@ at line %d, aborted" ),
    screen->Show( 0, std::cout );
#endif

    fclose( f );

    TestDanglingEnds( screen->EEDrawList, NULL );

    MsgDiag = _( "Done Loading " ) + screen->m_FileName;
@@ -389,17 +362,16 @@ at line %d, aborted" ),
}


static void LoadLayers( FILE* f, int* linecnt )
static void LoadLayers( LINE_READER* aLine )
{
    int  Number;
    char Line[1024];

    //int Mode,Color,Layer;
    char Name[256];

    GetLine( f, Line, linecnt, sizeof(Line) );       /* read line */
    aLine->ReadLine();

    sscanf( Line, "%s %d %d", Name, &Number, &g_LayerDescr.CurrentLayer );
    sscanf( *aLine, "%s %d %d", Name, &Number, &g_LayerDescr.CurrentLayer );
    if( strcmp( Name, "EELAYER" ) !=0 )
    {
        /* error : init par default */
@@ -413,9 +385,9 @@ static void LoadLayers( FILE* f, int* linecnt )

    g_LayerDescr.NumberOfLayers = Number;

    while( GetLine( f, Line, linecnt, sizeof(Line) ) )
    while( aLine->ReadLine() )
    {
        if( strnicmp( Line, "EELAYER END", 11 ) == 0 )
        if( strnicmp( *aLine, "EELAYER END", 11 ) == 0 )
            break;
    }
}