home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / FACETV.ZIP / TV_CONFG.CPP < prev    next >
C/C++ Source or Header  |  1994-01-04  |  7KB  |  291 lines

  1. /************************************************************************
  2. **
  3. ** @(#)tv_confg.cpp    12/09/93    Chris Ahlstrom
  4. **
  5. **    C++
  6. **
  7. **    Displays selectable directories and files related to a
  8. ** BasicConfiguration, and allows one to be picked.
  9. **
  10. *************************************************************************/
  11.  
  12. #define TV_CONFG_cpp
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. #include "tv_confg.h"        // SimpleConfiguration class
  18. #include "tv_confg.err"        // general configuration-file error codes
  19.  
  20.  
  21.  
  22.  
  23. /************************************************************************
  24. ** SimpleConfiguration constructor
  25. **
  26. *************************************************************************/
  27.  
  28. const char * const
  29. SimpleConfiguration::MAGIC = "SimpleConfiguration  ::12345678";
  30.  
  31. SimpleConfiguration::SimpleConfiguration        // constructor
  32. (
  33.     TDeskTop *desk,            // desktop to write onto
  34.     char *filename,            // default filename for config file
  35.     BasicConfiguration *cfglist,    // a list of data areas
  36.     char *magicaddition            // magic string, if applicable
  37. ) :
  38.     configList    (cfglist),
  39.     errorCode    (0)
  40. {
  41.     magic = new char[CFG_MAGIC_STRING_LENGTH];
  42.     if (magic)
  43.     strcpy(magic, SimpleConfiguration::MAGIC);    // get a copy
  44.  
  45.     if (magic != 0 && magicaddition != 0 && *magicaddition != '\0')
  46.     {
  47.     char *dest = &magic[CFG_OFFSET];    // offset of personal magic
  48.     int count = CFG_PERSONAL_MAGIC;        // size of personal magic
  49.     char ch;
  50.  
  51.     while ((ch = *magicaddition++) != '\0')
  52.     {
  53.         if (count-- == 0)
  54.         break;                // ran out of personal magic
  55.         else
  56.         *dest++ = ch;
  57.     }
  58.     }
  59.  
  60.     char wildcard[16];
  61.  
  62.     if (filename != 0 && *filename != '\0')
  63.     {
  64.     strcpy(wildcard, filename);    // try whatever the caller gave
  65.     }
  66.     else
  67.     {
  68.     strcpy(wildcard, "*.CFG");    // nothing, try default wildcard
  69.     }
  70.  
  71.     configFile = new FilePicker(desk, wildcard);
  72.  
  73.     if (configFile->fileActive())        // we have a full name
  74.     {
  75.     errorCode = read();
  76.     }
  77. }
  78.  
  79.  
  80. /************************************************************************
  81. ** SimpleConfiguration destructor
  82. **
  83. *************************************************************************/
  84.  
  85. SimpleConfiguration::~SimpleConfiguration ()        // constructor
  86. {
  87.     if (magic)
  88.     delete [] magic;
  89.     if (configFile)
  90.     delete configFile;
  91. }
  92.  
  93.  
  94. /************************************************************************
  95. *************************************************************************/
  96.  
  97. const char *
  98. SimpleConfiguration::errorMsg ()
  99. {
  100.     if (errorCode < 0 || errorCode >= (int) ERR_CFG_MAX)
  101.     return "Error code out of SimpleConfig range";
  102.     else
  103.     if (errorCode)
  104.         return simpleConfigErrors[errorCode];
  105.     else
  106.         return (char *) 0;
  107. }
  108.  
  109.  
  110. /************************************************************************
  111. ** read()
  112. **
  113. **    Reads a configuration into memory.  Not much error-checking
  114. ** is done.
  115. **
  116. **    If successful, a 0 is returned, otherwise an integer
  117. ** corresponding to the ConfigurationError type.
  118. **
  119. *************************************************************************/
  120.  
  121. int
  122. SimpleConfiguration::read
  123. (
  124.     ForceNameFlag newname
  125. )
  126. {
  127.     int errcode = ERR_CFG_NONE;
  128.     char magicbuffer[CFG_MAGIC_STRING_LENGTH];
  129.  
  130.     if (configFile)
  131.     {
  132.     if (configList)
  133.     {
  134.         char *fname;
  135.         FILE *fptr;
  136.  
  137.         if (newname)
  138.         configFile->inactivateFile();
  139.  
  140.         fname = configFile->fileSpec();
  141.         if (configFile->fileAction() == FILE_CANCELLED)
  142.         {
  143.         errorCode = errcode;
  144.         return errcode;
  145.         }
  146.  
  147.         if ((fptr = fopen(fname, "rb")) != NULL)
  148.         {
  149.         size_t items;        // number of items successfully written
  150.         long size = 0L;        // accumulation of items
  151.         size_t needsize = 0;    // the necessary size
  152.         BasicConfiguration *cfg = configList;
  153.  
  154.         items = fread
  155.         (
  156.             magicbuffer, (size_t) CFG_MAGIC_STRING_LENGTH,
  157.             (size_t) 1, fptr
  158.         );
  159.         if (items < (size_t) 1)
  160.         {
  161.             errcode = ERR_CFG_BAD_SECTION_READ;
  162.         }
  163.         else if (strcmp(magicbuffer, magic) != 0)
  164.         {
  165.             errcode = ERR_CFG_WRONG_KIND_OF_FILE;
  166.         }
  167.         if (errcode)
  168.         {
  169.             errorCode = errcode;
  170.             return errcode;
  171.         }
  172.         while (cfg->configuration != 0)
  173.         {
  174.             needsize = cfg->configurationSize;
  175.             items = fread
  176.             (
  177.             cfg->configuration, (size_t) needsize, (size_t) 1, fptr
  178.             );
  179.  
  180.             if (items < (size_t) 1)
  181.             {
  182.             errcode = ERR_CFG_BAD_SECTION_READ;
  183.             break;
  184.             }
  185.             size += items;
  186.             cfg++;        // next device in config list
  187.         }
  188.         (void) fread(&size, sizeof(long), (size_t) 1, fptr);    // check
  189.         if (fclose(fptr) == EOF)
  190.             errcode = (int) ERR_CFG_FILE_CLOSE;
  191.         }
  192.         else
  193.         errcode = (int) ERR_CFG_FILE_OPEN;
  194.     }
  195.     else
  196.         errcode = (int) ERR_CFG_NO_CONFIG_LIST;
  197.     }
  198.     else
  199.     errcode = (int) ERR_CFG_CANT_ASSOCIATE_A_FILE;
  200.  
  201.     errorCode = errcode;
  202.     return errcode;
  203. }
  204.  
  205.  
  206. /************************************************************************
  207. ** write()
  208. **
  209. **    Writes a configuration into memory.  Not much error-checking
  210. ** is done.
  211. **
  212. **    If successful, a 0 is returned, otherwise an integer
  213. ** corresponding to the ConfigurationError type.
  214. **
  215. *************************************************************************/
  216.  
  217. int
  218. SimpleConfiguration::write
  219. (
  220.     ForceNameFlag newname
  221. )
  222. {
  223.     int errcode = ERR_CFG_NONE;
  224.  
  225.     if (configFile)
  226.     {
  227.     if (configList)
  228.     {
  229.         char *fname;
  230.         FILE *fptr;
  231.  
  232.         if (newname)
  233.         configFile->inactivateFile();
  234.  
  235.         fname = configFile->fileSpec();
  236.         if (configFile->fileAction() == FILE_CANCELLED)
  237.         {
  238.         errorCode = errcode;
  239.         return errcode;
  240.         }
  241.  
  242.         if ((fptr = fopen(fname, "wb")) != NULL)
  243.         {
  244.         size_t items;        // number of items successfully written
  245.         size_t size = 0;    // accumulation of items
  246.         long needsize = 0L;    // the necessary size
  247.         BasicConfiguration *cfg = configList;
  248.  
  249.         items = fwrite
  250.         (
  251.             magic, (size_t) CFG_MAGIC_STRING_LENGTH, (size_t) 1, fptr
  252.         );
  253.         if (items < (size_t) 1)
  254.         {
  255.             errorCode = ERR_CFG_BAD_SECTION_READ;
  256.             return errorCode;
  257.         }
  258.  
  259.         while (cfg->configuration != 0)
  260.         {
  261.             needsize = cfg->configurationSize;
  262.             items = fwrite
  263.             (
  264.             cfg->configuration, (size_t) needsize, (size_t) 1, fptr
  265.             );
  266.             if (items < (size_t) 1)
  267.             {
  268.             errcode = ERR_CFG_BAD_SECTION_WRITE;
  269.             break;
  270.             }
  271.             size += items;
  272.             cfg++;        // next device in config list
  273.         }
  274.         (void) fwrite(&size, sizeof(long), (size_t) 1, fptr);    // check
  275.         if (fclose(fptr) == EOF)
  276.             errcode = (int) ERR_CFG_FILE_CLOSE;
  277.         }
  278.         else
  279.         errcode = (int) ERR_CFG_FILE_OPEN;
  280.     }
  281.     else
  282.         errcode = (int) ERR_CFG_NO_CONFIG_LIST;
  283.     }
  284.     else
  285.     errcode = (int) ERR_CFG_CANT_ASSOCIATE_A_FILE;
  286.  
  287.     errorCode = errcode;
  288.     return errcode;
  289. }
  290.  
  291.