home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 492.lha / ToolManager_v1.3 / src / config.c next >
Encoding:
C/C++ Source or Header  |  1991-04-06  |  5.2 KB  |  193 lines

  1. /*
  2.  * config.c   V1.3
  3.  *
  4.  * configuration file handling
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* Keywords in the configuration file, must end with a ':'!! */
  12. static char IconKey[]="ICON:";
  13. static char CLIKey[]="CLI:";
  14. static char WBKey[]="WB:";
  15.  
  16. /* miscellaneous */
  17. extern char *ConfigName;
  18. extern char SCButtonText[];  /* file requester title */
  19.  
  20. /* Set the name of the config file */
  21. void SetConfigFileName(char *s)
  22. {
  23.  if (ConfigName) free(ConfigName);
  24.  ConfigName=strdup(s);
  25. }
  26.  
  27. /* Build one config line */
  28. void BuildConfigLine(struct ToolNode *tn, char *buf)
  29. {
  30.  if (tn->tn_CLI) strcpy(buf,CLIKey); /* Copy tool type */
  31.  else strcpy(buf,WBKey);
  32.  
  33.  strcat(buf,tn->tn_Node.ln_Name);    /* Append menu entry name */
  34.  if (tn->tn_RealName)                /* Exists a real name? */
  35.   {
  36.    strcat(buf,";");                  /* Yes, append it */
  37.    strcat(buf,tn->tn_RealName);
  38.   }
  39. }
  40.  
  41. /* Scan one config line */
  42. BOOL ScanConfigLine(char *buf, BPTR fl)
  43. {
  44.  char *cp1,*cp2;
  45.  BOOL cli,rc=FALSE;
  46.  
  47.  if ((cli=!strnicmp(buf,CLIKey,sizeof(CLIKey)-1)) ||
  48.      !strnicmp(buf,WBKey,sizeof(WBKey)-1))
  49.   {
  50.    cp1=strchr(buf,':')+1;
  51.  
  52.    if (cp2=strchr(cp1,';'))              /* Scan config line for ';' */
  53.     {
  54.      *cp2='\0';                          /* Menu entry ; real program name */
  55.      rc=AddToolNode(fl,cp1,++cp2,cli);
  56.     }
  57.    else rc=AddToolNode(fl,cp1,NULL,cli); /* Menu entry == real program name */
  58.   }
  59.  
  60.  return(rc);
  61. }
  62.  
  63. /* Read configuration file */
  64. void ReadConfigFile(BPTR fl)
  65. {
  66.  FILE *fh;                /* Filehandle for config file */
  67.  char ConfigLine[BUFLEN]; /* Buffer for one config file line */
  68.  
  69.  if (fh=fopen(ConfigName,"r")) /* Scan config file */
  70.   {
  71.    while (!feof(fh)) /* if not end of file, read one line into buffer */
  72.     if (fgets(ConfigLine,BUFLEN,fh) && (strlen(ConfigLine)>1))
  73.      {
  74.       ConfigLine[strlen(ConfigLine)-1]='\0'; /* Strip newline */
  75.       if (!strnicmp(ConfigLine,IconKey,sizeof(IconKey)-1))
  76.        {
  77.         char *cp;
  78.  
  79.         IconXPos=strtol(ConfigLine+sizeof(IconKey)-1,&cp,10);
  80.         IconYPos=strtol(cp+1,&cp,10);
  81.        }
  82.       else ScanConfigLine(ConfigLine,fl);
  83.      }
  84.    fclose(fh);  /* close config file */
  85.   }
  86. }
  87.  
  88. /* Tiny long to string conversion routine */
  89. static void ltoa(char *s, long n)
  90. {
  91.  long i=1000000000;     /* Divisor */
  92.  BOOL inumber=FALSE;    /* Flag */
  93.  
  94.  if (n==-2147483648)    /* Handle special case 2^31*/
  95.   {
  96.    strcpy(s,"-2147483648");
  97.    return;
  98.   }
  99.  
  100.  if (n<0)               /* Handle negativ numbers */
  101.   {
  102.    n=-n;
  103.    *s++='-';
  104.   }
  105.  
  106.  if (n==0) *s++='0';    /* Zero is a special case */
  107.  else while (i)         /* Every other numer goes here */
  108.        {
  109.         *s=n/i+'0';     /* Retrieve leading digit */
  110.         if (*s!='0') inumber=TRUE; /* Suppress leading zero's */
  111.         if (inumber) s++;
  112.         n%=i;           /* Remove digit from number */
  113.         i/=10;          /* next divisor */
  114.        }
  115.  
  116.  *s='\0';               /* Append string terminator */
  117. }
  118.  
  119. /* Write configuration file */
  120. BOOL WriteConfigFile(struct Window *w)
  121. {
  122.  struct FileRequester *req;
  123.  BOOL rc=FALSE;
  124.  char dir[NAMELEN],file[NAMELEN],*cp;
  125.  
  126.  /* Split file name */
  127.  cp=FilePart(ConfigName);
  128.  if (cp!=ConfigName) strncpy(dir,ConfigName,cp-ConfigName);
  129.  dir[cp-ConfigName]='\0';
  130.  strncpy(file,cp,NAMELEN);
  131.  
  132.  /* Alloc file requester structure */
  133.  if (!(req=AllocAslRequestTags(ASL_FileRequest,ASL_Hail,SCButtonText,
  134.                                                ASL_Window,w,
  135.                                                ASL_LeftEdge,w->LeftEdge,
  136.                                                ASL_TopEdge,w->TopEdge,
  137.                                                ASL_Dir,dir,
  138.                                                ASL_File,file,
  139.                                                ASL_FuncFlags,FILF_SAVE,
  140.                                                TAG_DONE)))
  141.   return(TRUE);
  142.  
  143.  /* Open file requester */
  144.  if (AslRequest(req,NULL))
  145.   if (*req->rf_File)                   /* Valid file name? */
  146.    {
  147.     FILE *fh;
  148.     struct ToolNode *tn;
  149.  
  150.     strncpy(dir,req->rf_Dir,NAMELEN);  /* Build complete name */
  151.     AddPart(dir,req->rf_File,NAMELEN);
  152.     SetConfigFileName(dir);
  153.  
  154.     if (fh=fopen(ConfigName,"w"))      /* Open config file */
  155.      {
  156.       /* Write icon position line */
  157.       fputs(IconKey,fh);
  158.       ltoa(dir,IconXPos);
  159.       fputs(dir,fh);
  160.       fputc(',',fh);
  161.       ltoa(dir,IconYPos);
  162.       fputs(dir,fh);
  163.       fputc('\n',fh);
  164.  
  165.       for (tn=GetHead(&ToolList); tn; tn=GetSucc(tn))
  166.        {
  167.         if (tn->tn_CLI) fputs(CLIKey,fh); /* Write tool type */
  168.         else fputs(WBKey,fh);
  169.  
  170.         fputs(tn->tn_Node.ln_Name,fh); /* Write menu entry name */
  171.         if (tn->tn_RealName)           /* Exists a real name? */
  172.          {
  173.           fputc(';',fh);               /* Yes, append it */
  174.           if (tn->tn_CLI) fputs(tn->tn_RealName,fh);
  175.           else
  176.            if (!NameFromLock(tn->tn_DirLock,dir,NAMELEN) ||
  177.                 !AddPart(dir,tn->tn_RealName,NAMELEN))
  178.             fputs(tn->tn_RealName,fh);
  179.            else fputs(dir,fh);
  180.          }
  181.         fputc('\n',fh);                /* Append a new line */
  182.        }
  183.       fclose(fh);                      /* Close the config file */
  184.      }
  185.     else rc=TRUE;                      /* Could not open config file! */
  186.    }
  187.   else rc=TRUE;                        /* No valid file name */
  188.  
  189.  FreeAslRequest(req);
  190.  return(rc);
  191. }
  192.  
  193.