home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / savepa / savepal.cpp
Encoding:
Text File  |  1991-11-28  |  2.2 KB  |  86 lines

  1. /*
  2.    SAVEPAL.CPP
  3.  
  4.    Author: Michael Joseph Newton
  5.    CompuServe 70402,531
  6.    Uploaded 11-28-91
  7.  
  8.       An example of how to save and restore a Turbo Vision application's
  9.    palette to and from a binary file.
  10. */
  11.  
  12. void MyApp::writePalette(const char *path)
  13. // Saves the application's palette in binary file "path"
  14. {
  15.    char msg[MAXLINE];
  16.    ostrstream os(msg, sizeof msg);
  17.  
  18.    fpstream *f = new fpstream(path, ios::trunc | ios::binary);
  19.    if(!f)
  20.       {
  21.       os << "\003Could not create palette file \""
  22.          << path
  23.          << "\""
  24.          << ends;
  25.       messageBox(msg, mfOKButton | mfError);
  26.       }
  27.    else
  28.       {
  29.       opstream& strm = *f;
  30.       TPalette &palette = getPalette();
  31.       ushort paletteLen = palette[0];
  32.       strm.writeBytes(&palette[0], sizeof(palette[0]));
  33.       for(int ctr = 1; ctr < paletteLen + 1; ctr++)
  34.          strm.writeBytes(&palette[ctr], sizeof(palette[ctr]));
  35.       f->close();
  36.       delete f;
  37.       }
  38. }
  39.  
  40.  
  41. void MyApp::readPalette(const char *path)
  42. // Reads the application's palette from binary file "path"
  43. {
  44.    struct ffblk fcb;
  45.    char msg[MAXLINE];
  46.    ostrstream os(msg, sizeof msg);
  47.  
  48.    if(findfirst(path, &fcb, 0))
  49.       {
  50.       os << "\003Could not find palette file \""
  51.          << path
  52.          << "\""
  53.          << ends;
  54.       messageBox(msg, mfOKButton | mfError);
  55.       return;
  56.       }
  57.    else
  58.       {
  59.       fpstream *f = new fpstream(path, ios::in | ios::binary);
  60.       if(!f)
  61.          {
  62.          os << "\003Could not open palette file \""
  63.             << path
  64.             << "\""
  65.             << ends;
  66.          messageBox(msg, mfOKButton | mfError);
  67.          return;
  68.          }
  69.       else
  70.          {
  71.          ipstream& strm = *f;
  72.          TPalette &palette = getPalette();
  73.          ushort paletteLen = palette[0];
  74.          strm.readBytes(&palette[0], sizeof(palette[0]));
  75.          for(int ctr = 1; ctr < paletteLen + 1; ctr++)
  76.             strm.readBytes(&palette[ctr], sizeof(palette[ctr]));
  77.          f->close();
  78.          delete f;
  79.          }
  80.       }
  81.  
  82.    setState(sfExposed, False);    // Free all view buffers
  83.    setState(sfExposed, True);     // Back to normal
  84.    redraw();                      // Redraw it all
  85. }
  86.