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

  1. /*
  2.  * config.c  V2.0
  3.  *
  4.  * config file handling
  5.  *
  6.  * (c) 1990-1992 Stefan Becker
  7.  */
  8.  
  9. #include "ToolManagerLib.h"
  10.  
  11. /* Config data structure */
  12. struct ConfigBufNode {
  13.                       struct MinNode cbn_Node;
  14.                       ULONG          cbn_Size;
  15.                      };
  16.  
  17. typedef BOOL ReadConfigFunction(struct ConfigBufNode *);
  18.  
  19. /* Private data */
  20. static struct List ConfigBufList;
  21. static BOOL clistinit=FALSE;
  22. static ULONG stopchunks[]={ID_PREF,ID_TMEX,
  23.                            ID_PREF,ID_TMIM,
  24.                            ID_PREF,ID_TMSO,
  25.                            ID_PREF,ID_TMMO,
  26.                            ID_PREF,ID_TMIC,
  27.                            ID_PREF,ID_TMDO};
  28. #define NUMSTOPCHUNKS 6
  29.  
  30. /* Get one config string and correct pointer */
  31. static char *GetConfigStr(UBYTE **buf)
  32. {
  33.  char *s=*buf;
  34.  ULONG len=strlen(s)+1;
  35.  
  36.  /* Correct pointer */
  37.  *buf+=len;
  38.  
  39.  /* Return pointer to config string */
  40.  return(s);
  41. }
  42.  
  43. static struct TagItem ExecPrefsTags[]={
  44.                                        TMOP_Command,    NULL,
  45.                                        TMOP_CurrentDir, NULL,
  46.                                        TMOP_HotKey,     NULL,
  47.                                        TMOP_Output,     NULL,
  48.                                        TMOP_Path,       NULL,
  49.                                        TMOP_PubScreen,  NULL,
  50.  
  51.                                        TMOP_Arguments,  FALSE,
  52.                                        TMOP_ToFront,    FALSE,
  53.  
  54.                                        TMOP_Delay,      0,
  55.                                        TMOP_ExecType,   0,
  56.                                        TMOP_Priority,   0,
  57.                                        TMOP_Stack,      0,
  58.  
  59.                                        TAG_DONE
  60.                                       };
  61.  
  62. /* Interpret TMEX chunk */
  63. static BOOL ReadExecConfig(struct ConfigBufNode *cbn)
  64. {
  65.  struct ExecPrefsObject *epo=cbn+1;
  66.  ULONG sbits=epo->epo_StringBits;
  67.  UBYTE *ptr=epo+1;
  68.  char *name;
  69.  
  70.  /* Get name string */
  71.  name=(sbits & EXPO_NAME) ? GetConfigStr(&ptr) : DefaultNoName;
  72.  
  73.  /* Set string tags */
  74.  ExecPrefsTags[ 0].ti_Data=(sbits & EXPO_COMMAND) ? GetConfigStr(&ptr) : NULL;
  75.  ExecPrefsTags[ 1].ti_Data=(sbits & EXPO_CURDIR)  ? GetConfigStr(&ptr) : NULL;
  76.  ExecPrefsTags[ 2].ti_Data=(sbits & EXPO_HOTKEY)  ? GetConfigStr(&ptr) : NULL;
  77.  ExecPrefsTags[ 3].ti_Data=(sbits & EXPO_OUTPUT)  ? GetConfigStr(&ptr) : NULL;
  78.  ExecPrefsTags[ 4].ti_Data=(sbits & EXPO_PATH)    ? GetConfigStr(&ptr) : NULL;
  79.  ExecPrefsTags[ 5].ti_Data=(sbits & EXPO_PSCREEN) ? GetConfigStr(&ptr) : NULL;
  80.  
  81.  /* Set boolean tags */
  82.  ExecPrefsTags[ 6].ti_Data=(epo->epo_Flags & EXPOF_ARGS)    != 0;
  83.  ExecPrefsTags[ 7].ti_Data=(epo->epo_Flags & EXPOF_TOFRONT) != 0;
  84.  
  85.  /* Set integer tags */
  86.  ExecPrefsTags[ 8].ti_Data=epo->epo_Delay;
  87.  ExecPrefsTags[ 9].ti_Data=epo->epo_ExecType;
  88.  ExecPrefsTags[10].ti_Data=epo->epo_Priority;
  89.  ExecPrefsTags[11].ti_Data=epo->epo_Stack;
  90.  
  91.  /* Create object */
  92.  return(InternalCreateTMObject(PrivateTMHandle,name,TMOBJTYPE_EXEC,
  93.                                ExecPrefsTags));
  94. }
  95.  
  96. static struct TagItem ImagePrefsTags[]={
  97.                                         TMOP_File, NULL,
  98.  
  99.                                         TAG_DONE
  100.                                        };
  101.  
  102. /* Interpret TMIM chunk */
  103. static BOOL ReadImageConfig(struct ConfigBufNode *cbn)
  104. {
  105.  struct ImagePrefsObject *ipo=cbn+1;
  106.  ULONG sbits=ipo->ipo_StringBits;
  107.  UBYTE *ptr=ipo+1;
  108.  char *name;
  109.  
  110.  /* Get name string */
  111.  name=(sbits & IMPO_NAME) ? GetConfigStr(&ptr) : DefaultNoName;
  112.  
  113.  /* Set string tags */
  114.  ImagePrefsTags[ 0].ti_Data=(sbits & IMPO_FILE) ? GetConfigStr(&ptr) : NULL;
  115.  
  116.  /* Create object */
  117.  return(InternalCreateTMObject(PrivateTMHandle,name,TMOBJTYPE_IMAGE,
  118.                                ImagePrefsTags));
  119. }
  120.  
  121. static struct TagItem SoundPrefsTags[]={
  122.                                         TMOP_Command, NULL,
  123.                                         TMOP_Port,    NULL,
  124.  
  125.                                         TAG_DONE
  126.                                        };
  127.  
  128. /* Interpret TMSO chunk */
  129. static BOOL ReadSoundConfig(struct ConfigBufNode *cbn)
  130. {
  131.  struct SoundPrefsObject *spo=cbn+1;
  132.  ULONG sbits=spo->spo_StringBits;
  133.  UBYTE *ptr=spo+1;
  134.  char *name;
  135.  
  136.  /* Get name string */
  137.  name=(sbits & SOPO_NAME) ? GetConfigStr(&ptr) : DefaultNoName;
  138.  
  139.  /* Set string tags */
  140.  SoundPrefsTags[ 0].ti_Data=(sbits & SOPO_COMMAND) ? GetConfigStr(&ptr) : NULL;
  141.  SoundPrefsTags[ 1].ti_Data=(sbits & SOPO_PORT)    ? GetConfigStr(&ptr) : NULL;
  142.  
  143.  /* Create object */
  144.  return(InternalCreateTMObject(PrivateTMHandle,name,TMOBJTYPE_SOUND,
  145.                                SoundPrefsTags));
  146. }
  147.  
  148. static struct TagItem MenuPrefsTags[]={
  149.                                        TMOP_Exec,  NULL,
  150.                                        TMOP_Sound, NULL,
  151.  
  152.                                        TAG_DONE
  153.                                       };
  154.  
  155. /* Interpret TMMO chunk */
  156. static BOOL ReadMenuConfig(struct ConfigBufNode *cbn)
  157. {
  158.  struct MenuPrefsObject *mpo=cbn+1;
  159.  ULONG sbits=mpo->mpo_StringBits;
  160.  UBYTE *ptr=mpo+1;
  161.  char *name;
  162.  
  163.  /* Get name string */
  164.  name=(sbits & MOPO_NAME) ? GetConfigStr(&ptr) : DefaultNoName;
  165.  
  166.  /* Set string tags */
  167.  MenuPrefsTags[ 0].ti_Data=(sbits & MOPO_EXEC)  ? GetConfigStr(&ptr) : NULL;
  168.  MenuPrefsTags[ 1].ti_Data=(sbits & MOPO_SOUND) ? GetConfigStr(&ptr) : NULL;
  169.  
  170.  /* Create object */
  171.  return(InternalCreateTMObject(PrivateTMHandle,name,TMOBJTYPE_MENU,
  172.                                MenuPrefsTags));
  173. }
  174.  
  175. static struct TagItem IconPrefsTags[]={
  176.                                        TMOP_Exec,     NULL,
  177.                                        TMOP_Image,    NULL,
  178.                                        TMOP_Sound,    NULL,
  179.  
  180.                                        TMOP_ShowName, FALSE,
  181.  
  182.                                        TMOP_LeftEdge, 0,
  183.                                        TMOP_TopEdge,  0,
  184.  
  185.                                        TAG_DONE
  186.                                       };
  187.  
  188. /* Interpret TMIC chunk */
  189. static BOOL ReadIconConfig(struct ConfigBufNode *cbn)
  190. {
  191.  struct IconPrefsObject *ipo=cbn+1;
  192.  ULONG sbits=ipo->ipo_StringBits;
  193.  UBYTE *ptr=ipo+1;
  194.  char *name;
  195.  
  196.  /* Get name string */
  197.  name=(sbits & ICPO_NAME) ? GetConfigStr(&ptr) : DefaultNoName;
  198.  
  199.  /* Set string tags */
  200.  IconPrefsTags[ 0].ti_Data=(sbits & ICPO_EXEC)  ? GetConfigStr(&ptr) : NULL;
  201.  IconPrefsTags[ 1].ti_Data=(sbits & ICPO_IMAGE) ? GetConfigStr(&ptr) : NULL;
  202.  IconPrefsTags[ 2].ti_Data=(sbits & ICPO_SOUND) ? GetConfigStr(&ptr) : NULL;
  203.  
  204.  /* Set boolean tags */
  205.  IconPrefsTags[ 3].ti_Data=(ipo->ipo_Flags & ICPOF_SHOWNAME) != 0;
  206.  
  207.  /* Set integer tags */
  208.  IconPrefsTags[ 4].ti_Data=ipo->ipo_XPos;
  209.  IconPrefsTags[ 5].ti_Data=ipo->ipo_YPos;
  210.  
  211.  /* Create object */
  212.  return(InternalCreateTMObject(PrivateTMHandle,name,TMOBJTYPE_ICON,
  213.                                IconPrefsTags));
  214. }
  215.  
  216. static struct TagItem DockPrefsTags[]={
  217.                                        TMOP_HotKey,    NULL,
  218.                                        TMOP_PubScreen, NULL,
  219.                                        TMOP_Title,     NULL,
  220.  
  221.                                        TMOP_Font,      NULL,
  222.  
  223.                                        TMOP_Activated, FALSE,
  224.                                        TMOP_Centered,  FALSE,
  225.                                        TMOP_FrontMost, FALSE,
  226.                                        TMOP_Menu,      FALSE,
  227.                                        TMOP_Pattern,   FALSE,
  228.                                        TMOP_PopUp,     FALSE,
  229.                                        TMOP_Text,      FALSE,
  230.                                        TMOP_Vertical,  FALSE,
  231.  
  232.                                        TMOP_Columns,   0,
  233.                                        TMOP_LeftEdge,  0,
  234.                                        TMOP_TopEdge,   0,
  235.  
  236.                                        TAG_MORE,       NULL
  237.                                       };
  238.  
  239. struct ToolEntry {
  240.                   char *te_Exec;
  241.                   char *te_Image;
  242.                   char *te_Sound;
  243.                  };
  244. #define MAXTOOLS    1000
  245. #define TOOLBUFSIZE (MAXTOOLS*(sizeof(struct TagItem)+sizeof(struct ToolEntry)))
  246.  
  247. /* Interpret TMDO chunk */
  248. static BOOL ReadDockConfig(struct ConfigBufNode *cbn)
  249. {
  250.  char *toolbuf;
  251.  
  252.  /* Allocate memory for tools tags & strings */
  253.  if (toolbuf=AllocMem(TOOLBUFSIZE,MEMF_PUBLIC)) {
  254.   struct TagItem *tt=toolbuf;
  255.   struct ToolEntry *te=(struct TagItem *) toolbuf+MAXTOOLS;
  256.   struct DockPrefsObject *dpo=cbn+1;
  257.   ULONG sbits=dpo->dpo_StringBits;
  258.   UBYTE *ptr=dpo+1;
  259.   char *name;
  260.   BOOL rc;
  261.  
  262.   /* Get name string */
  263.   name=(sbits & DOPO_NAME) ? GetConfigStr(&ptr) : DefaultNoName;
  264.  
  265.   /* Set string tags */
  266.   DockPrefsTags[ 0].ti_Data=(sbits & DOPO_HOTKEY)  ? GetConfigStr(&ptr) : NULL;
  267.   DockPrefsTags[ 1].ti_Data=(sbits & DOPO_PSCREEN) ? GetConfigStr(&ptr) : NULL;
  268.   DockPrefsTags[ 2].ti_Data=(sbits & DOPO_TITLE)   ? GetConfigStr(&ptr) : NULL;
  269.  
  270.   /* Get font */
  271.   DockPrefsTags[ 3].ti_Data=
  272.   (dpo->dpo_Font.ta_Name=(sbits & DOPO_FONTNAME) ? GetConfigStr(&ptr) : NULL) ?
  273.    &dpo->dpo_Font : NULL;
  274.  
  275.   /* Set boolean tags */
  276.   DockPrefsTags[ 4].ti_Data=(dpo->dpo_Flags & DOPOF_ACTIVATED) != 0;
  277.   DockPrefsTags[ 5].ti_Data=(dpo->dpo_Flags & DOPOF_CENTERED)  != 0;
  278.   DockPrefsTags[ 6].ti_Data=(dpo->dpo_Flags & DOPOF_FRONTMOST) != 0;
  279.   DockPrefsTags[ 7].ti_Data=(dpo->dpo_Flags & DOPOF_MENU)      != 0;
  280.   DockPrefsTags[ 8].ti_Data=(dpo->dpo_Flags & DOPOF_PATTERN)   != 0;
  281.   DockPrefsTags[ 9].ti_Data=(dpo->dpo_Flags & DOPOF_POPUP)     != 0;
  282.   DockPrefsTags[10].ti_Data=(dpo->dpo_Flags & DOPOF_TEXT)      != 0;
  283.   DockPrefsTags[11].ti_Data=(dpo->dpo_Flags & DOPOF_VERTICAL)  != 0;
  284.  
  285.   /* Set integer tags */
  286.   DockPrefsTags[12].ti_Data=dpo->dpo_Columns;
  287.   DockPrefsTags[13].ti_Data=dpo->dpo_XPos;
  288.   DockPrefsTags[14].ti_Data=dpo->dpo_YPos;
  289.  
  290.   /* Read tool entries */
  291.   {
  292.    UBYTE tlflags;
  293.  
  294.    /* Get next tool entry */
  295.    while ((tlflags=*ptr++) & DOPOT_CONTINUE) {
  296.     /* Append tag */
  297.     tt->ti_Tag =TMOP_Tool;
  298.     tt->ti_Data=te;
  299.     tt++;
  300.  
  301.     /* Set tool entry strings */
  302.     te->te_Exec =(tlflags & DOPOT_EXEC)  ? GetConfigStr(&ptr) : NULL;
  303.     te->te_Image=(tlflags & DOPOT_IMAGE) ? GetConfigStr(&ptr) : NULL;
  304.     te->te_Sound=(tlflags & DOPOT_SOUND) ? GetConfigStr(&ptr) : NULL;
  305.     te++;
  306.    }
  307.   }
  308.  
  309.   /* Terminate tool tag array and chain both tag arrays */
  310.   tt->ti_Tag=TAG_DONE;
  311.   DockPrefsTags[15].ti_Data=toolbuf;
  312.  
  313.   /* Create object */
  314.   rc=InternalCreateTMObject(PrivateTMHandle,name,TMOBJTYPE_DOCK,DockPrefsTags);
  315.  
  316.   /* Free buffer */
  317.   FreeMem(toolbuf,TOOLBUFSIZE);
  318.   return(rc);
  319.  }
  320.  /* Call failed */
  321.  return(FALSE);
  322. }
  323.  
  324. static ReadConfigFunction *ReadConfigFunctions[TMOBJTYPES]={
  325.                                                             ReadExecConfig,
  326.                                                             ReadImageConfig,
  327.                                                             ReadSoundConfig,
  328.                                                             ReadMenuConfig,
  329.                                                             ReadIconConfig,
  330.                                                             ReadDockConfig
  331.                                                            };
  332.  
  333. /* Read config file */
  334. void ReadConfig(void)
  335. {
  336.  /* Open IFF parsing library */
  337.  if (IFFParseBase=OpenLibrary("iffparse.library",0)) {
  338.   struct IFFHandle *iff;
  339.  
  340.   DEBUG_PRINTF("IFF Library opened.\n");
  341.  
  342.   /* Alloc IFF handle */
  343.   if (iff=AllocIFF()) {
  344.  
  345.    DEBUG_PRINTF("IFF Handle (0x%08lx)\n",iff);
  346.  
  347.    /* Open IFF file */
  348.    if (iff->iff_Stream=Open(PrefsFileName,MODE_OLDFILE)) {
  349.     /* Init IFF handle */
  350.     InitIFFasDOS(iff);
  351.  
  352.     DEBUG_PRINTF("IFF Stream (0x%08lx)\n",iff->iff_Stream);
  353.  
  354.     /* Open IFF handle */
  355.     if (!OpenIFF(iff,IFFF_READ)) {
  356.  
  357.      DEBUG_PRINTF("IFF Opened\n");
  358.  
  359.      /* Start IFF parsing */
  360.      if (!ParseIFF(iff,IFFPARSE_STEP)) {
  361.       struct ContextNode *cn;
  362.  
  363.       DEBUG_PRINTF("First IFF scan step\n");
  364.  
  365.       /* Init config list */
  366.       NewList(&ConfigBufList);
  367.       clistinit=TRUE;
  368.  
  369.       /* Check IFF type and set IFF chunk types */
  370.       if ((cn=CurrentChunk(iff)) && (cn->cn_ID==ID_FORM) &&
  371.           (cn->cn_Type==ID_PREF) &&
  372.           !PropChunk(iff,ID_PREF,ID_PRHD) &&
  373.           !StopChunks(iff,stopchunks,NUMSTOPCHUNKS) &&
  374.           !StopOnExit(iff,ID_PREF,ID_FORM) &&
  375.           !ParseIFF(iff,IFFPARSE_SCAN)) {
  376.        /* First stop chunk encountered */
  377.        struct StoredProperty *sp;
  378.  
  379.        /* Get pointer to PRHD chunk */
  380.        if (sp=FindProp(iff,ID_PREF,ID_PRHD)) {
  381.         struct PrefHeader *ph=(struct PrefHeader *) sp->sp_Data;
  382.  
  383.         /* Check file version number */
  384.         if (ph->ph_Version==TMPREFSVERSION) {
  385.  
  386.          /* Parse IFF chunks */
  387.          do {
  388.           /* Get current chunk */
  389.           if (cn=CurrentChunk(iff)) {
  390.            LONG type;
  391.  
  392.            DEBUG_PRINTF("chunk ID: 0x%08lx",cn->cn_ID);
  393.            DEBUG_PRINTF(" size: 0x%08lx",cn->cn_Size);
  394.  
  395.            /* Read IFF chunk according to chunk ID */
  396.            switch(cn->cn_ID) {
  397.             case ID_TMEX: type=TMOBJTYPE_EXEC;
  398.                           break;
  399.             case ID_TMIM: type=TMOBJTYPE_IMAGE;
  400.                           break;
  401.             case ID_TMSO: type=TMOBJTYPE_SOUND;
  402.                           break;
  403.             case ID_TMMO: type=TMOBJTYPE_MENU;
  404.                           break;
  405.             case ID_TMIC: type=TMOBJTYPE_ICON;
  406.                           break;
  407.             case ID_TMDO: type=TMOBJTYPE_DOCK;
  408.                           break;
  409.             default:      type=-1;
  410.                           break;
  411.            }
  412.  
  413.            DEBUG_PRINTF(" type: %ld\n",type);
  414.  
  415.            /* valid type? */
  416.            if (type!=-1) {
  417.             ULONG sizechunk=cn->cn_Size;
  418.             ULONG sizebuf=sizechunk+sizeof(struct ConfigBufNode);
  419.             struct ConfigBufNode *cbn;
  420.  
  421.             /* Allocate memory for config buffer */
  422.             if (cbn=AllocMem(sizebuf,MEMF_PUBLIC)) {
  423.              /* Read chunk, interpret contents & create object */
  424.              if ((ReadChunkBytes(iff,cbn+1,sizechunk)==sizechunk) &&
  425.                  (*ReadConfigFunctions[type])(cbn)) {
  426.               /* Set size */
  427.               cbn->cbn_Size=sizebuf;
  428.  
  429.               /* Add node */
  430.               AddTail(&ConfigBufList,(struct Node *) cbn);
  431.               cbn=NULL;
  432.              }
  433.  
  434.              /* Error: free buffer */
  435.              if (cbn) FreeMem(cbn,sizebuf);
  436.             }
  437.  
  438.             DEBUG_PRINTF("cbn: 0x%08lx\n",cbn);
  439. /*            DEBUG_GETCHR(); */
  440.            }
  441.           }
  442.          /* Next parse step */
  443.          } while (!ParseIFF(iff,IFFPARSE_SCAN));
  444.         }
  445.        }
  446.       }
  447.      }
  448.      CloseIFF(iff);
  449.     }
  450.     Close(iff->iff_Stream);
  451.    }
  452.    FreeIFF(iff);
  453.   }
  454.   CloseLibrary(IFFParseBase);
  455.  }
  456. }
  457.  
  458. /* Free config buffers */
  459. void FreeConfig(void)
  460. {
  461.  struct ConfigBufNode *cbn;
  462.  
  463.  /* List valid? */
  464.  if (clistinit) {
  465.   /* Scan list, remove head entry */
  466.   while (cbn=RemHead(&ConfigBufList))
  467.    /* Free entry */
  468.    FreeMem(cbn,cbn->cbn_Size);
  469.  
  470.   clistinit=FALSE;
  471.  }
  472. }
  473.