KiCad PCB EDA Suite
Loading...
Searching...
No Matches
idf_helpers.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2014-2017 Cirilo Bernardo
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <cctype>
22#include <iostream>
23#include <sstream>
24
25#include <io/idf/idf_common.h>
26#include <io/idf/idf_helpers.h>
27
28#include "string_utils.h"
29
30using namespace std;
31using namespace IDF3;
32
33
34bool IDF3::FetchIDFLine( std::istream& aModel, std::string& aLine, bool& isComment,
35 std::streampos& aFilePos )
36{
37 aLine = "";
38 aFilePos = aModel.tellg();
39
40 if( aModel.fail() )
41 return false;
42
43 std::getline( aModel, aLine );
44
45 isComment = false;
46
47 // A comment begins with a '#' and must be the first character on the line
48 if( aLine[0] == '#' )
49 {
50 // opening '#' is stripped
51 isComment = true;
52 aLine.erase( aLine.begin() );
53 }
54
55 // strip leading and trailing spaces
56 while( !aLine.empty() && isspace( *aLine.begin() ) )
57 aLine.erase( aLine.begin() );
58
59 while( !aLine.empty() && isspace( *aLine.rbegin() ) )
60 aLine.erase( --aLine.end() );
61
62 // a comment line may be empty to improve human readability
63 if( aLine.empty() && !isComment )
64 return false;
65
66 return true;
67}
68
69
70bool IDF3::GetIDFString( const std::string& aLine, std::string& aIDFString, bool& hasQuotes,
71 int& aIndex )
72{
73 // 1. drop all leading spaces
74 // 2. if the first character is '"', read until the next '"',
75 // otherwise read until the next space or EOL.
76
77 std::ostringstream ostr;
78
79 int len = aLine.length();
80 int idx = aIndex;
81
82 if( idx < 0 || idx >= len )
83 return false;
84
85 while( idx < len && isspace( aLine[idx] ) )
86 ++idx;
87
88 if( idx == len )
89 {
90 aIndex = idx;
91 return false;
92 }
93
94 if( aLine[idx] == '"' )
95 {
96 hasQuotes = true;
97 ++idx;
98
99 while( idx < len && aLine[idx] != '"' )
100 ostr << aLine[idx++];
101
102 if( idx == len )
103 {
104 ERROR_IDF << "unterminated quote mark in line:\n";
105 aIndex = idx;
106 return false;
107 }
108
109 ++idx;
110 }
111 else
112 {
113 hasQuotes = false;
114
115 while( idx < len && !isspace( aLine[idx] ) )
116 ostr << aLine[idx++];
117
118 }
119
120 aIDFString = hasQuotes ? UnescapeString( ostr.str() ) : ostr.str();
121 aIndex = idx;
122
123 return true;
124}
125
126
127// perform a comparison between a fixed token string and an input string.
128// the token is assumed to be an upper case IDF token and the input string
129// is data from an IDF file. Since IDF tokens are case-insensitive, we cannot
130// assume anything about the case of the input string.
131bool IDF3::CompareToken( const char* aTokenString, const std::string& aInputString )
132{
133 std::string::size_type i, j;
134 std::string bigToken = aInputString;
135 j = aInputString.length();
136
137 for( i = 0; i < j; ++i )
138 bigToken[i] = std::toupper( bigToken[i] );
139
140 if( !bigToken.compare( aTokenString ) )
141 return true;
142
143 return false;
144}
145
146
147bool IDF3::ParseOwner( const std::string& aToken, IDF3::KEY_OWNER& aOwner )
148{
149 if( CompareToken( "UNOWNED", aToken ) )
150 {
151 aOwner = KEY_OWNER::UNOWNED;
152 return true;
153 }
154 else if( CompareToken( "ECAD", aToken ) )
155 {
156 aOwner = KEY_OWNER::ECAD;
157 return true;
158 }
159 else if( CompareToken( "MCAD", aToken ) )
160 {
161 aOwner = KEY_OWNER::MCAD;
162 return true;
163 }
164
165 ERROR_IDF << "unrecognized IDF OWNER: '" << aToken << "'\n";
166
167 return false;
168}
169
170
171bool IDF3::ParseIDFLayer( const std::string& aToken, IDF3::IDF_LAYER& aLayer )
172{
173 if( CompareToken( "TOP", aToken ) )
174 {
175 aLayer = IDF_LAYER::LYR_TOP;
176 return true;
177 }
178 else if( CompareToken( "BOTTOM", aToken ) )
179 {
180 aLayer = IDF_LAYER::LYR_BOTTOM;
181 return true;
182 }
183 else if( CompareToken( "BOTH", aToken ) )
184 {
185 aLayer = IDF_LAYER::LYR_BOTH;
186 return true;
187 }
188 else if( CompareToken( "INNER", aToken ) )
189 {
190 aLayer = IDF_LAYER::LYR_INNER;
191 return true;
192 }
193 else if( CompareToken( "ALL", aToken ) )
194 {
195 aLayer = IDF_LAYER::LYR_ALL;
196 return true;
197 }
198
199 ERROR_IDF << "unrecognized IDF LAYER: '" << aToken << "'\n";
200
201 aLayer = IDF_LAYER::LYR_INVALID;
202 return false;
203}
204
205
206bool IDF3::WriteLayersText( std::ostream& aBoardFile, IDF3::IDF_LAYER aLayer )
207{
208 switch( aLayer )
209 {
211 aBoardFile << "TOP";
212 break;
213
215 aBoardFile << "BOTTOM";
216 break;
217
219 aBoardFile << "BOTH";
220 break;
221
223 aBoardFile << "INNER";
224 break;
225
227 aBoardFile << "ALL";
228 break;
229
230 default:
231 do
232 {
233 std::ostringstream ostr;
234 ostr << "invalid IDF layer: " << static_cast<int>( aLayer );
235
236 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
237 } while( 0 );
238
239 break;
240 }
241
242 return !aBoardFile.fail();
243}
244
245
247{
248 switch( aPlacement )
249 {
251 return "UNPLACED";
252
254 return "PLACED";
255
257 return "MCAD";
258
260 return "ECAD";
261
262 default:
263 break;
264 }
265
266 std::ostringstream ostr;
267 ostr << "[INVALID PLACEMENT VALUE]:" << static_cast<int>( aPlacement );
268
269 return ostr.str();
270}
271
272
274{
275 switch( aLayer )
276 {
278 return "TOP";
279
281 return "BOTTOM";
282
284 return "BOTH";
285
287 return "INNER";
288
290 return "ALL";
291
292 default:
293 break;
294 }
295
296 std::ostringstream ostr;
297 ostr << "[INVALID LAYER VALUE]:" << static_cast<int>( aLayer );
298
299 return ostr.str();
300}
301
302
304{
305 switch( aOwner )
306 {
308 return "UNOWNED";
309
310 case KEY_OWNER::MCAD:
311 return "MCAD";
312
313 case KEY_OWNER::ECAD:
314 return "ECAD";
315
316 default:
317 break;
318 }
319
320 ostringstream ostr;
321 ostr << "UNKNOWN: " << static_cast<int>( aOwner );
322
323 return ostr.str();
324}
#define ERROR_IDF
Definition idf_helpers.h:29
std::string GetPlacementString(IDF3::IDF_PLACEMENT aPlacement)
Return a string representing the given IDF Placement type.
bool ParseIDFLayer(const std::string &aToken, IDF3::IDF_LAYER &aLayer)
Parse an input string for a valid IDF layer specification.
IDF_PLACEMENT
The placement status of a component.
Definition idf_common.h:188
bool GetIDFString(const std::string &aLine, std::string &aIDFString, bool &hasQuotes, int &aIndex)
Parse a line retrieved via FetchIDFLine() and returns the first IDF string found from the starting po...
std::string GetOwnerString(IDF3::KEY_OWNER aOwner)
bool FetchIDFLine(std::istream &aModel, std::string &aLine, bool &isComment, std::streampos &aFilePos)
Retrieve a single line from an IDF file and performs minimal processing.
bool CompareToken(const char *aTokenString, const std::string &aInputString)
Perform a case-insensitive comparison of a token string and an input string.
IDF_LAYER
The various IDF layer classes and groupings.
Definition idf_common.h:137
KEY_OWNER
The type of CAD which has ownership an object.
Definition idf_common.h:85
bool WriteLayersText(std::ostream &aBoardFile, IDF3::IDF_LAYER aLayer)
Write out text corresponding to the given IDF Layer type.
bool ParseOwner(const std::string &aToken, IDF3::KEY_OWNER &aOwner)
Parse the input string for a valid IDF Owner type.
std::string GetLayerString(IDF3::IDF_LAYER aLayer)
Return a string representing the given IDF Layer type.
STL namespace.
wxString UnescapeString(const wxString &aSource)