2014-07-30 09:01:25 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2015-03-28 11:33:56 +00:00
|
|
|
* Copyright (C) 2014-2015 Mario Luzeiro <mrluzeiro@gmail.com>
|
2015-01-18 11:49:32 +00:00
|
|
|
* Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
|
2014-07-30 09:01:25 +00:00
|
|
|
*
|
|
|
|
* 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 vrml_aux.cpp
|
2015-03-28 11:33:56 +00:00
|
|
|
* @brief implements auxiliar functions to parse VRML files
|
2014-07-30 09:01:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vrml_aux.h"
|
|
|
|
|
2015-01-18 11:49:32 +00:00
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
bool GetString( FILE* File, char* aDstString, size_t maxDstLen )
|
|
|
|
{
|
|
|
|
|
|
|
|
if( (!aDstString) || (maxDstLen == 0) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int c;
|
2015-04-02 11:18:19 +00:00
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
while( ( c = fgetc( File ) ) != EOF )
|
|
|
|
{
|
|
|
|
if( c == '\"' )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( c != '\"' )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while( (( c = fgetc( File ) ) != EOF) && (maxDstLen > 0) )
|
|
|
|
{
|
|
|
|
if( c == '\"' )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
maxDstLen--;
|
|
|
|
|
|
|
|
*aDstString = c;
|
|
|
|
aDstString++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*aDstString = 0;
|
|
|
|
|
|
|
|
if( c == '\"' )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-18 11:49:32 +00:00
|
|
|
static int SkipGetChar ( FILE* File );
|
|
|
|
|
|
|
|
|
|
|
|
static int SkipGetChar( FILE* File )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-01-18 11:49:32 +00:00
|
|
|
int c;
|
2014-08-16 18:01:00 +00:00
|
|
|
bool re_parse;
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
if( ( c = fgetc( File ) ) == EOF )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "EOF\n" ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "c %c 0x%02X\n", c, c ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
re_parse = false;
|
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
if( (c == ' ') || (c == '\t') || (c == '{') || (c == '[') )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "Skipping space \\t or { or [\n" ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
do
|
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
if( ( c = fgetc( File ) ) == EOF )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "EOF\n" ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
|
|
|
return EOF;
|
|
|
|
}
|
2014-08-16 18:01:00 +00:00
|
|
|
} while( (c == ' ') || (c == '\t') || (c == '{') || (c == '[') );
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
if( (c == '#') || (c == '\n') || (c == '\r') || (c == 0) || (c == ',') )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
if( c == '#' )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "Skipping # \\n or \\r or 0, 0x%02X\n", c ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
do
|
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
if( ( c = fgetc( File ) ) == EOF )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "EOF\n" ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
return EOF;
|
|
|
|
}
|
2014-08-16 18:01:00 +00:00
|
|
|
} while( (c != '\n') && (c != '\r') && (c != 0) && (c != ',') );
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
if( ( c = fgetc( File ) ) == EOF )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "EOF\n" ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
re_parse = true;
|
|
|
|
}
|
2014-08-16 18:01:00 +00:00
|
|
|
} while( re_parse == true );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-20 00:21:34 +00:00
|
|
|
bool GetNextTag( FILE* File, char* tag, size_t len )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-01-18 11:49:32 +00:00
|
|
|
int c = SkipGetChar( File );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
if( c == EOF )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-02-20 00:21:34 +00:00
|
|
|
return false;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
2014-08-16 18:01:00 +00:00
|
|
|
|
|
|
|
tag[0] = c;
|
|
|
|
tag[1] = 0;
|
|
|
|
|
|
|
|
// DBG( printf( "tag[0] %c\n", tag[0] ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
if( (c != '}') && (c != ']') )
|
|
|
|
{
|
2015-02-20 00:21:34 +00:00
|
|
|
len--;
|
2014-08-16 18:01:00 +00:00
|
|
|
char* dst = &tag[1];
|
|
|
|
|
2015-02-20 00:21:34 +00:00
|
|
|
while( fscanf( File, "%c", dst ) && len > 0 )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
if( (*dst == ' ') || (*dst == '[') || (*dst == '{')
|
|
|
|
|| (*dst == '\t') || (*dst == '\n')|| (*dst == '\r') )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
|
|
|
*dst = 0;
|
|
|
|
break;
|
|
|
|
}
|
2014-08-16 18:01:00 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
dst++;
|
2015-02-20 00:21:34 +00:00
|
|
|
len--;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "tag %s\n", tag ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
c = SkipGetChar( File );
|
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
if( c != EOF )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
|
|
|
// Puts again the read char in the buffer
|
|
|
|
ungetc( c, File );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 00:21:34 +00:00
|
|
|
return true;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
int Read_NotImplemented( FILE* File, char closeChar )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-01-18 11:49:32 +00:00
|
|
|
int c;
|
2014-08-16 18:01:00 +00:00
|
|
|
|
|
|
|
// DBG( printf( "look for %c\n", closeChar) );
|
|
|
|
while( ( c = fgetc( File ) ) != EOF )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
|
|
|
if( c == '{' )
|
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "{\n") );
|
2015-03-28 11:33:56 +00:00
|
|
|
Read_NotImplemented( File, '}' );
|
2014-08-16 18:01:00 +00:00
|
|
|
}
|
|
|
|
else if( c == '[' )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "[\n") );
|
2015-03-28 11:33:56 +00:00
|
|
|
Read_NotImplemented( File, ']' );
|
2014-08-16 18:01:00 +00:00
|
|
|
}
|
|
|
|
else if( c == closeChar )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "%c\n", closeChar) );
|
2014-07-30 09:01:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( " NotImplemented failed\n" ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
int ParseVertexList( FILE* File, std::vector<glm::vec3>& dst_vector )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-03-28 11:33:56 +00:00
|
|
|
// DBG( printf( " ParseVertexList\n" ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
|
|
|
dst_vector.clear();
|
|
|
|
|
|
|
|
glm::vec3 vertex;
|
2014-08-16 18:01:00 +00:00
|
|
|
|
2015-04-08 12:50:36 +00:00
|
|
|
while( ParseVertex( File, vertex ) )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
|
|
|
dst_vector.push_back( vertex );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-07 11:52:29 +00:00
|
|
|
bool ParseVertex( FILE* File, glm::vec3& dst_vertex )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
float a, b, c;
|
|
|
|
int ret = fscanf( File, "%e %e %e", &a, &b, &c );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
dst_vertex.x = a;
|
|
|
|
dst_vertex.y = b;
|
|
|
|
dst_vertex.z = c;
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-01-18 11:49:32 +00:00
|
|
|
int s = SkipGetChar( File );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
if( s != EOF )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
|
|
|
// Puts again the read char in the buffer
|
|
|
|
ungetc( s, File );
|
|
|
|
}
|
2014-08-16 18:01:00 +00:00
|
|
|
|
|
|
|
// DBG( printf( "ret%d(%.9f,%.9f,%.9f)", ret, a,b,c) );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-04-07 11:52:29 +00:00
|
|
|
return ret == 3;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-02 11:18:19 +00:00
|
|
|
bool ParseFloat( FILE* aFile, float *aDstFloat, float aDefaultValue )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
float value;
|
2015-04-02 11:18:19 +00:00
|
|
|
int ret = fscanf( aFile, "%e", &value );
|
2014-08-16 18:01:00 +00:00
|
|
|
|
2015-04-02 11:18:19 +00:00
|
|
|
if( ret == 1 )
|
|
|
|
*aDstFloat = value;
|
|
|
|
else
|
|
|
|
*aDstFloat = aDefaultValue;
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-04-02 11:18:19 +00:00
|
|
|
return ret == 1;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|