home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 June / CHIPCD_6_2000.iso / software / cad / t425l1e / t425l1e.exe / _002320_ / IMPORT_.C
C/C++ Source or Header  |  1997-06-14  |  14KB  |  414 lines

  1. //------------------------------------------------------------------------------------------------------
  2. // Name       : import_.c
  3. // Date       : 05.03.1997     Author : SM           System : Win32
  4. //------------------------------------------------------------------------------------------------------
  5. // This file contains the language-independent implementation of the module IMPORT_.DLL. All texts and
  6. // resources that are language-dependent are located in an additional IMPORT.DLL, whose sources can be
  7. // found in the subdirectories \E (for English) and \D (for German).
  8. //------------------------------------------------------------------------------------------------------
  9.  
  10. #define TOSO_MODULE_VERSION     420             // Required version of Toso Interface
  11.  
  12. //------------------------------------------------------------------------------------------------------
  13.  
  14. #define USER_DATA_ID            "1.00-1996-11-20"
  15. #define TXT_ID_HEADER           "TommySoftware TXT 1.10"
  16.  
  17. //------------------------------------------------------------------------------------------------------
  18.  
  19. #include        "windows.h"
  20. #include        "windowsx.h"
  21. #include        "stdlib.h"
  22. #include        "stdio.h"
  23. #include        "math.h"
  24.  
  25. #include        "e:\release4\tosoapi4.h"        // Toso Interface Definitions
  26. #include        "dialog.h"
  27.  
  28. //------------------------------------------------------------------------------------------------------
  29.  
  30. typedef struct {
  31.   STR32         TimeStamp;
  32.  
  33.   FILENAME      FileName;
  34. } INF_HEADER;
  35.  
  36. //------ Language-dependent texts in IMPORT.DLL --------------------------------------------------------
  37.  
  38. DLL_IMPORT LPSTR
  39.         eStartUpText    [],
  40.         eDialogText     [],
  41.         eMessageText    [];
  42.  
  43. //------------------------------------------------------------------------------------------------------
  44.  
  45. static  HINSTANCE       hInstDLL,               // Instance handle of the main DLL
  46.                         hLanguage,              // Instance handle of the language DLL
  47.                         hGlobalInst;            // Instance handle of the serving application
  48. static  HWND            hGlobalWnd;             // Main window handle of the serving application
  49.  
  50. //------------------------------------------------------------------------------------------------------
  51.  
  52. static  HBITMAP         hBitmap;
  53. static  INF_HEADER      INFHeader;
  54. static  int             gError;                 // Current error status ( 0 = OK )
  55.  
  56. //------------------------------------------------------------------------------------------------------
  57.  
  58. BOOL ModuleLoadSettings( void )
  59. {
  60.   BOOL          Result = FALSE;
  61.  
  62.   if( TosoProfileReadKeyOpen( "IMPORT", FALSE ) ) {
  63.     if( TosoProfileReadData( "Init", (LPBYTE) &INFHeader, sizeof( INFHeader ) ) )
  64.       Result = TRUE;
  65.     TosoProfileReadKeyClose();
  66.  
  67.     INFHeader.TimeStamp[31] = 0x00;
  68.     if( lstrcmp( INFHeader.TimeStamp, USER_DATA_ID ) )
  69.       Result = FALSE;
  70.   }
  71.  
  72.   if( !Result ) {
  73.     lstrcpy( INFHeader.FileName, eDialogText[1] );
  74.   }
  75.   return( Result );
  76. }
  77.  
  78. //------------------------------------------------------------------------------------------------------
  79.  
  80. BOOL ModuleSaveSettings( void )
  81. {
  82.   BOOL          Result = FALSE;
  83.  
  84.   if( TosoProfileWriteKeyOpen( "IMPORT", FALSE ) ) {
  85.     lstrcpy( INFHeader.TimeStamp, USER_DATA_ID );
  86.     if( TosoProfileWriteData( "Init", (LPBYTE) &INFHeader, sizeof( INFHeader ) ) )
  87.       Result = TRUE;
  88.     TosoProfileWriteKeyClose();
  89.   }
  90.   return( Result );
  91. }
  92.  
  93. //------------------------------------------------------------------------------------------------------
  94. // This procedure reads a single coordinate pair from the current file. If it finds the last line of the
  95. // file (indicated by the DB_END data block identifier), it returns FALSE without setting gError to a
  96. // non-zero value.
  97.  
  98. BOOL ModuleReadCoordinate( double* x, double* y )
  99. {
  100.   DUMMYSTR      Text1, Text2;
  101.   short         Dummy;
  102.  
  103.   if( gError )
  104.     return( FALSE );
  105.  
  106.   wsprintf( Text1, eDialogText[3], TosoFileReadCurrentLine() );
  107.   wsprintf( Text2, eDialogText[4], ( TosoFileReadCurrentSize() + 1023 ) / 1024 );
  108.   TosoDialogUpdateProgress( Text1, Text2, TosoFileReadCurrentSize(), TosoFileReadTotalSize() );
  109.  
  110.   if( TosoDialogIsCanceled() ) {
  111.     gError = 999;
  112.     return( FALSE );
  113.   }
  114.  
  115.   TosoFileReadShort( &Dummy );
  116.   if( Dummy == DB_END )
  117.     return( FALSE );
  118.  
  119.   TosoFileReadCommaDouble( x );
  120.   TosoFileReadCommaDouble( y );
  121.   TosoFileReadSemi();
  122.  
  123.   if( TosoFileReadError() ) {
  124.     gError = 2;
  125.     return( FALSE );
  126.   }
  127.  
  128.   return( TRUE );
  129. }
  130.  
  131. //------------------------------------------------------------------------------------------------------
  132. // This procedure performs the import. It reads all coordinate pairs from the import file and insert a
  133. // marking for each coordinate pair.
  134. // In addition, it initializes and display a progress indicator window to inform the user about the
  135. // current progress and to allow him to cancel the import. Since the final file size is already known
  136. // in advance, the progress indicator does include a percent bar.
  137.  
  138. void ModuleImport( HANDLE FileHandle, const LPSTR FileName )
  139. {
  140.   FILENAME      FileName2;
  141.   DUMMYSTR      DummyStr;
  142.   int           Count;
  143.   double        x, y;
  144.  
  145.   gError = 0;
  146.  
  147.   if( !TosoFileReadInitDisk( FileHandle ) )
  148.     return;
  149.  
  150.   if( !TosoFileReadLine( DummyStr, 255 ) ) {
  151.     gError = 3;
  152.     TosoFileReadExit();
  153.     return;
  154.   }
  155.  
  156.   TosoFileSplitName( FileName, NULL, FileName2 );
  157.   wsprintf( DummyStr, eDialogText[2], FileName2 );
  158.   TosoDialogShowProgress( eDialogText[0], DummyStr, TRUE );
  159.  
  160.   if( ModuleReadCoordinate( &x, &y ) ) {
  161.     Count = 0;
  162.  
  163.     do {
  164.       if( Count == 0 )
  165.         TosoObjectOpen( OBJ_MARK );
  166.  
  167.       TosoObjectAddPoint( DB_POINT_MARK, x, y );
  168.       Count++;
  169.  
  170.       if( Count >= POINTS_PER_OBJECT ) {
  171.         if( !TosoObjectFastInsert() ) {
  172.           gError = 998;
  173.           goto _stop;
  174.         }
  175.         Count = 0;
  176.       }
  177.     } while( ModuleReadCoordinate( &x, &y ) );
  178.  
  179.     if( Count > 0 )
  180.       if( !TosoObjectFastInsert() )
  181.         gError = 998;
  182.  
  183.     TosoFileReadSemi();
  184.   }
  185.  
  186. _stop:
  187.   TosoFileReadExit();
  188.   TosoDialogHideProgress();
  189. }
  190.  
  191. //------------------------------------------------------------------------------------------------------
  192. // This DLL entry procedure must exist in any DLL to be used in Win32. Since our DLL does all necessary
  193. // initialization in its TosoModuleInit() procedure, this procedure is quite empty.
  194.  
  195. BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD Reason, LPVOID Dummy )
  196. {
  197.   switch( Reason ) {
  198.     case DLL_PROCESS_ATTACH:
  199.       hInstDLL = hInstance;
  200.       break;
  201.  
  202.     case DLL_PROCESS_DETACH:
  203.       hInstDLL = NULL;
  204.       break;
  205.   }
  206.   return( TRUE );
  207. }
  208.  
  209. //------------------------------------------------------------------------------------------------------
  210. // This procedure is called when the module is loaded by the serving application. Its main tasks are:
  211. // - Checking whether it is compatible with the given InterfaceVersion
  212. // - Checking whether it is licensed to the given serial number (optional)
  213. // - Storing of the serving application's instance and main windows handle for further use
  214. // - Loading of the language-dependent library
  215. // - Filling in the module ID structure whose address is passed in ModuleID
  216. // - Loading of options from the registry database
  217. // - Loading profiles
  218. // - Allocating any static memory required
  219.  
  220. DLL_EXPORT int TosoModuleInit( const LPSTR SerialNumber, HINSTANCE hMainInst, HWND hMainWnd,
  221.                                                          int InterfaceVersion, MODULE_ID* ModuleID )
  222. {
  223.   if( InterfaceVersion < TOSO_MODULE_VERSION ) {
  224.     MessageBox( hMainWnd, eMessageText[0], eDialogText[0], MB_OK );
  225.     return( 0 );
  226.   }
  227.   hGlobalInst = hMainInst;
  228.   hGlobalWnd  = hMainWnd;
  229.  
  230.   hLanguage = LoadLibrary( "IMPORT.DLL" );
  231.   hBitmap = LoadBitmap( hLanguage, "IDB_COMMAND" );
  232.  
  233.   ModuleID->OwnerID    = DB_OWNER_TOSO;
  234.   ModuleID->ModuleID   = 0x1000;
  235.   ModuleID->ModuleCTRL = MODULECTRL_ALL;
  236.  
  237.   ModuleID->ModuleProc.InputPointInitProc = (TOSOINPUTPOINTINIT_PROC) NULL;
  238.   ModuleID->ModuleProc.InputPointMoveProc = (TOSOINPUTPOINTMOVE_PROC) NULL;
  239.   ModuleID->ModuleProc.InputPointExitProc = (TOSOINPUTPOINTEXIT_PROC) NULL;
  240.   ModuleID->ModuleProc.InputDisplayProc   = (TOSOINPUTDISPLAY_PROC)   NULL;
  241.   ModuleID->ModuleProc.InputParameterProc = (TOSOINPUTPARAMETER_PROC) NULL;
  242.   ModuleID->ModuleProc.InputCancelProc    = (TOSOINPUTCANCEL_PROC)    NULL;
  243.   ModuleID->ModuleProc.InputFinishProc    = (TOSOINPUTFINISH_PROC)    NULL;
  244.  
  245.   ModuleID->ModuleData.Type = MODULETYPE_IMPORT;
  246.  
  247.   ModuleID->ModuleData.InputData.CommandMode = COMMAND_DIRECT;
  248.   ModuleID->ModuleData.MenuData.MenuEntry   = eStartUpText[1];
  249.   ModuleID->ModuleData.MenuData.Description = eStartUpText[2];
  250.  
  251.   ModuleID->ModuleData.IconHandle  = hBitmap;
  252.   ModuleID->ModuleData.IconXOffset = 0;
  253.   ModuleID->ModuleData.IconYOffset = 0;
  254.   ModuleID->ModuleData.IconMode    = 0;
  255.  
  256.   ModuleID->CommandData = NULL;
  257.   ModuleID->VendorData  = NULL;
  258.  
  259.   ModuleID->FileType      = eStartUpText[4];
  260.   ModuleID->FileExtension = eStartUpText[5];
  261.   ModuleID->FileDefault   = eStartUpText[6];
  262.  
  263.   ModuleLoadSettings();
  264.  
  265.   return( TOSO_MODULE_VERSION );
  266. }
  267.  
  268. //------------------------------------------------------------------------------------------------------
  269. // This procedure is called when the module is removed by the serving application. Its main tasks are:
  270. // - Checking whether anything is to be saved. If so, it should display a message information the user
  271. //   about it and allowing him to save those changes.
  272. // - Freeing of all statically allocated memory.
  273. // If this procedure return FALSE, the serving application will not be able to terminate. So please, do
  274. // only return FALSE if shutting down the module now would severely damage or destroy user data.
  275.  
  276. DLL_EXPORT BOOL TosoModuleExit( void )
  277. {
  278.   ModuleSaveSettings();
  279.  
  280.   DeleteBitmap( hBitmap );
  281.  
  282.   return( TRUE );
  283. }
  284.  
  285. //------------------------------------------------------------------------------------------------------
  286. // This procedure is called when the user wants to load a non-TVG drawing using the File>Open command.
  287. // For an import filter, its task is to check whether he knows this type of file, and to load it, if
  288. // possible.
  289.  
  290. DLL_EXPORT int TosoModuleImport( const LPSTR FileName, BOOL Merge )
  291. {
  292.   HANDLE                FileHandle;
  293.   DUMMYSTR              DummyStr;
  294.   BOOL                  Found = FALSE;
  295.  
  296.   SetCursor( LoadCursor( NULL, IDC_WAIT ) );
  297.  
  298. //
  299. // Step 1: Check whether this file is a valid file containing coordinate pairs.
  300. //
  301.  
  302.   if( TosoFileOpen( &FileHandle, FileName ) ) {
  303.     if( !TosoCreationStart() ) {
  304.       TosoFileClose( FileHandle );
  305.       return( IMPORT_RETURN_ERROR );
  306.     }
  307.  
  308.     if( TosoFileRead( FileHandle, DummyStr, TVG_ID_LENGTH + 1 ) == TVG_ID_LENGTH + 1 ) {
  309.       DummyStr[TVG_ID_LENGTH] = '\0';
  310.       if( !lstrcmpi( DummyStr, TXT_ID_HEADER ) )
  311.         Found = TRUE;
  312.     }
  313.     TosoFileClose( FileHandle );
  314.   }  
  315.  
  316.   if( !Found )
  317.     return( IMPORT_RETURN_UNKNOWN );
  318.     
  319. //
  320. // Step 2: Try to import the coordinates stored in the file.
  321. //
  322.  
  323.   if( !TosoCreationStart() )
  324.     return( IMPORT_RETURN_ERROR );
  325.  
  326.   if( TosoFileOpen( &FileHandle, FileName ) ) {
  327.     if( Merge )
  328.       TosoUndoInitProcess();
  329.     else
  330.       TosoDrawingNewFile( TosoDrawingGetActive(), FALSE );
  331.     
  332.     ModuleImport( FileHandle, FileName );
  333.     
  334.     TosoFileClose( FileHandle );
  335.     switch( gError ) {
  336.       case 999:
  337.         MessageBox( hGlobalWnd, eMessageText[2], eDialogText[0], MB_OK );
  338.         goto _error;
  339.     
  340.       case 998:
  341.         MessageBox( hGlobalWnd, eMessageText[4], eDialogText[0], MB_OK );
  342.         goto _error;
  343.     
  344.       case 0:
  345.         break;
  346.     
  347.       default:
  348.         wsprintf( DummyStr, eMessageText[5], gError, TosoFileReadCurrentLine(), TosoFileReadCurrentSize() );
  349.         MessageBox( hGlobalWnd, DummyStr, eDialogText[0], MB_OK );
  350.         goto _error;
  351.     }
  352.  
  353.     if( Merge )
  354.       TosoUndoFinishProcess();
  355.  
  356. //  TosoUndoUpdateLinks();      <- Would only be necessary if blocks or instances had been modified!
  357.     TosoDrawWindowAll();
  358.  
  359.     TosoCreationEnd();
  360.     return( IMPORT_RETURN_OK );
  361.   }
  362.   return( IMPORT_RETURN_ERROR );
  363.  
  364. _error:
  365.   if( Merge )
  366.     TosoUndoCancelProcess();
  367.   else
  368.     TosoDrawingNewFile( TosoDrawingGetActive(), FALSE );
  369.  
  370.   TosoCreationEnd();
  371.   return( IMPORT_RETURN_ERROR );
  372. }
  373.  
  374. //------------------------------------------------------------------------------------------------------
  375. // This procedure is called when a module's command is chosen by the user. For an import filter, its
  376. // main tasks are:
  377. // - Parameter editing
  378. // - Help file display
  379.  
  380. DLL_EXPORT BOOL TosoModuleCommand( int CommandID, int ExecMode )
  381. {
  382.   switch( ExecMode ) {                  // Check which type of action is requested
  383.     case MODULEEXEC_HELP:               // Display the corresponding help topic
  384.       {
  385.         FILENAME    FileName;
  386.         TosoFileApplicationPath( "IMPORT.HLP", FileName );
  387.         WinHelp( hGlobalWnd, FileName, HELP_INDEX, 0 );
  388.       }
  389.       return( TRUE );
  390.  
  391.     case MODULEEXEC_USER:               // Edit the options
  392.       {
  393.         DUMMYSTR    Text1, Text2;
  394.         TOSO_GET_BUILD_DATE( Text2 );
  395.         wsprintf( Text1, eStartUpText[0], Text2 );
  396.         MessageBox( GetActiveWindow(), Text1, eDialogText[0], MB_OK );
  397.       }
  398.       return( TRUE );
  399.  
  400.     case MODULEEXEC_SYSTEM:             // Execute the command
  401.       // Nothing to do here...
  402.       break;
  403.  
  404.     case MODULEEXEC_GET_PROFILE:        // Read drawing-dependent data
  405.       // Nothing to do here...
  406.       break;
  407.  
  408.     case MODULEEXEC_SET_PROFILE:        // Write drawing-dependent data
  409.       // Nothing to do here...
  410.       break;
  411.   }
  412.   return( FALSE );
  413. }
  414.