home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / GRAPHICS / MISC / EEDPR23S.ZIP / EELOAD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-03  |  7.1 KB  |  238 lines

  1. /*****************************************************************************
  2. *   Module to load EEDraw files.                         *
  3. *                                         *
  4. * Written by:  Gershon Elber            IBM PC Ver 1.0,    Oct. 1989    *
  5. *****************************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. #ifdef __MSDOS__
  11. #include <stdlib.h>
  12. #include <conio.h>
  13. #include <dos.h>
  14. #include <alloc.h>
  15. #endif /* __MSDOS__ */
  16.  
  17. #include "program.h"
  18. #include "eelibs.h"
  19. #include "eeredraw.h"
  20. #include "eeload.h"
  21. #include "virtrstr.h"
  22.  
  23. #define EEDRAW_FILE_STAMP "EEDraw"
  24. #define NULL_STRING "_NONAME_"
  25.  
  26. /*****************************************************************************
  27. * Routine to load an EEDraw file.                         *
  28. * Save in LoadedFileName the name of the file loaded to reset it to an       *
  29.  * empty string if none loaded.                             *
  30. *****************************************************************************/
  31. void LoadEEFile(FILE *f)
  32. {
  33.     char Line[LINE_LEN], Name1[LINE_LEN_SHORT], Name2[LINE_LEN_SHORT],
  34.              Char1[LINE_LEN_SHORT], Char2[LINE_LEN_SHORT];
  35.     int i, LineCount = 1;
  36.     BooleanType Failed = FALSE;
  37.     DrawGenericStruct *Phead, *Pnext;
  38.     DrawLibItemStruct *LibItemStruct;
  39.     DrawConnectionStruct *ConnectionStruct;
  40.     DrawPolylineStruct *PolylineStruct;
  41.     DrawTextStruct *TextStruct;
  42.  
  43.     if (fgets(Line, LINE_LEN - 1, f) == NULL ||
  44.     strncmp(Line, EEDRAW_FILE_STAMP, sizeof(EEDRAW_FILE_STAMP) - 1) != 0) {
  45.     fclose(f);
  46.     FatalError("Given file is NOT EEDraw file");
  47.     }
  48.  
  49.     if (fgets(Line, LINE_LEN - 1, f) == NULL ||
  50.     strncmp(Line, "LIBS:", 5) != 0) {
  51.     fclose(f);
  52.     FatalError("Given file is NOT EEDraw file");
  53.     }
  54.     else
  55.     LoadLibraries(&Line[5]);
  56.  
  57.     while (!feof(f) &&
  58.        fgets(Line, LINE_LEN - 1, f) != NULL) {
  59.     LineCount++;
  60.  
  61.     switch(Line[0]) {
  62.         case 'L':                      /* Its a library item. */
  63.         LibItemStruct = (DrawLibItemStruct *)
  64.                         MyMalloc(sizeof(DrawLibItemStruct));
  65.         LibItemStruct -> StructType = DRAW_LIB_ITEM_STRUCT_TYPE;
  66.         if (sscanf(&Line[1], "%s %s %s %s %d %d %d %d",
  67.                Name1, Name2,
  68.                Char1, Char2,
  69.                &LibItemStruct -> ChipNameX,
  70.                &LibItemStruct -> ChipNameY,
  71.                &LibItemStruct -> PartNameX,
  72.                &LibItemStruct -> PartNameY) != 8 ||
  73.             (Char1[0] != 'V' && Char1[0] != 'H') ||
  74.             (Char2[0] != 'V' && Char2[0] != 'H')) {
  75.             sprintf(Line,
  76.             "EEDraw file lib item struct error at line %d, aborted",
  77.                                 LineCount);
  78.             Failed = TRUE;
  79.             break;
  80.         }
  81.         LibItemStruct -> ChipNameOrient =
  82.             Char1[0] == 'V' ? TEXT_ORIENT_VERT : TEXT_ORIENT_HORIZ;
  83.         LibItemStruct -> PartNameOrient =
  84.             Char2[0] == 'V' ? TEXT_ORIENT_VERT : TEXT_ORIENT_HORIZ;
  85.  
  86.         if (strcmp(Name1, NULL_STRING) != 0) {
  87.             for (i = 0; i < strlen(Name1); i++)
  88.             if (Name1[i] == '~') Name1[i] = ' ';
  89.             LibItemStruct -> ChipName = strdup(Name1);
  90.         }
  91.         else {
  92.             LibItemStruct -> ChipName = NULL;
  93.             LibItemStruct -> ChipNameOrient = TEXT_ORIENT_NON;
  94.         }
  95.  
  96.         if (strcmp(Name2, NULL_STRING) != 0) {
  97.             for (i = 0; i < strlen(Name2); i++)
  98.             if (Name2[i] == '~') Name2[i] = ' ';
  99.             LibItemStruct -> PartName = strdup(Name2);
  100.         }
  101.         else {
  102.             LibItemStruct -> PartName = NULL;
  103.             LibItemStruct -> PartNameOrient = TEXT_ORIENT_NON;
  104.         }
  105.  
  106.         LineCount++;
  107.         if (!Failed &&
  108.             (fgets(Line, LINE_LEN - 1, f) == NULL ||
  109.              sscanf(Line, "%d %d %d %d %d %d %d",
  110.                 &LibItemStruct -> Multi,
  111.                 &LibItemStruct -> PosX,
  112.                 &LibItemStruct -> PosY,
  113.                 &LibItemStruct -> BBoxMinX,
  114.                 &LibItemStruct -> BBoxMinY,
  115.                 &LibItemStruct -> BBoxMaxX,
  116.                 &LibItemStruct -> BBoxMaxY) != 7)) {
  117.             Failed = TRUE;
  118.             break;
  119.         }
  120.  
  121.         LineCount++;
  122.         if (!Failed &&
  123.             (fgets(Line, LINE_LEN - 1, f) == NULL ||
  124.              sscanf(Line, "%d %d %d %d",
  125.                 &LibItemStruct -> Transform[0][0],
  126.                 &LibItemStruct -> Transform[0][1],
  127.                 &LibItemStruct -> Transform[1][0],
  128.                 &LibItemStruct -> Transform[1][1]) != 4)) {
  129.             Failed = TRUE;
  130.             break;
  131.         }
  132.  
  133.         if (!Failed) {
  134.             LibItemStruct -> Pnext = EEDrawList;
  135.             EEDrawList = (DrawGenericStruct *) LibItemStruct;
  136.         }
  137.         break;
  138.         case 'P':                     /* Its a polyline item. */
  139.         PolylineStruct = (DrawPolylineStruct *)
  140.                     MyMalloc(sizeof(DrawPolylineStruct));
  141.         PolylineStruct -> StructType = DRAW_POLYLINE_STRUCT_TYPE;
  142.         if (sscanf(&Line[1], "%s %d",
  143.                Name1,
  144.                &PolylineStruct -> NumOfPoints) != 2 ||
  145.             (Name1[0] != 'B' && Name1[0] != 'L')) {
  146.             sprintf(Line,
  147.             "EEDraw file polyline struct error at line %d, aborted",
  148.                                 LineCount);
  149.             Failed = TRUE;
  150.             break;
  151.         }
  152.         PolylineStruct -> Width =
  153.             Name1[0] == 'B' ? THICK_WIDTH : NORM_WIDTH;
  154.         PolylineStruct -> Points = (int *) MyMalloc(sizeof(int) * 2 *
  155.                         PolylineStruct -> NumOfPoints);
  156.         for (i = 0; i < PolylineStruct -> NumOfPoints; i++) {
  157.             LineCount++;
  158.             if (fgets(Line, LINE_LEN - 1, f) == NULL ||
  159.             sscanf(Line, "%d %d", &PolylineStruct -> Points[i*2],
  160.                           &PolylineStruct -> Points[i*2+1])
  161.                                 != 2) {
  162.             sprintf(Line,
  163.              "EEDraw file polyline struct error at line %d, aborted",
  164.                                 LineCount);
  165.             Failed = TRUE;
  166.             MyFree((VoidPtr) PolylineStruct -> Points);
  167.             break;
  168.             }
  169.         }
  170.  
  171.         if (!Failed) {
  172.             PolylineStruct -> Pnext = EEDrawList;
  173.             EEDrawList = (DrawGenericStruct *) PolylineStruct;
  174.         }
  175.         break;
  176.         case 'C':                   /* Its a connection item. */
  177.         ConnectionStruct = (DrawConnectionStruct *)
  178.                     MyMalloc(sizeof(DrawConnectionStruct));
  179.         ConnectionStruct -> StructType = DRAW_CONNECTION_STRUCT_TYPE;
  180.         if (sscanf(&Line[1], "%d %d",
  181.                &ConnectionStruct -> PosX,
  182.                &ConnectionStruct -> PosY) != 2) {
  183.             sprintf(Line,
  184.               "EEDraw file connection struct error at line %d, aborted",
  185.                                 LineCount);
  186.             Failed = TRUE;
  187.         }
  188.         else {
  189.             ConnectionStruct -> Pnext = EEDrawList;
  190.             EEDrawList = (DrawGenericStruct *) ConnectionStruct;
  191.         }
  192.         break;
  193.         case 'T':                         /* Its a text item. */
  194.         TextStruct = (DrawTextStruct *) MyMalloc(sizeof(DrawTextStruct));
  195.         TextStruct -> StructType = DRAW_TEXT_STRUCT_TYPE;
  196.         if ((((i = sscanf(&Line[1], "%d %d %d %d",
  197.                        &TextStruct -> PosX,
  198.                   &TextStruct -> PosY,
  199.                   &TextStruct -> Orient,
  200.                   &TextStruct -> Scale)) != 3) && i != 4) ||
  201.             fgets(Line, LINE_LEN - 1, f) == NULL) {
  202.             sprintf(Line,
  203.               "EEDraw file text struct error at line %d, aborted",
  204.                                 LineCount);
  205.             Failed = TRUE;
  206.         }
  207.         else {
  208.             TextStruct -> Text = strdup(strtok(Line, "\n\r"));
  209.             TextStruct -> Pnext = EEDrawList;
  210.             EEDrawList = (DrawGenericStruct *) TextStruct;
  211.         }
  212.         if (i == 3) TextStruct -> Scale = 1;  /* No scale specified. */
  213.         break;
  214.         default:
  215.         Failed = FALSE;
  216.         sprintf(Line, "EEDraw file undef structdef at line %d, aborted",
  217.                                 LineCount);
  218.         break;
  219.     }
  220.  
  221.     if (Failed) {
  222.         FatalError(Line);
  223.     }
  224.     }
  225.  
  226.     /* EEDrawList was constructed in reverse order - reverse it back: */
  227.     Phead = NULL;
  228.     while (EEDrawList) {
  229.     Pnext = EEDrawList;
  230.     EEDrawList = EEDrawList -> Pnext;
  231.     Pnext -> Pnext = Phead;
  232.     Phead = Pnext;
  233.     }
  234.     EEDrawList = Phead;
  235.  
  236.     fclose(f);
  237. }
  238.