root/libutil/args.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. args_open
  2. args_open_filelist
  3. args_open_both
  4. args_open_gfind
  5. args_open_nop
  6. args_read
  7. args_close

   1 /*
   2  * Copyright (c) 2010 Tama Communications Corporation
   3  *
   4  * This file is part of GNU GLOBAL.
   5  *
   6  * This program is free software: you can redistribute it and/or modify
   7  * it under the terms of the GNU General Public License as published by
   8  * the Free Software Foundation, either version 3 of the License, or
   9  * (at your option) any later version.
  10  * 
  11  * This program is distributed in the hope that it will be useful,
  12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14  * GNU General Public License for more details.
  15  * 
  16  * You should have received a copy of the GNU General Public License
  17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18  */
  19 
  20 #ifdef HAVE_CONFIG_H
  21 #include <config.h>
  22 #endif
  23 #include <stdio.h>
  24 #include "die.h"
  25 #include "strbuf.h"
  26 #include "gpathop.h"
  27 
  28 #define ARGS_NOP        0
  29 #define ARGS_ARGS       1
  30 #define ARGS_FILELIST   2
  31 #define ARGS_GFIND      3
  32 #define ARGS_BOTH       4
  33 
  34 int type;
  35 const char **argslist;
  36 FILE *ip;
  37 GFIND *gp;
  38 
  39 /**
  40  * args_open:
  41  *
  42  *      @param[in]      args    args array
  43  */
  44 void
  45 args_open(const char **args)
  46 {
  47         type = ARGS_ARGS;
  48         argslist = args;
  49 }
  50 /**
  51  * args_open_filelist: #args_open like interface for handling output of @XREF{find,1}.
  52  *
  53  *      @param[in]      filename        file including list of file names. <br>
  54  *                              When @FILE{-} is specified, read from standard input.
  55  */
  56 void
  57 args_open_filelist(const char *filename)
  58 {
  59         type = ARGS_FILELIST;
  60         if (!strcmp(filename, "-")) {
  61                 ip = stdin;
  62         } else {
  63                 ip = fopen(filename, "r");
  64                 if (ip == NULL)
  65                         die("cannot open '%s'.", filename);
  66         }
  67 }
  68 /**
  69  * args_open_both: #args_open like interface for argument and file list.
  70  *
  71  *      @param[in]      args            args array
  72  *      @param[in]      filename        file including list of file names. <br>
  73  *                              When @FILE{-} is specified, read from standard input.
  74  */
  75 void
  76 args_open_both(const char **args, const char *filename)
  77 {
  78         type = ARGS_BOTH;
  79         argslist = args;
  80         if (!strcmp(filename, "-")) {
  81                 ip = stdin;
  82         } else {
  83                 ip = fopen(filename, "r");
  84                 if (ip == NULL)
  85                         die("cannot open '%s'.", filename);
  86         }
  87 }
  88 /**
  89  * args_open_gfind: #args_open like interface for handling output of @NAME{gfind}.
  90  *
  91  *      @param[in]      agp     GFIND descriptor
  92  */
  93 void
  94 args_open_gfind(GFIND *agp)
  95 {
  96         type = ARGS_GFIND;
  97         gp = agp;
  98 }
  99 void
 100 args_open_nop(void)
 101 {
 102         type = ARGS_NOP;
 103 }
 104 /**
 105  * args_read: read path From args.
 106  *
 107  *      @return         path (@VAR{NULL}: end of argument)
 108  */
 109 const char *
 110 args_read(void)
 111 {
 112         const char *p;
 113         STATIC_STRBUF(sb);
 114 
 115         strbuf_clear(sb);
 116         switch (type) {
 117         case ARGS_NOP:
 118                 p = NULL;
 119                 break;
 120         case ARGS_ARGS:
 121                 p = *argslist++;
 122                 break;
 123         case ARGS_FILELIST:
 124                 p = strbuf_fgets(sb, ip, STRBUF_NOCRLF);
 125                 break;
 126         case ARGS_GFIND:
 127                 p = gfind_read(gp);
 128                 break;
 129         case ARGS_BOTH:
 130                 if (*argslist != NULL)
 131                         p = *argslist++;
 132                 else
 133                         p = strbuf_fgets(sb, ip, STRBUF_NOCRLF);
 134                 break;
 135         default:
 136                 die("args_read: illegal type.");
 137         }
 138         return p;
 139 }
 140 /**
 141  * args_close: close args.
 142  */
 143 void
 144 args_close(void)
 145 {
 146         switch (type) {
 147         case ARGS_NOP:
 148         case ARGS_ARGS:
 149                 break;
 150         case ARGS_FILELIST:
 151         case ARGS_BOTH:
 152                 if (ip != NULL && ip != stdin)
 153                         fclose(ip);
 154                 ip = NULL;
 155                 break;
 156         case ARGS_GFIND:
 157                 if (gp != NULL)
 158                         gfind_close(gp);
 159                 gp = NULL;
 160                 break;
 161         default:
 162                 die("something wrong.");
 163         }
 164 }

/* [previous][next][first][last][top][bottom][index][help] */