home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / internet-tools / ipdial / source / vsscanf.c < prev   
Encoding:
C/C++ Source or Header  |  1996-04-02  |  4.6 KB  |  192 lines

  1. /**
  2. ***  IPDial - Vsscanf.c
  3. ***
  4. ***  This file implements a function very similar to sscanf().
  5. ***
  6. ***
  7. ***  This program is free software; you can redistribute it and/or modify
  8. ***  it under the terms of the GNU General Public License as published by
  9. ***  the Free Software Foundation; either version 2 of the License, or
  10. ***  (at your option) any later version.
  11. ***
  12. ***  This program is distributed in the hope that it will be useful,
  13. ***  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ***  GNU General Public License for more details.
  16. ***
  17. ***  You should have received a copy of the GNU General Public License
  18. ***  along with this program; if not, write to the Free Software
  19. ***  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. ***
  21. ***  Authors: Jochen Wiedmann <wiedmann@neckar-alb.de>
  22. ***           Stefan Gybas <cab@studbox.uni-stuttgart.de>
  23. **/
  24.  
  25.  
  26.  
  27.  
  28. /**
  29. ***  Include files
  30. **/
  31. #ifndef IPDIAL_H
  32. #include "IPDial.h"
  33. #endif
  34. #include <ctype.h>
  35.  
  36.  
  37.  
  38.  
  39. /**
  40. ***  Scan the character buf for a string like %} and terminate it
  41. ***  with a '\0'. Return the pointer after %}
  42. **/
  43. STRPTR ReadWord(STRPTR buf, UBYTE c)
  44.  
  45. { while(*buf)
  46.   { if (*buf == '%')
  47.     { char *ptr;
  48.  
  49.       if (buf[1] == c)
  50.       { *buf = '\0';
  51.         return(buf+2);
  52.       }
  53.  
  54.       ptr = (char*) buf;
  55.       while (ptr[1])
  56.       { *ptr = ptr[1];
  57.         ++ptr;
  58.       }
  59.       *ptr = '\0';
  60.     }
  61.     else
  62.     { ++buf;
  63.     }
  64.   }
  65.   return(buf);
  66. }
  67.  
  68.  
  69. /**
  70. ***  This is a function very similar to sscanf: A buffer is scanned
  71. ***  according to a format string.
  72. ***
  73. ***  The format string is interpreted as follows:
  74. ***
  75. ***     - The sequence %{WORD%} means any number of characters until the
  76. ***       word WORD is found. (Use %% to include the character '%'
  77. ***       into WORD itself.
  78. ***     - A blank means any number (including 0) and kind (blank, tab,
  79. ***       return, line feed, form feed) of white space characters.
  80. ***     - The sequence %[VAR%]%(SUFFIX%) means to store the following
  81. ***       white space separated word into the environment variable VAR.
  82. ***       The optional SUFFIX will be removed, if it is present.
  83. ***
  84. ***  Other characters are simply ignored.
  85. ***
  86. ***  The function returns the number of environment variables created.
  87. **/
  88. LONG Vsscanf(STRPTR buffer, STRPTR format, ULONG flags)
  89.  
  90. { char *buf, *fmt;
  91.   int result;
  92.  
  93.   /*  Copy strings, so that we can modify them. */
  94.   if (!(buf = strdup((char*) buffer))  ||
  95.       !(fmt = strdup((char*) format)))
  96.   { PrintError(0, "not enough memory");
  97.   }
  98.   buffer = (STRPTR) buf;
  99.   format = (STRPTR) fmt;
  100.  
  101.   result = 0;
  102.   while (*fmt  &&  *buf)
  103.   { if (isspace(*fmt))
  104.     { while (isspace(*buf))
  105.       { ++buf;
  106.       }
  107.       ++fmt;
  108.     }
  109.     else if (*fmt == '%')
  110.     { ++fmt;
  111.       if (*fmt == '{')
  112.       { char *word;
  113.         int len;
  114.  
  115.         /* Read word to match    */
  116.         word = fmt+1;
  117.         fmt = (char*) ReadWord((STRPTR) word, '}');
  118.         len = strlen(word);
  119.  
  120.         /*  Word read, look into buffer  */
  121.         if (!len)
  122.         { continue;
  123.         }
  124.  
  125.         while (*buf  &&  strncmp(buf, word, len) != 0)
  126.         { ++buf;
  127.         }
  128.         if (*buf)
  129.         { buf += len;
  130.         }
  131.       }
  132.       else if (*fmt == '[')
  133.       { char *var, *val, *suffix;
  134.  
  135.         /*  Read variable name.     */
  136.         var = fmt+1;
  137.         fmt = (char*) ReadWord((STRPTR) var, ']');
  138.  
  139.         /*  Read suffix             */
  140.         if (strncmp(fmt, "%(", 2) == 0)
  141.         { suffix = fmt+2;
  142.           fmt = (char*) ReadWord((STRPTR) suffix, ')');
  143.         }
  144.         else
  145.         { suffix = "";
  146.         }
  147.  
  148.         /*  Read next word from buffer  */
  149.         val = buf;
  150.         while (*buf  &&  !isspace(*buf))
  151.         { ++buf;
  152.         }
  153.         if (*buf)
  154.         { *buf = '\0';
  155.           ++buf;
  156.         }
  157.  
  158.         /*  Check for suffix            */
  159.         if (strlen(suffix) < strlen(val)  &&
  160.             strcmp(val + strlen(val) - strlen(suffix), suffix) == 0)
  161.         { val[strlen(val) - strlen(suffix)] = '\0';
  162.         }
  163.  
  164.         /*  Set environment variable    */
  165.         if (*var)
  166.         { if (VerboseMode)
  167.           { fprintf(stderr, "Setting environment variable %s to value %s.\n",
  168.                     var, val);
  169.           }
  170.           if (!SetVar((STRPTR) var, (STRPTR) val, -1, LV_VAR | flags))
  171.           { fprintf(stderr, "Error while setting environment variable %s to value %s.\n",
  172.                     var, val);
  173.           }
  174.           ++result;
  175.         }
  176.       }
  177.       else
  178.       { /*  Ignore this character.    */
  179.         ++fmt;
  180.       }
  181.     }
  182.     else
  183.     { ++fmt;     /*  Ignore this character   */
  184.     } 
  185.   }
  186.  
  187.   free(buffer);
  188.   free(format);
  189.  
  190.   return(result);
  191. }
  192.