home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / toolmana.lha / ToolManager / Source / prefs / config.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-26  |  7.0 KB  |  273 lines

  1. /*
  2.  * config.c  V2.0
  3.  *
  4.  * preferences file handling
  5.  *
  6.  * (c) 1990-1992 Stefan Becker
  7.  */
  8.  
  9. #include "ToolManagerConf.h"
  10.  
  11. /* misc. data */
  12. #define BUFSIZE 65536
  13. ULONG stopchunks[]={ID_PREF,ID_TMEX,
  14.                     ID_PREF,ID_TMIM,
  15.                     ID_PREF,ID_TMSO,
  16.                     ID_PREF,ID_TMMO,
  17.                     ID_PREF,ID_TMIC,
  18.                     ID_PREF,ID_TMDO};
  19. #define NUMSTOPCHUNKS 6
  20. struct PrefHeader PrefHdrChunk={TMPREFSVERSION,0,0};
  21.  
  22. /* Function tables */
  23. ReadNodeFunction *ReadNodeFunctions[TMOBJTYPES]={
  24.                                                  ReadExecNode,
  25.                                                  ReadImageNode,
  26.                                                  ReadSoundNode,
  27.                                                  ReadMenuNode,
  28.                                                  ReadIconNode,
  29.                                                  ReadDockNode
  30.                                                 };
  31.  
  32. WriteNodeFunction *WriteNodeFunctions[TMOBJTYPES]={
  33.                                                    WriteExecNode,
  34.                                                    WriteImageNode,
  35.                                                    WriteSoundNode,
  36.                                                    WriteMenuNode,
  37.                                                    WriteIconNode,
  38.                                                    WriteDockNode
  39.                                                   };
  40.  
  41. /* Read config file */
  42. BOOL ReadConfigFile(char *filename)
  43. {
  44.  UBYTE *configbuf;
  45.  BOOL rc=FALSE;
  46.  
  47.  DEBUG_PRINTF("read config from '%s'\n",filename);
  48.  
  49.  /* Allocate memory for config buffer */
  50.  if (configbuf=malloc(BUFSIZE)) {
  51.   struct IFFHandle *iff;
  52.  
  53.   DEBUG_PRINTF("config buffer: 0x%08lx\n",configbuf)
  54.  
  55.   /* Allocate IFF handle */
  56.   if (iff=AllocIFF()) {
  57.    /* Every error will be ignored after this point! */
  58.    rc=TRUE;
  59.  
  60.    DEBUG_PRINTF("IFF Handle: 0x%08lx\n",iff);
  61.  
  62.    /* Open IFF File */
  63.    if (iff->iff_Stream=Open(filename,MODE_OLDFILE)) {
  64.     /* Init IFF handle */
  65.     InitIFFasDOS(iff);
  66.  
  67.     DEBUG_PRINTF("IFF Stream: 0x%08lx\n",iff->iff_Stream);
  68.  
  69.     /* Open IFF handle */
  70.     if (!OpenIFF(iff,IFFF_READ)) {
  71.  
  72.      DEBUG_PRINTF("IFF open\n");
  73.  
  74.      /* Start IFF parsing */
  75.      if (!ParseIFF(iff,IFFPARSE_STEP)) {
  76.       struct ContextNode *cn;
  77.  
  78.       DEBUG_PRINTF("First IFF scan step\n");
  79.  
  80.       /* Check IFF type and set IFF chunk types */
  81.       if ((cn=CurrentChunk(iff)) && (cn->cn_ID==ID_FORM) &&
  82.           (cn->cn_Type==ID_PREF) &&
  83.           !PropChunk(iff,ID_PREF,ID_PRHD) &&
  84.           !StopChunks(iff,stopchunks,NUMSTOPCHUNKS) &&
  85.           !StopOnExit(iff,ID_PREF,ID_FORM) &&
  86.           !ParseIFF(iff,IFFPARSE_SCAN)) {
  87.        /* First stop chunk encountered */
  88.        struct StoredProperty *sp;
  89.  
  90.        /* Get pointer to PRHD chunk */
  91.        if (sp=FindProp(iff,ID_PREF,ID_PRHD)) {
  92.         struct PrefHeader *ph=(struct PrefHeader *) sp->sp_Data;
  93.  
  94.         /* Check file version number */
  95.         if (ph->ph_Version==TMPREFSVERSION) {
  96.  
  97.          /* Parse IFF chunks */
  98.          do {
  99.           /* Get current chunk */
  100.           if (cn=CurrentChunk(iff)) {
  101.            LONG type;
  102.  
  103.            DEBUG_PRINTF("chunk ID: 0x%08lx",cn->cn_ID);
  104.            DEBUG_PRINTF(" size: 0x%08lx",cn->cn_Size);
  105.  
  106.            /* Read IFF chunk according to chunk ID */
  107.            switch(cn->cn_ID) {
  108.             case ID_TMEX: type=TMOBJTYPE_EXEC;
  109.                           break;
  110.             case ID_TMIM: type=TMOBJTYPE_IMAGE;
  111.                           break;
  112.             case ID_TMSO: type=TMOBJTYPE_SOUND;
  113.                           break;
  114.             case ID_TMMO: type=TMOBJTYPE_MENU;
  115.                           break;
  116.             case ID_TMIC: type=TMOBJTYPE_ICON;
  117.                           break;
  118.             case ID_TMDO: type=TMOBJTYPE_DOCK;
  119.                           break;
  120.             default:      type=-1;
  121.                           break;
  122.            }
  123.  
  124.            DEBUG_PRINTF(" type: %ld\n",type);
  125.  
  126.            /* valid type? */
  127.            if (type!=-1) {
  128.             ULONG size=cn->cn_Size;
  129.  
  130.             /* Read chunk */
  131.             if (ReadChunkBytes(iff,configbuf,size)==size) {
  132.              struct Node *node;
  133.  
  134.              DEBUG_PRINTF("chunk read\n");
  135.  
  136.              /* Interpret chunk contents */
  137.              if (node=(*ReadNodeFunctions[type])(configbuf)) {
  138.  
  139.               DEBUG_PRINTF("new node: 0x%08lx\n",node);
  140.  
  141.               /* Make sure ln_Name is valid */
  142.               if (!node->ln_Name) node->ln_Name=strdup("");
  143.  
  144.               /* Append new node to list */
  145.               AddTail(&ObjectLists[type],node);
  146.              }
  147.             }
  148.            }
  149.           }
  150.          /* Next parse step */
  151.          } while (!ParseIFF(iff,IFFPARSE_SCAN));
  152.         }
  153.        }
  154.       }
  155.      }
  156.      CloseIFF(iff);
  157.     }
  158.     Close(iff->iff_Stream);
  159.    }
  160.    FreeIFF(iff);
  161.   }
  162.   free(configbuf);
  163.  }
  164.  return(rc);
  165. }
  166.  
  167. /* Write config file */
  168. BOOL WriteConfigFile(char *filename)
  169. {
  170.  UBYTE *configbuf;
  171.  BOOL rc=FALSE;
  172.  
  173.  DEBUG_PRINTF("write config to '%s'\n",filename);
  174.  
  175.  /* Allocate memory for config buffer */
  176.  if (configbuf=malloc(BUFSIZE)) {
  177.   struct IFFHandle *iff;
  178.  
  179.   DEBUG_PRINTF("config buffer: 0x%08lx\n",configbuf)
  180.  
  181.   /* Allocate IFF handle */
  182.   if (iff=AllocIFF()) {
  183.  
  184.    DEBUG_PRINTF("IFF Handle: 0x%08lx\n",iff);
  185.  
  186.    /* Open IFF File */
  187.    if (iff->iff_Stream=Open(filename,MODE_NEWFILE)) {
  188.     /* Init IFF handle */
  189.     InitIFFasDOS(iff);
  190.  
  191.     DEBUG_PRINTF("IFF Stream: 0x%08lx\n",iff->iff_Stream);
  192.  
  193.     /* Open IFF handle */
  194.     if (!OpenIFF(iff,IFFF_WRITE)) {
  195.  
  196.      DEBUG_PRINTF("IFF open\n");
  197.  
  198.      /* Push FORM IFF chunk */
  199.      if (!PushChunk(iff,ID_PREF,ID_FORM,IFFSIZE_UNKNOWN)) {
  200.  
  201.       /* Write PRHD IFF chunk */
  202.       if (!PushChunk(iff,0,ID_PRHD,sizeof(struct PrefHeader)) &&
  203.           (WriteChunkBytes(iff,(UBYTE *) &PrefHdrChunk,
  204.                            sizeof(struct PrefHeader))
  205.                             ==sizeof(struct PrefHeader)) &&
  206.           !PopChunk(iff))
  207.        /* Set return code */
  208.        rc=TRUE;
  209.  
  210.       /* error? */
  211.       if (rc) {
  212.        ULONG i;
  213.  
  214.        /* No, scan all object lists */
  215.        for (i=0; (i<TMOBJTYPES) && rc; i++) {
  216.         struct Node *node=GetHead(&ObjectLists[i]);
  217.         WriteNodeFunction *wnfp=WriteNodeFunctions[i];
  218.  
  219.         /* Scan list */
  220.         while (node && rc) {
  221.          /* Convert node into IFF chunk */
  222.          rc=(*wnfp)(iff,configbuf,node);
  223.  
  224.          /* Get next node */
  225.          node=GetSucc(node);
  226.         }
  227.        }
  228.       }
  229.      }
  230.      CloseIFF(iff);
  231.     }
  232.     Close(iff->iff_Stream);
  233.    }
  234.    FreeIFF(iff);
  235.   }
  236.   free(configbuf);
  237.  }
  238.  return(rc);
  239. }
  240.  
  241. /* Read one config string and correct pointer */
  242. char *GetConfigStr(UBYTE **buf)
  243. {
  244.  char *s=*buf;
  245.  char *new;
  246.  ULONG len=strlen(s)+1;
  247.  
  248.  /* Allocate string buffer */
  249.  if (new=malloc(len)) {
  250.   /* Copy string */
  251.   strcpy(new,s);
  252.  
  253.   /* Correct pointer */
  254.   *buf+=len;
  255.  }
  256.  return(new);
  257. }
  258.  
  259. /* Write one config string and correct pointer */
  260. BOOL PutConfigStr(char *s, UBYTE **buf)
  261. {
  262.  /* string valid? */
  263.  if (s) {
  264.   /* Copy string to buffer */
  265.   strcpy(*buf,s);
  266.  
  267.   /* Correct pointer */
  268.   *buf+=strlen(s)+1;
  269.   return(TRUE);
  270.  }
  271.  return(FALSE);
  272. }
  273.