home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / MakeWrite / MakeWrite Folder / MWIO.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  5.7 KB  |  301 lines  |  [TEXT/KAHL]

  1. /*
  2.  * MakeWrite File I/O  Routines.
  3.  */
  4.  
  5. # include    "MWFileStuff.h"
  6. # include    "MWMaca.h"
  7. # include    "MakeWrite.h"
  8.  
  9.  
  10. static SFReply    inFile;
  11. static SFReply    outFile;
  12. static Str255    fName;        /* map name and volume reference number */
  13. static short    vRefNum;
  14.  
  15.  
  16. /*
  17.  * Clear the current file name, and reset the title of the map
  18.  * window.  Obviously, the first call to this must occur
  19.  * after the map window is initialized.
  20.  */
  21.  
  22. void
  23. ClearMapName (void)
  24. {
  25.     CopyString ("\pUntitled", fName);
  26.     SetMapName (fName);
  27. }
  28.  
  29.  
  30. void
  31. SetMapName (StringPtr name)
  32. {
  33. Str255    s;
  34.  
  35.     CopyString ("\pMap Name:  ", s);
  36.     AppendString (name, s);
  37.     SetWTitle (mapWind, s);
  38. }
  39.  
  40.  
  41. /*
  42.  * Check a ConvSpec to see that it makes sense
  43.  */
  44.  
  45. static Boolean
  46. CheckMSpec (MapSpec *m)
  47. {
  48. short    errCnt = 0;
  49.  
  50.     if (FontIndex (m->font) < 0)
  51.     {
  52.         ErrWindMsge ("\pUnknown font number: ", m->font);
  53.         ++errCnt;
  54.     }
  55.     if (SizeIndex ((short) m->size) < 0)
  56.     {
  57.         ErrWindMsge ("\pUnknown size: ", (short) m->size);
  58.         ++errCnt;
  59.     }
  60.  
  61.     return (errCnt == 0);
  62. }
  63.  
  64.  
  65. /*
  66.  * Read paragraph style variables
  67.  */
  68.  
  69. static Boolean
  70. ReadParaStyle (short f, short version)
  71. {
  72.     if (version == 1)
  73.     {
  74.         InitParaStyle ();        /* use defaults */
  75.         return (true);
  76.     }
  77.     return (FileRead (f, (Ptr) ¶Style, (long) sizeof (ParaStyle))
  78.             && ReadString (f, paraMark)
  79.             && ReadString (f, pageMark)
  80.             && ReadString (f, periodStr)
  81.             && ReadString (f, quoteStr));
  82. }
  83.  
  84.  
  85. /*
  86.  * Read one map specification from a file.  First read the MapSpec
  87.  * structure, being careful not to clobber the mark string handle.
  88.  * Then read the string that gets put into the handle (first the
  89.  * length byte, then the rest of the string).
  90.  */
  91.  
  92. static Boolean
  93. ReadMSpec (short f, MapSpec *m)
  94. {
  95. StringHandle    hStr;
  96. Boolean            ok;
  97.  
  98.     hStr = m->mark;    /* save since clobbered by next read */
  99.     ok = FileRead (f, (Ptr) m, (long) sizeof (MapSpec));
  100.     m->mark = hStr;
  101.     if (ok)
  102.     {
  103.         HLock ((Handle) hStr);
  104.         ok = ReadString (f, *hStr);
  105.         HUnlock ((Handle) hStr);
  106.     }
  107.     return (ok);
  108. }
  109.  
  110.  
  111. /*
  112.  * Read map specifications from a file.  Duplicate or illegal
  113.  * specifications are not added.
  114.  *
  115.  * Assumes file is open and all mappings already cleared (for Open) or
  116.  * not (for Append).  If setParaInfo is true (Open), paragraph style
  117.  * info is set.  If it's false (Append), it's not set.
  118.  *
  119.  * Does not set undo variables or map-changed variables.
  120.  */
  121.  
  122. static void
  123. ReadMap (short f, Boolean setParaInfo)
  124. {
  125. MapSpec    mSpec;
  126. short    badCnt = 0;
  127. short    ver;
  128. short    i, nLines;
  129. Str255    str;
  130. Boolean    ok;
  131.  
  132.     InitMSpec (&mSpec);
  133.     ErrWindInit (inFile.fName);
  134.     if (!ReadInteger (f, &ver))
  135.     {
  136.         Message1 ("\pCan't read map file.");
  137.     }
  138.     else if (ver != mapVersion && ver != 1)
  139.     {
  140.         NumToString ((long) ver, str);
  141.         Message2 ("\pUnknown map file version:  ", str);
  142.     }
  143.     else if (ReadInteger (f, &nLines))
  144.     {
  145.         ok = true;
  146.         for (i = 0; i < nLines; ++i)
  147.         {
  148.             ok = false;
  149.             if (!ReadMSpec (f, &mSpec))
  150.                 break;
  151.             ok = true;
  152.     
  153.             /*
  154.              * Need to check legality here, since the file may have been
  155.              * created using the fonts from a font list other than the current
  156.              * one.
  157.              */
  158.     
  159.             if (CheckMSpec (&mSpec) == false)
  160.             {
  161.                 ++badCnt;
  162.                 continue;            /* its bad - don't use it */
  163.             }
  164.     
  165.             if (StatMSpec (&mSpec))
  166.             {
  167.                 ++badCnt;
  168.                 continue;            /* already exists - don't use dups */
  169.             }
  170.     
  171.             if (InsertMapping (&mSpec, mapList->nLines) == false)
  172.                 break;
  173.         }
  174.         if (ok && setParaInfo)
  175.             (void) ReadParaStyle (f, ver);
  176.     }
  177.  
  178.     TermMSpec (&mSpec);
  179.  
  180.     (void) FSClose (f);
  181.     ScrollToLine (mapList, 0);        /* force scroll to top */
  182.     SelectMapping (noLine);            /* but leave unselected */
  183.  
  184.     if (badCnt)
  185.     {
  186.         NumToString ((long) badCnt, str);
  187.         Message2 (str, "\p illegal or duplicate specifications were found and ignored");
  188.     }
  189. }
  190.  
  191.  
  192. /*
  193.  * Open a map.  Clobber the current map first, set the map name
  194.  * afterward.
  195.  */
  196.  
  197. Boolean
  198. OpenMap (void)
  199. {
  200. short    f;
  201.  
  202.     if (GetInputFile ("\pOpen", mwType, &inFile)
  203.         && OpenInputFile (&inFile, &f))
  204.     {
  205.         ClobberMap ();
  206.         ReadMap (f, true);
  207.         CopyString (inFile.fName, fName);
  208.         vRefNum = inFile.vRefNum;
  209.         SetMapName (fName);
  210.         return (true);
  211.     }
  212.     return (false);
  213. }
  214.  
  215.  
  216. /*
  217.  * Add a map to the current map.  Doesn't set map name afterward.
  218.  */
  219.  
  220. Boolean
  221. AddMap (void)
  222. {
  223. short    f;
  224.  
  225.     if (GetInputFile ("\pAdd", mwType, &inFile)
  226.         && OpenInputFile (&inFile, &f))
  227.     {
  228.         ReadMap (f, false);
  229.         return (true);
  230.     }
  231.     return (false);
  232. }
  233.  
  234.  
  235. /*
  236.  * Save map to file.  askForName is true if should ask for name,
  237.  * otherwise use current name.  If name is "Untitled", ask for a name
  238.  * anyway.  Sets the current name if it wasn't already set.
  239.  *
  240.  * The file is deleted if not written properly.
  241.  */
  242.  
  243. Boolean
  244. SaveMap (Boolean askForName)
  245. {
  246. short    f;
  247. short    i;
  248. OSErr    result;
  249. long    pos;
  250. Boolean    ok = false;
  251. StringHandle    hStr;
  252.  
  253.     if (GetOutputFile (askForName, fName, vRefNum, &outFile) == false)
  254.         return (false);
  255.     if (OpenOutputFile (&outFile, mwCreator, mwType, &f) == false)
  256.         return (false);
  257.  
  258.     if (WriteInteger (f, (short) mapVersion)
  259.         && WriteInteger (f, (short) mapList->nLines))
  260.     {
  261.         ok = true;
  262.         for (i = 0; i < mapList->nLines; ++i)
  263.         {
  264.             ok = FileWrite (f, (Ptr) &mapSpec[i], (long) sizeof (MapSpec));
  265.             if (!ok)
  266.                 break;
  267.             hStr = mapSpec[i].mark;
  268.             HLock ((Handle) hStr);
  269.             ok = WriteString (f, *hStr);
  270.             HUnlock ((Handle) hStr);
  271.             if (!ok)
  272.                 break;
  273.         }
  274.         if (ok)
  275.         {
  276.             ok = FileWrite (f, (Ptr) ¶Style, (long) sizeof (ParaStyle))
  277.                 && WriteString (f, paraMark)
  278.                 && WriteString (f, pageMark)
  279.                 && WriteString (f, periodStr)
  280.                 && WriteString (f, quoteStr);
  281.         }
  282.     }
  283.  
  284.     (void) GetFPos (f, &pos);
  285.     (void) SetEOF (f, pos);
  286.     (void) FSClose (f);
  287.     (void) FlushVol (/* f */ nil, outFile.vRefNum);
  288.  
  289.     if (!ok)
  290.     {
  291.         Message1 ("\pCould not write map");
  292.         (void) FSDelete (outFile.fName, outFile.vRefNum);
  293.         return (false);
  294.     }
  295.  
  296.     CopyString (outFile.fName, fName);        /* set map name */
  297.     vRefNum = outFile.vRefNum;
  298.     SetMapName (fName);
  299.     return (true);
  300. }
  301.