You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

501 lines
11 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2010 KiCad Developers, see change_log.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, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <cstring>
  25. #include <wx/wx.h> // _()
  26. #include <sch_lpid.h>
  27. using namespace SCH;
  28. static inline bool isDigit( char c )
  29. {
  30. return c >= '0' && c <= '9';
  31. }
  32. const char* EndsWithRev( const char* start, const char* tail, char separator )
  33. {
  34. bool sawDigit = false;
  35. while( tail > start && isDigit( *--tail ) )
  36. {
  37. sawDigit = true;
  38. }
  39. // if sawDigit, tail points to the 'v' here.
  40. if( sawDigit && tail-3 >= start )
  41. {
  42. tail -= 3;
  43. if( tail[0]==separator && tail[1]=='r' && tail[2]=='e' && tail[3]=='v' )
  44. {
  45. return tail+1; // omit separator, return "revN[N..]"
  46. }
  47. }
  48. return 0;
  49. }
  50. int RevCmp( const char* s1, const char* s2 )
  51. {
  52. int r = strncmp( s1, s2, 3 );
  53. if( r || strlen(s1)<4 || strlen(s2)<4 )
  54. {
  55. return r;
  56. }
  57. int rnum1 = atoi( s1+3 );
  58. int rnum2 = atoi( s2+3 );
  59. return -(rnum1 - rnum2); // swap the sign, higher revs first
  60. }
  61. //----<Policy and field test functions>-------------------------------------
  62. // These all return -1 on success, or >= 0 if there is an error at a
  63. // particular character offset into their respective arguments. If >=0,
  64. // then that return value gives the character offset of the error.
  65. static inline int okLogical( const STRING& aField )
  66. {
  67. // std::string::npos is largest positive number, casting to int makes it -1.
  68. // Returning that means success.
  69. return int( aField.find_first_of( ":/" ) );
  70. }
  71. static inline int okBase( const STRING& aField )
  72. {
  73. int offset = int( aField.find_first_of( ":/" ) );
  74. if( offset != -1 )
  75. return offset;
  76. // cannot be empty
  77. if( !aField.size() )
  78. return 0;
  79. return offset; // ie. -1
  80. }
  81. static inline int okCategory( const STRING& aField )
  82. {
  83. return int( aField.find_first_of( ":/" ) );
  84. }
  85. static int okRevision( const STRING& aField )
  86. {
  87. char rev[32]; // C string for speed
  88. if( aField.size() >= 4 )
  89. {
  90. strcpy( rev, "x/" );
  91. strcat( rev, aField.c_str() );
  92. if( EndsWithRev( rev, rev + strlen(rev) ) == rev+2 )
  93. return -1; // success
  94. }
  95. return 0; // first character position "is in error", is best we can do.
  96. }
  97. //----</Policy and field test functions>-------------------------------------
  98. void LPID::clear()
  99. {
  100. logical.clear();
  101. category.clear();
  102. baseName.clear();
  103. partName.clear();
  104. revision.clear();
  105. }
  106. int LPID::Parse( const STRING& aLPID )
  107. {
  108. clear();
  109. const char* rev = EndsWithRev( aLPID );
  110. size_t revNdx;
  111. size_t partNdx;
  112. size_t baseNdx;
  113. int offset;
  114. //=====<revision>=========================================
  115. if( rev )
  116. {
  117. revNdx = rev - aLPID.c_str();
  118. // no need to check revision, EndsWithRev did that.
  119. revision = aLPID.substr( revNdx );
  120. --revNdx; // back up to omit the '/' which preceeds the rev
  121. }
  122. else
  123. revNdx = aLPID.size();
  124. //=====<logical>==========================================
  125. if( ( partNdx = aLPID.find( ':' ) ) != aLPID.npos )
  126. {
  127. offset = SetLogicalLib( aLPID.substr( 0, partNdx ) );
  128. if( offset > -1 )
  129. {
  130. return offset;
  131. }
  132. ++partNdx; // skip ':'
  133. }
  134. else
  135. partNdx = 0;
  136. //=====<rawName && category>==============================
  137. // "length limited" search:
  138. const char* base = (const char*) memchr( aLPID.c_str() + partNdx, '/', revNdx - partNdx );
  139. if( base )
  140. {
  141. baseNdx = base - aLPID.c_str();
  142. offset = SetCategory( aLPID.substr( partNdx, baseNdx - partNdx ) );
  143. if( offset > -1 )
  144. {
  145. return offset + partNdx;
  146. }
  147. ++baseNdx; // skip '/'
  148. }
  149. else
  150. {
  151. baseNdx = partNdx;
  152. }
  153. //=====<baseName>==========================================
  154. offset = SetBaseName( aLPID.substr( baseNdx, revNdx - baseNdx ) );
  155. if( offset > -1 )
  156. {
  157. return offset + baseNdx;
  158. }
  159. return -1;
  160. }
  161. LPID::LPID( const STRING& aLPID ) throw( PARSE_ERROR )
  162. {
  163. int offset = Parse( aLPID );
  164. if( offset != -1 )
  165. {
  166. THROW_PARSE_ERROR(
  167. _( "Illegal character found in LPID string" ),
  168. wxString::FromUTF8( aLPID.c_str() ),
  169. aLPID.c_str(),
  170. 0,
  171. offset
  172. );
  173. }
  174. }
  175. int LPID::SetLogicalLib( const STRING& aLogical )
  176. {
  177. int offset = okLogical( aLogical );
  178. if( offset == -1 )
  179. {
  180. logical = aLogical;
  181. }
  182. return offset;
  183. }
  184. int LPID::SetCategory( const STRING& aCategory )
  185. {
  186. int offset = okCategory( aCategory );
  187. if( offset == -1 )
  188. {
  189. category = aCategory;
  190. // set the partName too
  191. if( category.size() )
  192. {
  193. partName = category;
  194. partName += '/';
  195. partName += baseName;
  196. }
  197. else
  198. partName = baseName;
  199. }
  200. return offset;
  201. }
  202. int LPID::SetBaseName( const STRING& aBaseName )
  203. {
  204. int offset = okBase( aBaseName );
  205. if( offset == -1 )
  206. {
  207. baseName = aBaseName;
  208. // set the partName too
  209. if( category.size() )
  210. {
  211. partName = category;
  212. partName += '/';
  213. partName += baseName;
  214. }
  215. else
  216. partName = baseName;
  217. }
  218. return offset;
  219. }
  220. int LPID::SetPartName( const STRING& aPartName )
  221. {
  222. STRING category;
  223. STRING base;
  224. int offset;
  225. int separation = int( aPartName.find_first_of( "/" ) );
  226. if( separation != -1 )
  227. {
  228. category = aPartName.substr( 0, separation );
  229. base = aPartName.substr( separation+1 );
  230. }
  231. else
  232. {
  233. // leave category empty
  234. base = aPartName;
  235. }
  236. if( (offset = SetCategory( category )) != -1 )
  237. return offset;
  238. if( (offset = SetBaseName( base )) != -1 )
  239. {
  240. return offset + separation + 1;
  241. }
  242. return -1;
  243. }
  244. int LPID::SetRevision( const STRING& aRevision )
  245. {
  246. int offset = okRevision( aRevision );
  247. if( offset == -1 )
  248. {
  249. revision = aRevision;
  250. }
  251. return offset;
  252. }
  253. STRING LPID::Format() const
  254. {
  255. STRING ret;
  256. if( logical.size() )
  257. {
  258. ret += logical;
  259. ret += ':';
  260. }
  261. if( category.size() )
  262. {
  263. ret += category;
  264. ret += '/';
  265. }
  266. ret += baseName;
  267. if( revision.size() )
  268. {
  269. ret += '/';
  270. ret += revision;
  271. }
  272. return ret;
  273. }
  274. STRING LPID::GetPartNameAndRev() const
  275. {
  276. STRING ret;
  277. if( category.size() )
  278. {
  279. ret += category;
  280. ret += '/';
  281. }
  282. ret += baseName;
  283. if( revision.size() )
  284. {
  285. ret += '/';
  286. ret += revision;
  287. }
  288. return ret;
  289. }
  290. STRING LPID::Format( const STRING& aLogicalLib, const STRING& aPartName, const STRING& aRevision )
  291. throw( PARSE_ERROR )
  292. {
  293. STRING ret;
  294. int offset;
  295. if( aLogicalLib.size() )
  296. {
  297. offset = okLogical( aLogicalLib );
  298. if( offset != -1 )
  299. {
  300. THROW_PARSE_ERROR(
  301. _( "Illegal character found in logical lib name" ),
  302. wxString::FromUTF8( aLogicalLib.c_str() ),
  303. aLogicalLib.c_str(),
  304. 0,
  305. offset
  306. );
  307. }
  308. ret += aLogicalLib;
  309. ret += ':';
  310. }
  311. {
  312. STRING category;
  313. STRING base;
  314. int separation = int( aPartName.find_first_of( "/" ) );
  315. if( separation != -1 )
  316. {
  317. category = aPartName.substr( 0, separation );
  318. base = aPartName.substr( separation+1 );
  319. }
  320. else
  321. {
  322. // leave category empty
  323. base = aPartName;
  324. }
  325. if( (offset = okCategory( category )) != -1 )
  326. {
  327. THROW_PARSE_ERROR(
  328. _( "Illegal character found in category" ),
  329. wxString::FromUTF8( aRevision.c_str() ),
  330. aRevision.c_str(),
  331. 0,
  332. offset
  333. );
  334. }
  335. if( (offset = okBase( base )) != -1 )
  336. {
  337. THROW_PARSE_ERROR(
  338. _( "Illegal character found in base name" ),
  339. wxString::FromUTF8( aRevision.c_str() ),
  340. aRevision.c_str(),
  341. 0,
  342. offset + separation + 1
  343. );
  344. }
  345. if( category.size() )
  346. {
  347. ret += category;
  348. ret += '/';
  349. }
  350. ret += base;
  351. }
  352. if( aRevision.size() )
  353. {
  354. offset = okRevision( aRevision );
  355. if( offset != -1 )
  356. {
  357. THROW_PARSE_ERROR(
  358. _( "Illegal character found in revision" ),
  359. wxString::FromUTF8( aRevision.c_str() ),
  360. aRevision.c_str(),
  361. 0,
  362. offset
  363. );
  364. }
  365. ret += '/';
  366. ret += aRevision;
  367. }
  368. return ret;
  369. }
  370. #if 0 && defined(DEBUG)
  371. // build this with Debug CMAKE_BUILD_TYPE
  372. void LPID::Test()
  373. {
  374. static const char* lpids[] = {
  375. "me:passives/R/rev0",
  376. "passives/R/rev2",
  377. ":passives/R/rev3",
  378. "C/rev22",
  379. "passives/C22",
  380. "R",
  381. "me:R",
  382. // most difficult:
  383. "me:/R/rev0",
  384. "me:R/rev0",
  385. };
  386. for( unsigned i=0; i<sizeof(lpids)/sizeof(lpids[0]); ++i )
  387. {
  388. // test some round tripping
  389. LPID lpid( lpids[i] ); // parse
  390. // format
  391. printf( "input:'%s' full:'%s' base:'%s' partName:'%s' cat:'%s' rev:'%s'\n",
  392. lpids[i],
  393. lpid.Format().c_str(),
  394. lpid.GetBaseName().c_str(),
  395. lpid.GetPartName().c_str(),
  396. lpid.GetCategory().c_str(),
  397. lpid.GetRevision().c_str()
  398. );
  399. }
  400. }
  401. int main( int argc, char** argv )
  402. {
  403. LPID::Test();
  404. return 0;
  405. }
  406. #endif