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.

279 lines
7.4 KiB

  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) 2015-2020 KiCad Developers, see CHANGELOG.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 <template_fieldnames.h>
  25. #include <pgm_base.h>
  26. using namespace TFIELD_T;
  27. const wxString TEMPLATE_FIELDNAME::GetDefaultFieldName( int aFieldNdx )
  28. {
  29. static void* locale = nullptr;
  30. static wxString referenceDefault;
  31. static wxString valueDefault;
  32. static wxString footprintDefault;
  33. static wxString datasheetDefault;
  34. static wxString fieldDefault;
  35. // Fetching translations can take a surprising amount of time when loading libraries,
  36. // so only do it when necessary.
  37. if( Pgm().GetLocale() != locale )
  38. {
  39. referenceDefault = _( "Reference" );
  40. valueDefault = _( "Value" );
  41. footprintDefault = _( "Footprint" );
  42. datasheetDefault = _( "Datasheet" );
  43. fieldDefault = _( "Field%d" );
  44. locale = Pgm().GetLocale();
  45. }
  46. // Fixed values for the mandatory fields
  47. switch( aFieldNdx )
  48. {
  49. case REFERENCE: return referenceDefault; // The component reference, R1, C1, etc.
  50. case VALUE: return valueDefault; // The component value + name
  51. case FOOTPRINT: return footprintDefault; // The footprint for use with Pcbnew
  52. case DATASHEET: return datasheetDefault; // Link to a datasheet for component
  53. default: return wxString::Format( fieldDefault, aFieldNdx );
  54. }
  55. }
  56. void TEMPLATE_FIELDNAME::Format( OUTPUTFORMATTER* out, int nestLevel ) const
  57. {
  58. out->Print( nestLevel, "(field (name %s)", out->Quotew( m_Name ).c_str() );
  59. if( m_Visible )
  60. out->Print( 0, " visible" );
  61. if( m_URL )
  62. out->Print( 0, " url" );
  63. out->Print( 0, ")\n" );
  64. }
  65. void TEMPLATE_FIELDNAME::Parse( TEMPLATE_FIELDNAMES_LEXER* in )
  66. {
  67. T tok;
  68. in->NeedLEFT(); // begin (name ...)
  69. if( (tok = in->NextTok()) != T_name )
  70. in->Expecting( T_name );
  71. in->NeedSYMBOLorNUMBER();
  72. m_Name = FROM_UTF8( in->CurText() );
  73. in->NeedRIGHT(); // end (name ...)
  74. while( (tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
  75. {
  76. // "visible" has no '(' prefix, "value" does, so T_LEFT is optional.
  77. if( tok == T_LEFT )
  78. tok = in->NextTok();
  79. switch( tok )
  80. {
  81. case T_value:
  82. // older format; silently skip
  83. in->NeedSYMBOLorNUMBER();
  84. in->NeedRIGHT();
  85. break;
  86. case T_visible:
  87. m_Visible = true;
  88. break;
  89. case T_url:
  90. m_URL = true;
  91. break;
  92. default:
  93. in->Expecting( "value|url|visible" );
  94. break;
  95. }
  96. }
  97. }
  98. void TEMPLATES::Format( OUTPUTFORMATTER* out, int nestLevel, bool aGlobal ) const
  99. {
  100. // We'll keep this general, and include the \n, even though the only known
  101. // use at this time will not want the newlines or the indentation.
  102. out->Print( nestLevel, "(templatefields" );
  103. const TEMPLATE_FIELDNAMES& source = aGlobal ? m_globals : m_project;
  104. for( const TEMPLATE_FIELDNAME& temp : source )
  105. temp.Format( out, nestLevel+1 );
  106. out->Print( 0, ")\n" );
  107. }
  108. void TEMPLATES::Parse( TEMPLATE_FIELDNAMES_LEXER* in, bool aGlobal )
  109. {
  110. T tok;
  111. while( ( tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
  112. {
  113. if( tok == T_LEFT )
  114. tok = in->NextTok();
  115. switch( tok )
  116. {
  117. case T_templatefields: // a token indicating class TEMPLATES.
  118. // Be flexible regarding the starting point of the TEMPLATE_FIELDNAMES_LEXER
  119. // stream. Caller may not have read the first two tokens out of the
  120. // stream: T_LEFT and T_templatefields, so ignore them if seen here.
  121. break;
  122. case T_field:
  123. {
  124. // instantiate on stack, so if exception is thrown,
  125. // destructor runs
  126. TEMPLATE_FIELDNAME field;
  127. field.Parse( in );
  128. // add the field
  129. AddTemplateFieldName( field, aGlobal );
  130. }
  131. break;
  132. default:
  133. in->Unexpected( in->CurText() );
  134. break;
  135. }
  136. }
  137. }
  138. /*
  139. * Flatten project and global templates into a single list. (Project templates take
  140. * precedence.)
  141. */
  142. void TEMPLATES::resolveTemplates()
  143. {
  144. m_resolved = m_project;
  145. // Note: order N^2 algorithm. Would need changing if fieldname template sets ever
  146. // get large.
  147. for( const TEMPLATE_FIELDNAME& global : m_globals )
  148. {
  149. for( const TEMPLATE_FIELDNAME& project : m_project )
  150. {
  151. if( global.m_Name == project.m_Name )
  152. continue;
  153. }
  154. m_resolved.push_back( global );
  155. }
  156. m_resolvedDirty = false;
  157. }
  158. void TEMPLATES::AddTemplateFieldName( const TEMPLATE_FIELDNAME& aFieldName, bool aGlobal )
  159. {
  160. // Ensure that the template fieldname does not match a fixed fieldname.
  161. for( int i = 0; i < MANDATORY_FIELDS; ++i )
  162. {
  163. if( TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) == aFieldName.m_Name )
  164. return;
  165. }
  166. TEMPLATE_FIELDNAMES& target = aGlobal ? m_globals : m_project;
  167. // ensure uniqueness, overwrite any template fieldname by the same name.
  168. for( TEMPLATE_FIELDNAME& temp : target )
  169. {
  170. if( temp.m_Name == aFieldName.m_Name )
  171. {
  172. temp = aFieldName;
  173. m_resolvedDirty = true;
  174. return;
  175. }
  176. }
  177. // the name is legal and not previously added to the config container, append
  178. // it and return its index within the container.
  179. target.push_back( aFieldName );
  180. m_resolvedDirty = true;
  181. }
  182. void TEMPLATES::DeleteAllFieldNameTemplates( bool aGlobal )
  183. {
  184. if( aGlobal )
  185. {
  186. m_globals.clear();
  187. m_resolved = m_project;
  188. }
  189. else
  190. {
  191. m_project.clear();
  192. m_resolved = m_globals;
  193. }
  194. m_resolvedDirty = false;
  195. }
  196. const TEMPLATE_FIELDNAMES& TEMPLATES::GetTemplateFieldNames()
  197. {
  198. if( m_resolvedDirty )
  199. resolveTemplates();
  200. return m_resolved;
  201. }
  202. const TEMPLATE_FIELDNAMES& TEMPLATES::GetTemplateFieldNames( bool aGlobal )
  203. {
  204. if( aGlobal )
  205. return m_globals;
  206. else
  207. return m_project;
  208. }
  209. const TEMPLATE_FIELDNAME* TEMPLATES::GetFieldName( const wxString& aName )
  210. {
  211. if( m_resolvedDirty )
  212. resolveTemplates();
  213. for( const TEMPLATE_FIELDNAME& field : m_resolved )
  214. {
  215. if( field.m_Name == aName )
  216. return &field;
  217. }
  218. return nullptr;
  219. }