"Fossies" - the Fresh Open Source Software Archive

Member "cook-2.34/cook/builtin/findstring.c" (25 Sep 2010, 2326 Bytes) of package /linux/misc/old/cook-2.34.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. For more information about "findstring.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *      cook - file construction tool
    3  *      Copyright (C) 1994, 1997-1999, 2006-2008 Peter Miller
    4  *
    5  *      This program is free software; you can redistribute it and/or modify
    6  *      it under the terms of the GNU General Public License as published by
    7  *      the Free Software Foundation; either version 3 of the License, or
    8  *      (at your option) any later version.
    9  *
   10  *      This program is distributed in the hope that it will be useful,
   11  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  *      GNU General Public License for more details.
   14  *
   15  *      You should have received a copy of the GNU General Public License
   16  *      along with this program. If not, see
   17  *      <http://www.gnu.org/licenses/>.
   18  */
   19 
   20 #include <common/ac/string.h>
   21 
   22 #include <cook/builtin/findstring.h>
   23 #include <common/error_intl.h>
   24 #include <cook/expr/position.h>
   25 #include <common/str_list.h>
   26 #include <common/trace.h>
   27 
   28 
   29 static int
   30 contains(string_ty *s1, string_ty *s2)
   31 {
   32     size_t          len;
   33     size_t          j;
   34 
   35     if (s1->str_length == 0)
   36         return 1;
   37     if (s1->str_length > s2->str_length)
   38         return 0;
   39     len = s2->str_length - s1->str_length;
   40     for (j = 0; j <= len; ++j)
   41     {
   42         if (!memcmp(s2->str_text + j, s1->str_text, s1->str_length))
   43             return 1;
   44     }
   45     return 0;
   46 }
   47 
   48 
   49 static int
   50 interpret(string_list_ty *result, const string_list_ty *arg,
   51     const expr_position_ty *pp, const struct opcode_context_ty *ocp)
   52 {
   53     size_t          j;
   54 
   55     trace(("findstring\n"));
   56     (void)ocp;
   57     if (arg->nstrings < 2)
   58     {
   59         sub_context_ty  *scp;
   60 
   61         scp = sub_context_new();
   62         sub_var_set_string(scp, "Name", arg->string[0]);
   63         error_with_position
   64         (
   65             pp,
   66             scp,
   67             i18n("$name: requires one or more arguments")
   68         );
   69         sub_context_delete(scp);
   70         return -1;
   71     }
   72     for (j = 2; j < arg->nstrings; ++j)
   73     {
   74         if (contains(arg->string[1], arg->string[j]))
   75             string_list_append(result, arg->string[1]);
   76         else
   77             string_list_append(result, str_false);
   78     }
   79     return 0;
   80 }
   81 
   82 
   83 builtin_ty builtin_findstring =
   84 {
   85     "findstring",
   86     interpret,
   87     interpret,                  /* script */
   88 };