OSDN Git Service

(no commit message)
[ccunit/ccunit.git] / src / ccunit / CCUnitMakeSuite.c
1 /*
2  * $Id$
3  */
4 /* Copyright (C) 2003 TSUTSUMI Kikuo.
5    This file is part of the CCUnit Library.
6
7    The CCUnit Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public License
9    as published by the Free Software Foundation; either version 2.1 of
10    the License, or (at your option) any later version.
11
12    The CCUnit Library is distributed in the hope that it will be
13    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU Lesser General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with the CCUnit Library; see the file COPYING.LESSER.
19    If not, write to the Free Software Foundation, Inc., 59 Temple
20    Place - Suite 330, Boston, MA 02111-1307, USA.  
21 */
22
23 #include <unistd.h>
24 #include <ccunit/CCUnitMakeSuite.h>
25 #include <ccunit/CCUnitLogMessage.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <stdarg.h>
29
30 #ifndef AC_FUNC_SETVBUF_REVERSED
31 #  define SETVBUF(FP,PTR,TYP,SZ) setvbuf(FP,PTR,TYP,SZ)
32 #else
33 #  define SETVBUF(FP,PTR,TYP,SZ) setvbuf(FP,TYP,PTR,SZ)
34 #endif
35
36 #if defined(unix) || defined(__MACH__) || defined(__CYGWIN__)
37 #define DIRSEP '/'
38 #elif defined (__WIN32__) || defined (__MSDOS__)
39 #define DIRSEP '\\'
40 #else
41 #define DIRSEP '/'
42 #endif
43
44 #if HAVE_GETOPT_LONG
45 #  define LONGOPTNAME(LO) "--" #LO " | "
46 #else
47 #  define LONGOPTNAME(LO) /* -- LO | */
48 #endif
49
50 /**
51  * The main function.  launch genregsuite function.
52  *
53  * @param ac number of arguments.
54  *
55  * @param av input source files.
56  *
57  * @return zero when successfuly generated. non-zero when error occurred. 
58  */
59 int ccunit_makeSuite (int ac, char** av)
60 {
61   CCUnitTestSuiteDef* suite;
62   char* outputFile = "-";
63   const char* functionName = "ccunit_suite";
64   FILE* ofp = stdout;
65   int opt;
66   char* progname = NULL;
67 #ifdef HAVE_GETOPT_LONG
68   int option_index = 0;
69   static struct option long_options[] = {
70     { "output", required_argument, NULL, 'o' },
71     { "function", required_argument, NULL, 'f' },
72     { "verbose", no_argument, NULL, 'v' },
73     { "debug", no_argument, NULL, 'd' },
74     { "version", no_argument, NULL, 'V' },
75     { "help", no_argument, NULL, 'h' },
76     { NULL, 0, NULL, 0 }
77   };
78 #endif
79   if (av[0])
80     {
81       progname = strrchr (av[0], DIRSEP);
82       if (!progname)
83         {
84           progname = av[0];
85 #         if defined (__WIN32__) || defined (__MSDOS__)
86           if (progname[2] == ':')
87             progname += 2;
88 #         endif
89         }
90       else
91         progname ++;
92     }
93   for (;;)
94     {
95 #ifdef HAVE_GETOPT_LONG
96       opt = getopt_long (ac, av, "o:f:vdVh", long_options, &option_index);
97 #else
98       opt = getopt (ac, av, "o:f:vdVh");
99 #endif
100       if (opt == -1)
101         break;
102       switch (opt)
103         {
104         case 0:
105 #ifdef HAVE_GETOPT_LONG
106           puts (long_options[option_index].name);
107 #endif
108           break;
109         case 'o':
110           outputFile = optarg;
111           break;
112         case 'f':
113           functionName = optarg;
114           break;
115         case 'v':
116           _ccunit_verbose_message = !_ccunit_verbose_message;
117           break;
118         case 'd':
119           _ccunit_debug_message = !_ccunit_debug_message;
120           break;
121         case 'V':
122           fprintf (stdout, "%s - %s\n", PACKAGE_STRING, progname);
123           return 0;
124         case 'h':
125           fprintf (stdout,
126                    "%s\n"
127                    "  USAGE: %s [OPTIONS] FILES...\n"
128                    "    OPTION:\n"
129                    "      " LONGOPTNAME(output) "-o OUTFILE\n"
130                    "          output file name (default stdout)\n"
131                    "      " LONGOPTNAME(function) "-f FUNCTION\n"
132                    "          created function name\n"
133                    "      " LONGOPTNAME(verbose) "-v\n"
134                    "          output verbose message\n"
135                    "      " LONGOPTNAME(debug) "-d\n"
136                    "          output debug message\n"
137                    "      " LONGOPTNAME(version) "-V\n"
138                    "          print version\n"
139                    "      " LONGOPTNAME(help) "-h\n"
140                    "          print this message\n"
141                    , PACKAGE_STRING, progname);
142           return 1;
143         case '?':
144           fprintf (stderr, "unknown option %s\n", av[optind]);
145           return 1;
146         case ':':
147           fprintf (stderr, "bad option argument: %s\n", av[optind]);
148           return 1;
149         }
150     }
151   if (strcmp (outputFile, "-") == 0)
152     ofp = stdout;
153   else
154     {
155       ofp = fopen (outputFile, "w");
156       if (!ofp)
157         {
158           fprintf (stderr, "can't open file '%s': %s.\n",
159                    outputFile, strerror (errno));
160           return 2;
161         }
162     }
163   SETVBUF(ofp, NULL, _IONBF, 0);
164   SETVBUF(stderr, NULL, _IONBF, 0);
165   suite = ccunit_newTestSuiteDef (NULL);
166   /* process source files */
167   if (optind >= ac)
168     ccunit_readTestDef ("-", suite);
169   else
170     for (; optind < ac; optind ++)
171       {
172         ccunit_readTestDef (av[optind], suite);
173       }
174   ccunit_writeTestAdd (ofp, functionName, suite);
175   if (ofp != stdout)
176     fclose (ofp);
177   ccunit_deleteTestSuiteDef (suite);
178   return 0;
179 }
180
181 /**
182  * The main function.  launch genregsuite function.
183  *
184  * @param arg input source files.
185  *
186  * @return zero when successfuly generated. non-zero when error occurred. 
187  */
188 int ccunit_va_makeSuite (const char* prg, ...)
189 {
190   va_list args;
191   size_t capacity = 10;
192   char** av = calloc (10, sizeof (char*));
193   int ac = 0;
194   av[0] = (char*)prg;
195   va_start (args, prg);
196   while (av[ac])
197     {
198       ac ++;
199       if (ac >= capacity)
200         {
201           capacity *= 2;
202           av = realloc (av, capacity * sizeof (char*));
203         }
204       av[ac] = va_arg (args, char*);
205     }
206   va_end (args);
207   return ccunit_makeSuite (ac, av);
208 }