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.

278 lines
7.8 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
  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 <memory> // std::auto_ptr
  25. #include <wx/string.h>
  26. #include <sch_lib.h>
  27. #include <sch_lpid.h>
  28. #include <sch_part.h>
  29. #include <sch_sweet_parser.h>
  30. #include <sch_lib_table.h>
  31. #if 1
  32. #include <map>
  33. /*
  34. The LIB part cache consists of a std::map of partnames without revisions at the
  35. top level. Each top level map entry can point to another std::map which it owns
  36. and which holds all the revisions for that part name. At any point in the tree,
  37. there can be NULL pointers which allow for lazy loading, including the very top
  38. most root pointer itself, which is PARTS* parts. We use the key to hold the
  39. partName at one level, and revision at the deeper nested level.
  40. 1) Only things which are asked for are done.
  41. 2) Anything we learn we remember.
  42. */
  43. namespace SCH {
  44. /**
  45. * Struct LTREV
  46. * is for PART_REVS, and provides a custom way to compare rev STRINGs.
  47. * Namely, the revN[N..] string if present, is collated according to a
  48. * 'higher revision first'.
  49. */
  50. struct LTREV
  51. {
  52. bool operator() ( const STRING& s1, const STRING& s2 ) const
  53. {
  54. return RevCmp( s1.c_str(), s2.c_str() ) < 0;
  55. }
  56. };
  57. /**
  58. * Class PART_REVS
  59. * contains the collection of revisions for a particular part name, in the
  60. * form of cached PARTs. The tuple consists of a rev string and a PART pointer.
  61. * The rev string is like "rev1", the PART pointer will be NULL until the PART
  62. * gets loaded, lazily.
  63. */
  64. class PART_REVS : public std::map< STRING, PART*, LTREV >
  65. {
  66. public:
  67. ~PART_REVS()
  68. {
  69. for( iterator it = begin(); it != end(); ++it )
  70. {
  71. delete it->second; // second may be NULL, no problem
  72. }
  73. }
  74. };
  75. /**
  76. * Class PARTS
  77. * contains the collection of PART_REVS for all PARTs in the lib.
  78. * The tuple consists of a part name and a PART_REVS pointer.
  79. * The part name does not have the revision attached (of course this is understood
  80. * by definition of "part name"). The PART_REVS pointer will be NULL until a client
  81. * askes about the revisions for a part name, so the loading is done lazily.
  82. */
  83. class PARTS : public std::map< STRING, PART_REVS* >
  84. {
  85. public:
  86. ~PARTS()
  87. {
  88. for( iterator it = begin(); it != end(); ++it )
  89. {
  90. delete it->second; // second may be NULL, no problem
  91. }
  92. }
  93. };
  94. } // namespace SCH
  95. #else // was nothing but grief:
  96. #include <boost/ptr_container/ptr_map.hpp>
  97. namespace SCH {
  98. /// PARTS' key is revision, like "rev12", PART pointer may be null until loaded.
  99. typedef boost::ptr_map< STRING, boost::nullable<PART> > PARTS;
  100. typedef PARTS::iterator PARTS_ITER;
  101. typedef PARTS::const_iterator PARTS_CITER;
  102. /// PART_REVS' key is part name, w/o rev, PART pointer may be null until loaded.
  103. typedef boost::ptr_map< STRING, boost::nullable<PARTS> > PART_REVS;
  104. typedef PART_REVS::iterator PART_REVS_ITER;
  105. typedef PART_REVS::const_iterator PART_REVS_CITER;
  106. } // namespace SCH
  107. #endif
  108. using namespace SCH;
  109. LIB::LIB( const STRING& aLogicalLibrary, LIB_SOURCE* aSource, LIB_SINK* aSink ) :
  110. logicalName( aLogicalLibrary ),
  111. source( aSource ),
  112. sink( aSink ),
  113. cachedCategories( false ),
  114. parts( 0 )
  115. {
  116. }
  117. LIB::~LIB()
  118. {
  119. delete source;
  120. delete sink;
  121. delete parts;
  122. }
  123. const PART* LIB::lookupPart( const LPID& aLPID ) throw( IO_ERROR )
  124. {
  125. if( !parts )
  126. {
  127. parts = new PARTS;
  128. source->GetCategoricalPartNames( &vfetch );
  129. // insert a PART_REVS for each part name
  130. for( STRINGS::const_iterator it = vfetch.begin(); it!=vfetch.end(); ++it )
  131. {
  132. D(printf("lookupPart:%s\n", it->c_str() );)
  133. (*parts)[*it] = new PART_REVS;
  134. }
  135. }
  136. // load all the revisions for this part name, only if it has any
  137. PARTS::iterator pi = parts->find( aLPID.GetPartName() );
  138. PART_REVS* revs = pi != parts->end() ? pi->second : NULL;
  139. // D(printf("revs:%p partName:%s\n", revs, aLPID.GetPartName().c_str() );)
  140. // if the key for parts has no aLPID.GetPartName() the part is not in this lib
  141. if( revs )
  142. {
  143. if( revs->size() == 0 ) // assume rev list has not been loaded yet
  144. {
  145. // load all the revisions for this part.
  146. source->GetRevisions( &vfetch, aLPID.GetPartName() );
  147. // create a PART_REV entry for each revision, but leave the PART* NULL
  148. for( STRINGS::const_iterator it = vfetch.begin(); it!=vfetch.end(); ++it )
  149. {
  150. D(printf("lookupPartRev:%s\n", it->c_str() );)
  151. (*revs)[*it] = 0;
  152. }
  153. }
  154. PART_REVS::iterator rev;
  155. // If caller did not say what revision, find the highest numbered one and return that.
  156. if( !aLPID.GetRevision().size() && revs->size() )
  157. {
  158. rev = revs->begin(); // sort order has highest rev first
  159. if( !rev->second ) // the PART has never been instantiated before
  160. {
  161. rev->second = new PART( this, LPID::Format( "", aLPID.GetPartName(), rev->first ) );
  162. }
  163. D(printf("lookupPartLatestRev:%s\n", rev->second->partNameAndRev.c_str() );)
  164. return rev->second;
  165. }
  166. else
  167. {
  168. rev = revs->find( aLPID.GetRevision() );
  169. if( rev != revs->end() )
  170. {
  171. if( !rev->second ) // the PART has never been instantiated before
  172. {
  173. rev->second = new PART( this, aLPID.GetPartNameAndRev() );
  174. }
  175. return rev->second;
  176. }
  177. }
  178. }
  179. return 0; // no such part name in this lib
  180. }
  181. PART* LIB::LookupPart( const LPID& aLPID, LIB_TABLE* aLibTable ) throw( IO_ERROR )
  182. {
  183. PART* part = (PART*) lookupPart( aLPID );
  184. if( !part ) // part does not exist in this lib
  185. {
  186. wxString msg = wxString::Format( _("part '%s' not found in lib %s" ),
  187. wxString::FromUTF8( aLPID.GetPartNameAndRev().c_str() ).GetData(),
  188. wxString::FromUTF8( logicalName.c_str() ).GetData() );
  189. THROW_IO_ERROR( msg );
  190. }
  191. if( part->body.empty() )
  192. {
  193. // load body
  194. source->ReadPart( &part->body, aLPID.GetPartName(), aLPID.GetRevision() );
  195. #if 0 && defined(DEBUG)
  196. const STRING& body = part->body;
  197. printf( "body: %s", body.c_str() );
  198. if( !body.size() || body[body.size()-1] != '\n' )
  199. printf( "\n" );
  200. #endif
  201. // @todo consider changing ReadPart to return a "source"
  202. SWEET_PARSER sp( part->body, wxString::FromUTF8( aLPID.Format().c_str() ) );
  203. part->Parse( &sp, aLibTable );
  204. }
  205. return part;
  206. }
  207. #if 0 && defined(DEBUG)
  208. void LIB::Test( int argc, char** argv ) throw( IO_ERROR );
  209. {
  210. }
  211. int main( int argc, char** argv )
  212. {
  213. LIB::Test( argc, argv );
  214. return 0;
  215. }
  216. #endif