"Fossies" - the Fresh Open Source Software Archive

Member "rename-1.3/fixtoken.c" (6 May 2002, 3610 Bytes) of package /linux/privat/old/rename-1.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 
    2 /* fixtoken - extract tokens from a string
    3    Version 1.1
    4 
    5    Copyright (C) 1998, 1999  Xuming <xuming@bigfoot.com>
    6    
    7    This program is free software; you can redistribute it and/or 
    8    modify it under the terms of the GNU General Public License as 
    9    published by the Free Software Foundation; either version 2, 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 GNU
   15    General Public License, the file COPYING in this directory, for
   16    more details.
   17    
   18    You should have received a copy of the GNU General Public License
   19    along with this program; if not, write to the Free Software
   20    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
   21 */
   22 
   23 #include <ctype.h>
   24 #include <stdio.h>
   25 
   26 
   27 static int isdelim(char *delim, int ch)
   28 {
   29     while (*delim) {
   30     if (*delim == (char) ch)
   31         return 1;
   32     else if ((*delim == ' ') && isspace(ch))
   33         return 1;
   34     delim++;
   35     }
   36     return 0;
   37 }
   38 
   39 
   40 /* This function splits the string into tokens. Unlike strtok(), the 
   41    number of expected tokens has a fixed limitation. 
   42    This function splits everything between delimiter.
   43  
   44    sour  - the input string that is going to deconstruct to tokens
   45    idx   - the string array for storing tokens
   46    ids   - the maximem number of tokens, the size of "idx"
   47    delim - the delimiter array, each character in the array takes effect.
   48 
   49    It returns the number of token extracted.
   50 
   51    For example, fixtoken("#abc  wdc:have:::#:debug", idx, 16, "# :") returns
   52    10 tokens "", "abc", "", "wdc", "have", "", "", "", "" and "debug".
   53    
   54    BUGS:  'sour' is changed.
   55 */
   56 
   57 int fixtoken(char *sour, char **idx, int ids, char *delim)
   58 {
   59     int  i;
   60 
   61     for (i = 0; i < ids; idx[i++] = NULL);
   62     
   63     i = 0;
   64     for (idx[i++] = sour; *sour && (i < ids); sour++)  {
   65 
   66     if (isdelim(delim, *sour))  {
   67         *sour = 0;
   68         idx[i++] = sour + 1;
   69     }
   70     }
   71     return i;
   72 }
   73 
   74 /* This function splits the string into tokens. Unlike strtok(), the 
   75    number of expected tokens has a fixed limitation. 
   76    This function doesn't work like fixtoken(), it looks continent delimiters
   77    as one single delimter.
   78  
   79    sour  - the input string that is going to deconstruct to tokens
   80    idx   - the string array for storing tokens
   81    ids   - the maximem number of tokens, the size of "idx"
   82    delim - the delimiter array, each character in the array takes effect.
   83 
   84    It returns the number of token extracted.
   85 
   86    For example, fixtoken("#abc  wdc:have:::#:debug", idx, 16, "# :") returns
   87    4 tokens "abc", "wdc", "have" and "debug".
   88    
   89    BUGS:  'sour' is changed.
   90 */
   91 
   92 int ziptoken(char *sour, char **idx, int ids, char *delim)
   93 {
   94     int  i, ss;
   95 
   96     for (i = 0; i < ids; idx[i++] = NULL);
   97 
   98     for (i = ss = 0; *sour && (i < ids); sour++)  {
   99     
  100     if (isdelim(delim, *sour))  {
  101         ss = 0;
  102         *sour = 0;
  103     } else if (ss == 0) {
  104         ss = 1;
  105         idx[i++] = sour;
  106     }
  107     }
  108     
  109     return i;
  110 }
  111     
  112 
  113 #ifdef  EXECUTABLE
  114 
  115 #include <string.h>
  116 
  117 int main(int argc, char **argv)
  118 {
  119     char buf[256], *idx[32], *delim = " ";
  120     int  rs, i;
  121 
  122     if (argc > 1)  {
  123     if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
  124         printf("Usage: %s [--help] [DELIM]\n", argv[0]);
  125         return 0;
  126     } else
  127         delim = argv[1];
  128     }
  129     
  130     while (fgets(buf, 256, stdin)) {
  131 
  132     buf[strlen(buf) - 1] = 0;
  133     
  134     rs = fixtoken(buf, idx, 32, delim);
  135     printf("%d items: ", rs);
  136     for (i = 0; i < rs; i++) 
  137         printf("\"%s\", ", idx[i]);
  138     printf("\n");
  139     }
  140     return 0;
  141 }
  142 
  143 #endif
  144