home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d500 / wiconify.lha / wIconify / wIconSetter.lzh / wIconSetter / Source / wIconFile.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  15KB  |  524 lines

  1. /*
  2.  *  WICONSETTER     A companion utility to wIconify.  wIconSetter allows
  3.  *                  you to specify custom icons for windows ans screens
  4.  *                  that normally use the default icons.
  5.  *
  6.  *  wIconFile.c     Reads the initialization file.
  7.  *
  8.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  9.  *  You may use this code, provided this copyright notice is kept intact.
  10.  */
  11.  
  12. #include "wIconFile.h"
  13.  
  14. static char *Any = "[ANY]";
  15. static char *Null = "[NULL]";
  16. extern char **ComName;              /* current command table */
  17.  
  18. #define NAME(p)             ((p->Name)?((p->Name == NAME_ANY)?Any:p->Name):Null)
  19.  
  20.  
  21. /*
  22.  *  StartCommand()
  23.  *
  24.  *  Set up anything needed to begin the specified command.  Usually this
  25.  *  means clear any previous value of the appropriate variables or flags,
  26.  *  and possibly recording the command type for future reference.
  27.  *
  28.  *    PROGRAM:
  29.  *      If a previous program or define is in effect, check that an icon was
  30.  *        provided, and if not, give an error.
  31.  *      Allocate a new program structure.
  32.  *
  33.  *    SCREEN:
  34.  *      If a previous screen or define is in effect, check that an icon was
  35.  *        provided, and if not, give an error.
  36.  *      Allocate a new screen structure.
  37.  *
  38.  *    WINDOW:
  39.  *      If a previous define is in effect and no icon was give, error
  40.  *      Get a new window structure.
  41.  *
  42.  *    ICON:
  43.  *      Check that there is somewhere to put the icon.
  44.  *
  45.  *    IMAGE, SELECT, MASK:
  46.  *      Clear the image area and check that there is somewhere to put the icon.
  47.  *
  48.  *    FLAGS:
  49.  *      Check that there is somewhere to put the flags,
  50.  *      If so, clear the flag area.
  51.  *
  52.  *    POSITION, NAME:
  53.  *      Check that there is somewhere to put the position or name.
  54.  *
  55.  *    DEFINE:
  56.  *      If a previous program or define is in effect, check to be sure that
  57.  *        an icon was provided.  Error if not.
  58.  *      Get a new definition structure.
  59.  *
  60.  *  If an icon was started but there was no place to put it, error.
  61.  */
  62.  
  63. static int StartCommand(theCommand)
  64. int theCommand;
  65. {
  66.    int NeedIcon = FALSE;
  67.  
  68.    switch(theCommand)
  69.    {
  70.       case COM_PROGRAM:
  71.          if ((PROGRAM || DEFINE) && NoIcon) NoIconError();
  72.          GetNewProgram();
  73.          break;
  74.  
  75.       case COM_SCREEN:
  76.          if (((ScreenPtr && SCREEN) || DEFINE) && NoIcon) NoIconError();
  77.          GetNewScreen();
  78.          break;
  79.  
  80.       case COM_WINDOW:
  81.          if (DEFINE && NoIcon) NoIconError();
  82.          GetNewWindow();
  83.          break;
  84.  
  85.       case COM_ICON:
  86.          NeedIcon = CheckForIcon();
  87.          if (NeedIcon) theCommand = NULL;
  88.          NoIcon = FALSE;
  89.          break;
  90.  
  91.       case COM_IMAGE:
  92.       case COM_SELECT:
  93.       case COM_MASK:
  94.          ClearBitMap();
  95.          ImageType = theCommand;
  96.          NeedIcon = CheckForIcon();
  97.          NoIcon = FALSE;
  98.          break;
  99.  
  100.       case COM_FLAGS:
  101.          NeedIcon = CheckForIcon();
  102.          if (IconPtr)
  103.          {
  104.             ICON.Flags = 0;
  105.             CopyIconData(COM_FLAGS);
  106.          } else {
  107.             theCommand = NULL;
  108.          }
  109.          NoIcon = FALSE;
  110.          break;
  111.  
  112.       case COM_POSITION:
  113.       case COM_NAME:
  114.          NeedIcon = CheckForIcon();
  115.          if (NeedIcon) theCommand = NULL;
  116.          NoIcon = FALSE;
  117.          break;
  118.  
  119.       case COM_DEFINE:
  120.          if ((PROGRAM || DEFINE) && NoIcon) NoIconError();
  121.          GetNewDefine();
  122.          break;
  123.    }
  124.    if (NeedIcon)
  125.    {
  126.       ShowError("Command must follow a DEFINE or WINDOW command");
  127.       SkipLine();
  128.    }
  129.    return(theCommand);
  130. }
  131.  
  132.  
  133. /*
  134.  *  ContinueCommand()
  135.  *
  136.  *  Do the right thing for each kind of command:
  137.  *
  138.  *  PROGRAM:
  139.  *    If a program structure was allocated,
  140.  *      Get the name of the program
  141.  *      If it was NULL, give an error
  142.  *      If it was the wIconify process, give an error
  143.  *      Otherwise, check that the program was not already defined
  144.  *    End the command
  145.  *
  146.  *  SCREEN:
  147.  *    If a screen structure was allocated,
  148.  *      Read the screen name.
  149.  *      Check if the screen matches one that already is defined.
  150.  *      If so, show an error
  151.  *    End the command
  152.  *
  153.  *  WINDOW:
  154.  *    If the window structure has been allocated,
  155.  *      Read the window name
  156.  *      Check if the name matches one that is already defined
  157.  *      If so, show an error.
  158.  *    End the command
  159.  *
  160.  *  ICON:
  161.  *    If the next word is FROM,
  162.  *      Get the next word (the Icon file name) and read the file
  163.  *    Otherwise
  164.  *      Get the full line as the icon's name
  165.  *      Find the icon in the definition list, and copy it, if it exists,
  166.  *        Otherwise show an error
  167.  *    End the command
  168.  *
  169.  *  IMAGE, SELECT, MASK:
  170.  *    Read the next line of the image; we must be at the end of the line
  171.  *
  172.  *  FLAGS:
  173.  *    Read the next word as a flag and copy it to the current icon
  174.  *
  175.  *  POSITION:
  176.  *    Get the specified position and copy it to the icon
  177.  *    We must be at the end of the line, and end the command
  178.  *
  179.  *  NAME:
  180.  *    If the icon has a previous (allocated) name, free it
  181.  *    Clear the pointer and read a new name
  182.  *    If the name was allocated, mark it as such
  183.  *    Copy the name to the icon
  184.  *    We must be at the end of the line, and end the command
  185.  *
  186.  *  DEFINE:
  187.  *    If the define structure was allocated,
  188.  *      Read the definition name
  189.  *      If it was NULL or ANY, show an error
  190.  *      Otherwise, check if it matched a previously defined icon
  191.  *    End the command
  192.  *
  193.  *  If we are supposed to be at the end of the line but aren't,
  194.  *    Give an error and skip the rest of the line.
  195.  */
  196.  
  197. static int ContinueCommand(theCommand)
  198. int theCommand;
  199. {
  200.    int EndOfLine = FALSE;
  201.    ICONDEFINE *theDefine;
  202.    ICONWINDOW *theWindow;
  203.    ICONSCREEN *theScreen;
  204.    ICONPROGRAM *theProgram;
  205.  
  206.    switch(theCommand)
  207.    {
  208.       case COM_PROGRAM:
  209.          if (PROGRAM)
  210.          {
  211.             ReadName(&(PROGRAM->Name));
  212.             if (PROGRAM->Name == NAME_NULL)
  213.             {
  214.                ShowError("NULL is illegal as a PROGRAM name");
  215.                CopyName(&(PROGRAM->Name),Null);
  216.             } else if (stricmp(PROGRAM->Name,ICONPROCNAME) == 0) {
  217.                ShowError("wIcoinfy process itself can not be specified");
  218.                CopyName(&(PROGRAM->Name),Null);
  219.             } else {
  220.                theProgram = FirstProgram;
  221.                while (theProgram != PROGRAM)
  222.                {
  223.                   if (MATCH(theProgram->Name,PROGRAM->Name))
  224.                   {
  225.                      ShowError("Program '%s' defined twice",NAME(PROGRAM));
  226.                      theProgram = PROGRAM;
  227.                   } else theProgram = theProgram->Next;
  228.                }
  229.             }
  230.          }
  231.          theCommand = COM_NONE;
  232.          break;
  233.  
  234.       case COM_SCREEN:
  235.          if (SCREEN)
  236.          {
  237.             ReadName(&(SCREEN->Name));
  238.             theScreen = PROGRAM->Screen;
  239.             while (theScreen != SCREEN)
  240.             {
  241.                if (PREFIXMATCH(theScreen->Name,SCREEN->Name))
  242.                {
  243.                   if (MATCH(theScreen->Name,SCREEN->Name))
  244.                      ShowError("Screen '%s' defined twice",NAME(SCREEN));
  245.                     else
  246.                      ShowError("Screen '%s' matches '%s' already defined",
  247.                        NAME(SCREEN),NAME(theScreen));
  248.                   theScreen = SCREEN;
  249.                } else theScreen = theScreen->Next;
  250.             }
  251.          }
  252.          theCommand = COM_NONE;
  253.          break;
  254.  
  255.       case COM_WINDOW:
  256.          if (WINDOW)
  257.          {
  258.             ReadName(&(WINDOW->Name));
  259.             theWindow = SCREEN->Window;
  260.             while (theWindow != WINDOW)
  261.             {
  262.                if (PREFIXMATCH(theWindow->Name,WINDOW->Name))
  263.                {
  264.                   if (MATCH(theWindow->Name,WINDOW->Name))
  265.                      ShowError("Window '%s' defined twice",NAME(WINDOW));
  266.                     else
  267.                      ShowError("Window '%s' matches '%s' already defined",
  268.                        NAME(WINDOW),NAME(theWindow));
  269.                   theWindow = WINDOW;
  270.                } else theWindow = theWindow->Next;
  271.             }
  272.          }
  273.          theCommand = COM_NONE;
  274.          break;
  275.  
  276.       case COM_ICON:
  277.          if (WORDMATCH("FROM"))
  278.          {
  279.             ReadNextWord();
  280.             ReadIconFile();
  281.          } else {
  282.             ReadFullLine();
  283.             theDefine = FindDefine(Word);
  284.             if (theDefine) CopyDefine(theDefine);
  285.               else ShowError("Undefined Icon '%s'",Word);
  286.          }
  287.          theCommand = COM_NONE; 
  288.          break;
  289.  
  290.       case COM_IMAGE:
  291.       case COM_SELECT:
  292.       case COM_MASK:
  293.          ReadImageLine(BitMapLine++);
  294.          EndOfLine = TRUE;
  295.          break;
  296.  
  297.       case COM_FLAGS:
  298.          ReadIconFlags(&(ICON.Flags));
  299.          CopyIconData(COM_FLAGS);
  300.          break;
  301.  
  302.       case COM_POSITION:
  303.          ReadPosition(&(ICON.x),&(ICON.y));
  304.          CopyIconData(COM_POSITION);
  305.          EndOfLine = TRUE;
  306.          theCommand = COM_NONE;
  307.          break;
  308.  
  309.       case COM_NAME:
  310.          if (IconPtr->Flags & IW_NAME) FREECHAR(ICON.Name);
  311.          ICON.Name = NULL; ReadName(&(ICON.Name));
  312.          if (ICON.Name != NAME_NULL && ICON.Name != NAME_ANY)
  313.             IconPtr->Flags |= IW_NAME;
  314.          CopyIconData(COM_NAME);
  315.          EndOfLine = TRUE;
  316.          theCommand = COM_NONE;
  317.          break;
  318.  
  319.       case COM_DEFINE:
  320.          if (DEFINE)
  321.          {
  322.             ReadName(&(DEFINE->Name));
  323.             if (DEFINE->Name == NAME_NULL || DEFINE->Name == NAME_ANY)
  324.             {
  325.                ShowError("NULL and ANY are illegal as ICON names");
  326.                CopyName(&(DEFINE->Name),Null);
  327.             } else {
  328.                theDefine = FirstDefine;
  329.                while (theDefine != DEFINE)
  330.                {
  331.                   if (MATCH(theDefine->Name,DEFINE->Name))
  332.                   {
  333.                      ShowError("Icon '%s' defined twice",NAME(DEFINE));
  334.                      theDefine = DEFINE;
  335.                   } else theDefine = theDefine->Next;
  336.                }
  337.             }
  338.          }
  339.          theCommand = COM_NONE;
  340.          break;
  341.    }
  342.    if (EndOfLine && NextChar != '\n')
  343.    {
  344.       ReadNextWord();
  345.       ShowError("'%s' seen where End-of-Line was expected",Word);
  346.       SkipLine();
  347.    }
  348.    return(theCommand);
  349. }
  350.  
  351.  
  352. /*
  353.  *  EndCommand()
  354.  *
  355.  *  Finish the command that is in progress, and check that all parameters,
  356.  *  etc, have been included.  If not, give a message about what was
  357.  *  expected to be seen.  For the image commands, copy the image to the
  358.  *  correct image pointer.
  359.  */
  360.  
  361. static void EndCommand(theCommand,newCommand)
  362. int theCommand,newCommand;
  363. {
  364.    char *Wanted = NULL;
  365.  
  366.    switch(theCommand)
  367.    {
  368.       case COM_PROGRAM:
  369.          if (PROGRAM && PROGRAM->Name == NAME_ANY)
  370.             Wanted = "a Program Name", PROGRAM->Name = "";
  371.          break;
  372.  
  373.       case COM_SCREEN:
  374.          if (ScreenPtr && SCREEN && SCREEN->Name == NAME_ANY)
  375.             Wanted = "a Screen Name", CopyName(SCREEN->Name,Null);
  376.          break;
  377.  
  378.       case COM_WINDOW:
  379.          if (WindowPtr && WINDOW && WINDOW->Name == NAME_ANY)
  380.             Wanted = "a Window Name", CopyName(WINDOW->Name,Null);
  381.          break;
  382.  
  383.       case COM_IMAGE:
  384.          if (BitMapLine == 0) Wanted = "Image Data";
  385.          else if (IconPtr)
  386.          {
  387.             if (IconPtr->Flags & IW_IMAGE) FreeImage(ICON.Image);
  388.             SetImage(&(ICON.Image)); IconPtr->Flags |= IW_IMAGE;
  389.             CopyIconData(COM_IMAGE);
  390.          }
  391.          break;
  392.  
  393.       case COM_SELECT:
  394.          if (BitMapLine == 0) Wanted = "Image Data";
  395.          else if (IconPtr)
  396.          {
  397.             if (IconPtr->Flags & IW_SELECT) FreeImage(ICON.Select);
  398.             SetImage(&(ICON.Select)); IconPtr->Flags |= IW_SELECT;
  399.             CopyIconData(COM_SELECT);
  400.          }
  401.          break;
  402.  
  403.       case COM_MASK:
  404.          if (BitMapLine == 0) Wanted = "Mask Data";
  405.          else if (IconPtr)
  406.          {
  407.             if (IconPtr->Flags & IW_MASK) FreeImage(MASK);
  408.             SetImage(MaskPtr); IconPtr->Flags |= IW_MASK;
  409.             if (MASK) ICON.Mask = MASK->ImageData;
  410.             CopyIconData(COM_MASK);
  411.          }
  412.          break;
  413.  
  414.       case COM_ICON:
  415.          Wanted = "an Icon Name or FROM File";
  416.          break;
  417.  
  418.       case COM_POSITION:
  419.          Wanted = "an (x,y) position";
  420.          break;
  421.  
  422.       case COM_NAME:
  423.          Wanted = "an Icon Name";
  424.          break;
  425.  
  426.       case COM_DEFINE:
  427.          if (DEFINE && DEFINE->Name == NULL)
  428.             Wanted = "an Icon Name", DEFINE->Name = "";
  429.          break;
  430.    }
  431.    if (Wanted)
  432.    {
  433.       if (newCommand > COM_LAST) Expected(Wanted); else
  434.          ShowError("Keyword '%s' seen where %s was expected",
  435.             ComName[newCommand],Wanted);
  436.    }
  437. }
  438.  
  439.  
  440. /*
  441.  *  ReadFile()
  442.  *
  443.  *  While there is more data in the file,
  444.  *    Read the next word from the file and interpret it as a command
  445.  *    If the word was a command and there is an old command in progress
  446.  *      End the old command
  447.  *    If the new command is
  448.  *      Not a command:
  449.  *        If there is a command in progress, do it to the current word,
  450.  *        Otherwise give an error and skip to the next line.
  451.  *      Unknown:
  452.  *        Give a message and skip to the next line.
  453.  *      An End-Of-File:
  454.  *        If an icon file is currently being read, close it,
  455.  *        Otherwise set the end-of-file marker to TRUE.
  456.  *      Anything else (i.e., a legitimate command):
  457.  *        Mark the command as the old command, and setup whatever is needed.
  458.  */
  459.  
  460. static void ReadFile(theFile)
  461. APTR theFile;
  462. {
  463.    int PendingCommand = COM_NONE;
  464.    int NewCommand;
  465.    int EOF = FALSE;
  466.  
  467.    while (!EOF)
  468.    {
  469.       NewCommand = ReadCommand();
  470.       if (NewCommand != COM_NONE && PendingCommand != COM_NONE)
  471.          EndCommand(PendingCommand,NewCommand), PendingCommand = COM_NONE;
  472.       switch(NewCommand)
  473.       {
  474.          case COM_NONE:
  475.             if (PendingCommand != COM_NONE)
  476.                PendingCommand = ContinueCommand(PendingCommand);
  477.               else
  478.                Expected("a Command Keyword"),
  479.                SkipLine();
  480.             break;
  481.  
  482.          case COM_UNKNOWN:
  483.             ShowError("Unrecognized Command '%s'",Word);
  484.             SkipLine();
  485.             break;
  486.  
  487.          case COM_EOF:
  488.             if (IconFileOpen) EndIconFile();
  489.                else EOF = TRUE;
  490.             break;
  491.  
  492.          default:
  493.             PendingCommand = StartCommand(NewCommand);
  494.             break;
  495.       }
  496.    }
  497. }
  498.  
  499.  
  500. /*
  501.  *  ReadInitFile()
  502.  *
  503.  *  Try to open the primary initialization file,
  504.  *    If unsuccessful, try the secondary file.
  505.  *  If a file was openned,
  506.  *    Read the file and then close it.
  507.  *    Sort the program linked-list.
  508.  */
  509.  
  510. void ReadInitFile(Name1,Name2)
  511. char *Name1,*Name2;
  512. {
  513.    if (Name1)
  514.    {
  515.       if (OpenFile(Name1) == FALSE && Name2) OpenFile(Name2);
  516.       if (InFile)
  517.       {
  518.          ReadFile(InFile);
  519.          CloseFile(InFile);
  520.          SortPrograms();
  521.       } else DoExit("Can't open initialization file '%s'",Name1);
  522.    }
  523. }
  524.