OSDN Git Service

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