OSDN Git Service

add doxycomment
[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 <unistd.h>
27 #include <ccunit/CCUnitMakeSuite.h>
28 #include <ccunit/CCUnitReadSuite.h>
29 #include <ccunit/CCUnitWriteSuite.h>
30 #include <ccunit/CCUnitLogMessage.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <stdarg.h>
34
35 /**
36  * If `setvbuf' takes the buffering type as its second argument and
37  * the buffer pointer as the third, instead of the other way around.
38  */
39 #if SETVBUF_REVERSED
40 #  define SETVBUF(FP,PTR,TYP,SZ) setvbuf(FP,TYP,PTR,SZ)
41 #else
42 #  define SETVBUF(FP,PTR,TYP,SZ) setvbuf(FP,PTR,TYP,SZ)
43 #endif
44
45 #if defined(unix) || defined(__MACH__) || defined(__CYGWIN__)
46 #define DIRSEP '/'                              /**< directory separetor */
47 #elif defined (__WIN32__) || defined (__MSDOS__)
48 #define DIRSEP '\\'                             /**< directory separetor */
49 #else
50 #define DIRSEP '/'                              /**< directory separetor */
51 #endif
52
53 #if HAVE_GETOPT_LONG
54 #  define LONGOPTNAME(LO) "--" #LO " | "        /**< long name option */
55 #else
56 #  define LONGOPTNAME(LO) /* -- LO | */         /**< long name option */
57 #endif
58
59 /**
60  * @addtogroup CCUnitMakeSuite
61  * @{
62  */
63
64 int ccunit_makeSuite (int ac, char** av)
65 {
66   CCUnitTestSuiteDef* suite;
67   char* outputFile = "-";
68   const char* functionName = "ccunit_suite";
69   FILE* ofp = stdout;
70   int opt;
71   char* progname = NULL;
72 #ifdef HAVE_GETOPT_LONG
73   int option_index = 0;
74   static struct option long_options[] = {
75     { "output", required_argument, NULL, 'o' },
76     { "function", required_argument, NULL, 'f' },
77     { "verbose", no_argument, NULL, 'v' },
78     { "debug", no_argument, NULL, 'd' },
79     { "version", no_argument, NULL, 'V' },
80     { "help", no_argument, NULL, 'h' },
81     { NULL, 0, NULL, 0 }
82   };
83 #endif
84   if (av[0])
85     {
86       progname = strrchr (av[0], DIRSEP);
87       if (!progname)
88         {
89           progname = av[0];
90 #         if defined (__WIN32__) || defined (__MSDOS__)
91           if (progname[2] == ':')
92             progname += 2;
93 #         endif
94         }
95       else
96         progname ++;
97     }
98   for (;;)
99     {
100 #ifdef HAVE_GETOPT_LONG
101       opt = getopt_long (ac, av, "o:f:vdVh", long_options, &option_index);
102 #else
103       opt = getopt (ac, av, "o:f:vdVh");
104 #endif
105       if (opt == -1)
106         break;
107       switch (opt)
108         {
109         case 0:
110 #ifdef HAVE_GETOPT_LONG
111           puts (long_options[option_index].name);
112 #endif
113           break;
114         case 'o':
115           outputFile = optarg;
116           break;
117         case 'f':
118           functionName = optarg;
119           break;
120         case 'v':
121           _ccunit_verbose_message = !_ccunit_verbose_message;
122           break;
123         case 'd':
124           _ccunit_debug_message = !_ccunit_debug_message;
125           break;
126         case 'V':
127           fprintf (stdout, "%s - %s\n", PACKAGE_STRING, progname);
128           return 0;
129         case 'h':
130           fprintf (stdout,
131                    "%s\n"
132                    "  USAGE: %s [OPTIONS] FILES...\n"
133                    "    OPTION:\n"
134                    "      " LONGOPTNAME(output) "-o OUTFILE\n"
135                    "          output file name (default stdout)\n"
136                    "      " LONGOPTNAME(function) "-f FUNCTION\n"
137                    "          created function name\n"
138                    "      " LONGOPTNAME(verbose) "-v\n"
139                    "          output verbose message\n"
140                    "      " LONGOPTNAME(debug) "-d\n"
141                    "          output debug message\n"
142                    "      " LONGOPTNAME(version) "-V\n"
143                    "          print version\n"
144                    "      " LONGOPTNAME(help) "-h\n"
145                    "          print this message\n"
146                    , PACKAGE_STRING, progname);
147           return 1;
148         case '?':
149           fprintf (stderr, "unknown option %s\n", av[optind]);
150           return 1;
151         case ':':
152           fprintf (stderr, "bad option argument: %s\n", av[optind]);
153           return 1;
154         }
155     }
156   suite = ccunit_newTestSuiteDef (NULL);
157   if (!suite)
158     {
159       ccunit_err ("can't create test suite def. not enough memory.");
160       return -1;
161     }
162   if (strcmp (outputFile, "-") == 0)
163     ofp = stdout;
164   else
165     {
166       ofp = fopen (outputFile, "w");
167       if (!ofp)
168         {
169           fprintf (stderr, "can't open file '%s': %s.\n",
170                    outputFile, strerror (errno));
171           return 2;
172         }
173     }
174   SETVBUF(ofp, NULL, _IONBF, 0);
175   SETVBUF(stderr, NULL, _IONBF, 0);
176   /* process source files */
177   if (optind >= ac)
178     ccunit_readSuite ("-", suite);
179   else
180     for (; optind < ac; optind ++)
181       ccunit_readSuite (av[optind], suite);
182   ccunit_writeSuite (ofp, functionName, suite);
183   if (ofp != stdout)
184     fclose (ofp);
185   ccunit_deleteTestSuiteDef (suite);
186   return 0;
187 }
188
189 int ccunit_va_makeSuite (const char* prg, ...)
190 {
191   va_list args;
192   size_t capacity = 10;
193   char** av = calloc (10, sizeof (char*));
194   int ac = 0;
195   av[0] = (char*)prg;
196   va_start (args, prg);
197   while (av[ac])
198     {
199       ac ++;
200       if (ac >= capacity)
201         {
202           capacity *= 2;
203           av = realloc (av, capacity * sizeof (char*));
204         }
205       av[ac] = va_arg (args, char*);
206     }
207   va_end (args);
208   return ccunit_makeSuite (ac, av);
209 }
210
211 /** @} */