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.

198 lines
6.2 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
  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. #ifndef DIR_LIB_SOURCE_H_
  25. #define DIR_LIB_SOURCE_H_
  26. #include <set>
  27. #include <vector>
  28. #include <sch_lib.h>
  29. /// This file extension is an implementation detail specific to this LIB_SOURCE
  30. /// and to a corresponding LIB_SINK.
  31. /// Core EESCHEMA should never have to see this.
  32. #define SWEET_EXT ".part"
  33. #define SWEET_EXTZ (sizeof(SWEET_EXT)-1)
  34. /**
  35. * struct BY_REV
  36. * is here to provide a custom way to compare STRINGs. Namely, the revN[N..]
  37. * string if present, is collated according to a 'higher revision first', but
  38. * any part string without a revision, is even 'before' that.
  39. */
  40. struct BY_REV
  41. {
  42. bool operator() ( const STRING& s1, const STRING& s2 ) const;
  43. };
  44. /**
  45. * Type PART_CACHE
  46. * holds a set of part names in sorted order, according to the sort
  47. * order given by struct BY_REV.
  48. */
  49. typedef std::set< STRING, BY_REV > PART_CACHE;
  50. /**
  51. * Type NAME_CACHE
  52. * holds a set of categories in sorted order.
  53. */
  54. typedef std::set< STRING > NAME_CACHE;
  55. namespace SCH {
  56. /**
  57. * Class DIR_LIB_SOURCE
  58. * implements a LIB_SOURCE in a file system directory.
  59. *
  60. * @author Dick Hollenbeck
  61. */
  62. class DIR_LIB_SOURCE : public LIB_SOURCE
  63. {
  64. friend class LIB_TABLE; ///< constructor is protected, LIB_TABLE can construct
  65. bool useVersioning; ///< use files with extension ".revNNN..", else not
  66. /// normal partnames, some of which may be prefixed with a category,
  67. /// and some of which may have legal "revN[N..]" type strings.
  68. PART_CACHE partnames;
  69. typedef PART_CACHE::const_iterator PN_ITER;
  70. /// categories which we expect to find in the set of @a partnames
  71. NAME_CACHE categories;
  72. std::vector<char> readBuffer; ///< used by readString()
  73. /**
  74. * Function cache
  75. * [re-]loads the directory cache(s).
  76. */
  77. void cache();
  78. /**
  79. * Function isCategoryName
  80. * returns true iff aName is a valid category name.
  81. */
  82. bool isCategoryName( const char* aName )
  83. {
  84. return true;
  85. }
  86. /**
  87. * Function makePartName
  88. * returns true iff aEntry holds a valid part filename, in the form of
  89. * "someroot.part[.revNNNN]" where NNN are number characters [0-9]
  90. * @param aEntry is the raw directory entry without path information.
  91. * @param aCategory is the last portion of the directory path.
  92. * @param aPartName is where to put a part name, assuming @a aEntry is legal.
  93. * @return bool - true only if aEntry is a legal part file name.
  94. */
  95. bool makePartName( STRING* aPartName, const char* aEntry, const STRING& aCategory );
  96. /**
  97. * Function readString
  98. * reads a Sweet string into aResult. Candidate for virtual function later.
  99. */
  100. void readString( STRING* aResult, const STRING& aFileName );
  101. /**
  102. * Function cacheOneDir
  103. * loads part names [and categories] from a directory given by
  104. * "sourceURI + '/' + category"
  105. * Categories are only loaded if processing the top most directory because
  106. * only one level of categories is supported. We know we are in the
  107. * top most directory if aCategory is empty.
  108. */
  109. void cacheOneDir( const STRING& aCategory );
  110. /**
  111. * Function makeFileName
  112. * converts a part name into a filename and returns it.
  113. */
  114. STRING makeFileName( const STRING& aPartName );
  115. protected:
  116. /**
  117. * Constructor DIR_LIB_SOURCE( const STRING& aDirectoryPath )
  118. * sets up a LIB_SOURCE using aDirectoryPath in a file system.
  119. * @see LIB_TABLE::LookupPart().
  120. *
  121. * @param aDirectoryPath is a full file pathname of a directory which contains
  122. * the library source of part files. Examples might be "C:\kicad_data\mylib" or
  123. * "/home/designer/mylibdir". This is not a URI, but an OS specific path that
  124. * can be given to opendir().
  125. *
  126. * @param aOptions is the options string from the library table, currently
  127. * the only supported option, that this LIB_SOURCE knows about is
  128. * "useVersioning". If present means support versioning in the directory
  129. * tree, otherwise only a single version of each part is recognized, namely the
  130. * one without the ".revN[N..]" trailer.
  131. */
  132. DIR_LIB_SOURCE( const STRING& aDirectoryPath, const STRING& aOptions = "" );
  133. ~DIR_LIB_SOURCE();
  134. //-----<LIB_SOURCE implementation functions >------------------------------
  135. void ReadPart( STRING* aResult, const STRING& aPartName, const STRING& aRev = "" );
  136. void ReadParts( STRINGS* aResults, const STRINGS& aPartNames );
  137. void GetCategories( STRINGS* aResults );
  138. void GetCategoricalPartNames( STRINGS* aResults, const STRING& aCategory = "" );
  139. void GetRevisions( STRINGS* aResults, const STRING& aPartName );
  140. void FindParts( STRINGS* aResults, const STRING& aQuery )
  141. {
  142. // @todo
  143. }
  144. //-----</LIB_SOURCE implementation functions >------------------------------
  145. #if defined(DEBUG)
  146. /**
  147. * Function Show
  148. * will output a debug dump of contents.
  149. */
  150. void Show();
  151. public:
  152. static void Test( int argc, char** argv );
  153. #endif
  154. };
  155. } // namespace SCH
  156. #endif // DIR_LIB_SOURCE_H_