home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d502 / cells.lha / CELLS / CELLSSource.lzh / cWrite.c < prev    next >
C/C++ Source or Header  |  1991-04-20  |  7KB  |  256 lines

  1. /*
  2.  *  CELLS       An Implementation of the WireWorld cellular automata
  3.  *              as described in Scientific American, Jan 1990.
  4.  *
  5.  *              Copyright 1990 by Davide P. Cervone.
  6.  *  You may use this code, provided this copyright notice is kept intact.
  7.  *  See the CELLS.HELP file for complete information on distribution conditions.
  8.  */
  9.  
  10. /*
  11.  *  File:  cWrite.c         Handles writing of Circuit and Library files.
  12.  */
  13.  
  14.  
  15. #include "cGadget.h"
  16. #include "cRequest.h"
  17. #include "cParts.h"
  18. #include <stdio.h>
  19.  
  20. extern char FileName[MAXFNAME];     /* the name in the File Requester */
  21.  
  22. static FILE *theFile;               /* the current file being written */
  23. static int noError;                 /* TRUE if no errors while writing */
  24. static char Line[256];              /* output buffer */
  25.  
  26. static short BoundsX,BoundsY,BoundsW,BoundsH;       /* size of circuit */
  27.  
  28. /* the characters to use for each cell state number */
  29. /*  BLANK = '. '  TAIL = '=='  HEAD = '##'  WIRE = '[]' */
  30.  
  31. static char *CellSymbol[] = {"  ","  ","  ",". ","==","##","  ","[]"};
  32.  
  33.  
  34. /*
  35.  *  Write()
  36.  *
  37.  *  If no errors have occured,
  38.  *    Format the outpu string and send it to the file
  39.  *    If something went wrong, give an error, and note that one occurred
  40.  */
  41.  
  42. void Write(s,x1,x2,x3,x4,x5)
  43. char *s,*x1,*x2,*x3,*x4,*x5;
  44. {
  45.    char Line[132];
  46.  
  47.    if (noError)
  48.    {
  49.       sprintf(Line,s,x1,x2,x3,x4,x5);
  50.       if (fputs(Line,theFile))
  51.       {
  52.          DosError("Write","Line");
  53.          noError = FALSE;
  54.       }
  55.    }
  56. }
  57.  
  58.  
  59. /*
  60.  *  WriteGrid()
  61.  *
  62.  *  For each cell in the specified patch
  63.  *    Set the next characters in the output line to the proper symbols
  64.  *    Trim any trailing spaces on each line, and add a newline at the end
  65.  *    Send the line to the file, and check for errors
  66.  */
  67.  
  68. static void WriteGrid(Grid,x,y,w,h,GridWidth)
  69. UBYTE Grid[];
  70. short x,y,w,h;
  71. short GridWidth;
  72. {
  73.    register short i,j;
  74.    register short X,Y;
  75.    char Line[256];
  76.    
  77.    w += x; h += y;
  78.    for (Y=y; Y<h && noError; Y++)
  79.    {
  80.       for (X=x,i=X+GridWidth*Y,j=0; X<w; X++,i++)
  81.       {
  82.          Line[j++] = CellSymbol[Grid[i]][0];
  83.          Line[j++] = CellSymbol[Grid[i]][1];
  84.       }
  85.       while (j>0 && Line[--j] == ' ');
  86.       Line[++j] = '\n'; Line[++j] = 0;
  87.       if (fputs(Line,theFile))
  88.       {
  89.          DosError("Write","Line");
  90.          noError = FALSE;
  91.       }
  92.    }
  93. }
  94.  
  95.  
  96. /*
  97.  *  WriteCurGen()
  98.  *
  99.  *  Copy the contents of the current generation into the scratch array
  100.  *  Clear any blank cells to zeros (won't be written to file)
  101.  *  Write the contents of the grid to the file
  102.  */
  103.  
  104. static void WriteCurGen()
  105. {
  106.    register short i;
  107.  
  108.    CopyGrid(CurGen,NewGen);
  109.    for (i=0; i<ARRAYSIZE; i++) if (NewGen[i] == BLANK) NewGen[i] = 0;
  110.    WriteGrid(NewGen,BoundsX,BoundsY,BoundsW,BoundsH,MAXGRIDW);
  111. }
  112.  
  113.  
  114. /*
  115.  *  WriteParts()
  116.  *
  117.  *  For each part in the Parts List
  118.  *    write the part name and its defining data
  119.  *    go on to the nect part
  120.  */
  121.  
  122. static void WriteParts()
  123. {
  124.    PART *thePart = PartsList.FirstPart;
  125.    
  126.    while (thePart)
  127.    {
  128.       Write("\n\nPart %s\n\n",thePart->Name);
  129.       WriteGrid(thePart->Data,0,0,thePart->w,thePart->h,thePart->w);
  130.       thePart = thePart->Next;
  131.    }
  132. }
  133.  
  134. /*
  135.  *  WriteLibraries()
  136.  *
  137.  *  If there are libraries other than the parts list include a litle space
  138.  *    For each library in the list
  139.  *      if the library is not the parts list write it to the file
  140.  *      go on to the next library
  141.  */
  142.  
  143. static void WriteLibraries()
  144. {
  145.    LIBRARY *theLibrary = FirstLibrary;
  146.    
  147.    if (FirstLibrary->Next) Write("\n\n"); 
  148.    while (theLibrary)
  149.    {
  150.       if (theLibrary != &PartsList)
  151.          Write("Include %s\n",theLibrary->Name);
  152.       theLibrary = theLibrary->Next;
  153.    }
  154. }
  155.  
  156.  
  157. /*
  158.  *  CloseFile()
  159.  *
  160.  *  close the file, and clear the file pointer
  161.  *  if an error occured while writing the file
  162.  *    ask if the user want to keep the file on disk any way
  163.  *    if no, try to delete the file.
  164.  */
  165.  
  166. static void CloseFile(Name)
  167. char *Name;
  168. {
  169.    fclose(theFile);
  170.    theFile = NULL;
  171.    if (noError == FALSE)
  172.    {
  173.       if (DoQuestion("File not Completely Written: Delete it?"))
  174.          if (DeleteFile(Name) == FALSE) DosError("Delete","File");
  175.    }
  176. }
  177.  
  178.  
  179. /*
  180.  *  WriteFile()
  181.  *
  182.  *  If the specified file can be opened for writing
  183.  *    clear the error flag
  184.  *    if the save-as-library gadget was checked
  185.  *      put up a message about writing a library
  186.  *      indicate that the file is a library
  187.  *      write the parts list to the library
  188.  *    otherwise if a selection is in effect
  189.  *      put up a message about writing a selection
  190.  *      get the circuits size
  191.  *      write the pertinent data about the seleted part
  192.  *      write its data
  193.  *    otherwise
  194.  *      put up a message about writing a circuit
  195.  *      get the size of the whole circuit
  196.  *      write the pertinent data about the circuit
  197.  *      write the circuit to the file
  198.  *      write its parts list and library list
  199.  *    clear the info message
  200.  *    close the file
  201.  *    if no error occured and we were writing a circuit
  202.  *      set the UNDO and RESET arrays
  203.  *      save the circuit's name and clear the changes flag
  204.  *  Otherwise (can't open the file)
  205.  *    report the error message
  206.  *  Put the SAVE button back to normal
  207.  */
  208.  
  209. void WriteFile(Name,Check)
  210. char *Name;
  211. int Check;
  212. {
  213.    if (theFile = fopen(Name,"w"))
  214.    {
  215.       noError = TRUE;
  216.       if (Check)
  217.       {
  218.          DoInfoMessage("Writing Library '%s'",Name);
  219.          Write("Library %s\n",Name);
  220.          WriteParts();
  221.       } else if (SelectType) {
  222.          DoInfoMessage("Writing Selection '%s'",Name);
  223.          GetBounds(NewGen,&BoundsX,&BoundsY,&BoundsW,&BoundsH,0);
  224.          Write("Circuit %s\n",Name);
  225.          Write("CellSize %d\n",CellSize);
  226.          Write("Origin %d %d\n",BoundsX,BoundsY);
  227.          Write("View %d %d\n",GridX,GridY);
  228.          Write("\n");
  229.          WriteGrid(NewGen,BoundsX,BoundsY,BoundsW,BoundsH,MAXGRIDW);
  230.       } else {
  231.          DoInfoMessage("Writing Circuit '%s'",Name);
  232.          GetBounds(CurGen,&BoundsX,&BoundsY,&BoundsW,&BoundsH,BLANK);
  233.          Write("Circuit %s\n",Name);
  234.          Write("CellSize %d\n",CellSize);
  235.          Write("Origin %d %d\n",BoundsX,BoundsY);
  236.          Write("View %d %d\n",GridX,GridY);
  237.          Write("\n");
  238.          WriteCurGen();
  239.          WriteParts();
  240.          WriteLibraries();
  241.       }
  242.       ClearInfoMessage();
  243.       CloseFile(Name);
  244.       if (noError && Check == 0 && SelectType == SEL_NONE)
  245.       {
  246.          CopyGrid(CurGen,UndoArray);
  247.          CopyGrid(CurGen,ResetArray);
  248.          strcpy(FileName,Name);
  249.          Changed = FALSE;
  250.       }
  251.    } else {
  252.       DosError("Write","File");
  253.    }
  254.    InvertGadget(&cGadget[ID_SAVE]);
  255. }
  256.