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.

188 lines
5.2 KiB

  1. /****************/
  2. /* savecmp.cpp */
  3. /****************/
  4. #include "fctsys.h"
  5. #include "wxstruct.h"
  6. #include "common.h"
  7. #include "confirm.h"
  8. #include "kicad_string.h"
  9. #include "gestfich.h"
  10. #include "macros.h"
  11. #include "appl_wxstruct.h"
  12. #include "cvpcb.h"
  13. #include "protos.h"
  14. #include "cvstruct.h"
  15. #include "build_version.h"
  16. /* File header. */
  17. char EnteteCmpMod[] = { "Cmp-Mod V01" };
  18. const wxString titleComponentLibErr( _( "Component Library Error" ) );
  19. /*
  20. * Backup modules file.
  21. *
  22. * @param NetlistFullFileName - Name of net list file to save.
  23. * @returns - 1 if OK, 0 if error.
  24. */
  25. int WinEDA_CvpcbFrame::SaveComponentList( const wxString& NetlistFullFileName )
  26. {
  27. FILE* dest;
  28. wxFileName fn( NetlistFullFileName );
  29. char Line[1024];
  30. wxString Title = wxGetApp().GetTitle() + wxT( " " ) + GetBuildVersion();
  31. fn.SetExt( ComponentFileExtension );
  32. dest = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
  33. if( dest == NULL )
  34. return 0;
  35. fprintf( dest, "%s", EnteteCmpMod );
  36. fprintf( dest, " Created by %s", CONV_TO_UTF8( Title ) );
  37. fprintf( dest, " date = %s\n", DateAndTime( Line ) );
  38. BOOST_FOREACH( COMPONENT& component, m_components )
  39. {
  40. fprintf( dest, "\nBeginCmp\n" );
  41. fprintf( dest, "TimeStamp = %s;\n",
  42. CONV_TO_UTF8( component.m_TimeStamp ) );
  43. fprintf( dest, "Reference = %s;\n",
  44. CONV_TO_UTF8( component.m_Reference ) );
  45. fprintf( dest, "ValeurCmp = %s;\n",
  46. CONV_TO_UTF8( component.m_Value ) );
  47. fprintf( dest, "IdModule = %s;\n",
  48. CONV_TO_UTF8( component.m_Module ) );
  49. fprintf( dest, "EndCmp\n" );
  50. }
  51. fprintf( dest, "\nEndListe\n" );
  52. fclose( dest );
  53. return 1;
  54. }
  55. /*
  56. * Load list of associated components and footprints.
  57. */
  58. bool LoadComponentFile( const wxString& fileName, COMPONENT_LIST& list )
  59. {
  60. wxString timestamp, valeur, ilib, namecmp, msg;
  61. bool read_cmp_data = FALSE, eof = FALSE;
  62. char Line[1024], * ident, * data;
  63. FILE* source;
  64. wxFileName fn = fileName;
  65. fn.SetExt( ComponentFileExtension );
  66. source = wxFopen( fn.GetFullPath(), wxT( "rt" ) );
  67. if( source == NULL )
  68. {
  69. msg.Printf( _( "Cannot open component library <%s>." ),
  70. GetChars( fn.GetFullPath() ) );
  71. wxMessageBox( msg, titleComponentLibErr, wxOK | wxICON_ERROR );
  72. return false;
  73. }
  74. /* Identification of the type of file CmpMod */
  75. if( fgets( Line, 79, source ) == 0 )
  76. {
  77. msg.Printf( _( " <%s> does not appear to be a valid Kicad component library." ),
  78. GetChars( fn.GetFullPath() ) );
  79. wxMessageBox( msg, titleComponentLibErr, wxOK | wxICON_ERROR );
  80. fclose( source );
  81. return false;
  82. }
  83. if( strnicmp( Line, EnteteCmpMod, 11 ) != 0 ) /* old file version*/
  84. {
  85. msg.Printf( _( "<%s> is an old version component file." ) );
  86. wxMessageBox( msg, titleComponentLibErr, wxOK | wxICON_ERROR );
  87. fclose( source );
  88. return false;
  89. }
  90. while( !eof && fgets( Line, 79, source ) != 0 )
  91. {
  92. if( strnicmp( Line, "EndListe", 8 ) == 0 )
  93. break;
  94. /* Search the beginning of the component description. */
  95. if( strnicmp( Line, "BeginCmp", 8 ) != 0 )
  96. continue;
  97. timestamp.Empty();
  98. valeur.Empty();
  99. ilib.Empty();
  100. namecmp.Empty();
  101. read_cmp_data = TRUE;
  102. while( !eof && read_cmp_data )
  103. {
  104. if( fgets( Line, 1024, source ) == 0 )
  105. {
  106. eof = TRUE;
  107. break;
  108. }
  109. if( strnicmp( Line, "EndCmp", 6 ) == 0 )
  110. {
  111. read_cmp_data = TRUE;
  112. break;
  113. }
  114. ident = strtok( Line, "=;\n\r" );
  115. data = strtok( NULL, ";\n\r" );
  116. if( strnicmp( ident, "TimeStamp", 9 ) == 0 )
  117. {
  118. timestamp = CONV_FROM_UTF8( data );
  119. timestamp.Trim( TRUE );
  120. timestamp.Trim( FALSE );
  121. continue;
  122. }
  123. if( strnicmp( ident, "Reference", 9 ) == 0 )
  124. {
  125. namecmp = CONV_FROM_UTF8( data );
  126. namecmp.Trim( TRUE );
  127. namecmp.Trim( FALSE );
  128. continue;
  129. }
  130. if( strnicmp( ident, "ValeurCmp", 9 ) == 0 )
  131. {
  132. valeur = CONV_FROM_UTF8( data );
  133. valeur.Trim( TRUE );
  134. valeur.Trim( FALSE );
  135. continue;
  136. }
  137. if( strnicmp( ident, "IdModule", 8 ) == 0 )
  138. {
  139. ilib = CONV_FROM_UTF8( data );
  140. ilib.Trim( TRUE );
  141. ilib.Trim( FALSE );
  142. continue;
  143. }
  144. } /* End reading component description. */
  145. /* Search corresponding component and NetList
  146. * Update its parameters. */
  147. BOOST_FOREACH( COMPONENT& component, list )
  148. {
  149. if( namecmp != component.m_Reference )
  150. continue;
  151. /* Copy the name of the corresponding module. */
  152. component.m_Module = ilib;
  153. }
  154. }
  155. fclose( source );
  156. return true;
  157. }