~ubuntu-branches/ubuntu/maverick/codelite/maverick

« back to all changes in this revision

Viewing changes to sdk/codelite_indexer/libctags/args.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-02-10 02:27:55 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090210022755-m5692nfc1t5uf1w9
Tags: 1.0.2759+dfsg-0ubuntu1
* New upstream release (LP: #327216).
* debian/patches/series, debian/patches/00_fix-ia64-build.patch:
  + Dropped, applied upstream already.
* debian/patches/02_fix-desktop.patch,
  debian/patches/03_fix-sh.patch:
  + Refreshed to patch cleanly.
* debian/rules:
  + Make get-orig-source honour UPSTREAM_VERSION if set.
* debian/ctags-le.1,
  debian/codelite_indexer.1,
  debian/codelite.manpages:
  + Dropped ctags-le manpage, since ctags-le was replaced by
    codelite_indexer.
  + Added codelite_indexer manpage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
*   $Id: args.c,v 1.7 2006/05/30 04:37:11 darren Exp $
 
3
*
 
4
*   Copyright (c) 1999-2002, Darren Hiebert
 
5
*
 
6
*   This source code is released for free distribution under the terms of the
 
7
*   GNU General Public License.
 
8
*
 
9
*   This module contains functions for reading command line arguments.
 
10
*/
 
11
 
 
12
/*
 
13
*   INCLUDE FILES
 
14
*/
 
15
#include "general.h"  /* must always come first */
 
16
 
 
17
#include <stdio.h>
 
18
#include <string.h>
 
19
#include <ctype.h>
 
20
 
 
21
#include "args.h"
 
22
#include "debug.h"
 
23
#include "routines.h"
 
24
 
 
25
/*
 
26
*   FUNCTION DEFINITIONS
 
27
*/
 
28
 
 
29
static char *nextStringArg (const char** const next)
 
30
{
 
31
        char* result = NULL;
 
32
        const char* start;
 
33
 
 
34
        Assert (*next != NULL);
 
35
        for (start = *next  ;  isspace ((int) *start)  ;  ++start)
 
36
                ;
 
37
        if (*start == '\0')
 
38
                *next = start;
 
39
        else
 
40
        {
 
41
                size_t length;
 
42
                const char* end;
 
43
 
 
44
                for (end = start ;  *end != '\0'  &&  ! isspace ((int) *end)  ;  ++end)
 
45
                        ;
 
46
                length = end - start;
 
47
                Assert (length > 0);
 
48
                result = xMalloc (length + 1, char);
 
49
                strncpy (result, start, length);
 
50
                result [length] = '\0';
 
51
                *next = end;
 
52
        }
 
53
        return result;
 
54
}
 
55
 
 
56
static char* nextStringLine (const char** const next)
 
57
{
 
58
        char* result = NULL;
 
59
        size_t length;
 
60
        const char* end;
 
61
 
 
62
        Assert (*next != NULL);
 
63
        for (end = *next ;  *end != '\n'  &&  *end != '\0' ;  ++end)
 
64
                ;
 
65
        length = end - *next;
 
66
        if (length > 0)
 
67
        {
 
68
                result = xMalloc (length + 1, char);
 
69
                strncpy (result, *next, length);
 
70
                result [length] = '\0';
 
71
        }
 
72
        if (*end == '\n')
 
73
                ++end;
 
74
        else if (*end == '\r')
 
75
        {
 
76
                ++end;
 
77
                if (*end == '\n')
 
78
                        ++end;
 
79
        }
 
80
        *next = end;
 
81
        return result;
 
82
}
 
83
 
 
84
static char* nextString (const Arguments* const current, const char** const next)
 
85
{
 
86
        char* result;
 
87
        if (current->lineMode)
 
88
                result = nextStringLine (next);
 
89
        else
 
90
                result = nextStringArg (next);
 
91
        return result;
 
92
}
 
93
 
 
94
static char* nextFileArg (FILE* const fp)
 
95
{
 
96
        char* result = NULL;
 
97
        Assert (fp != NULL);
 
98
        if (! feof (fp))
 
99
        {
 
100
                vString* vs = vStringNew ();
 
101
                int c;
 
102
                do
 
103
                        c = fgetc (fp);
 
104
                while (isspace (c));
 
105
 
 
106
                if (c != EOF)
 
107
                {
 
108
                        do
 
109
                        {
 
110
                                vStringPut (vs, c);
 
111
                                c = fgetc (fp);
 
112
                        } while (c != EOF  &&  ! isspace (c));
 
113
                        vStringTerminate (vs);
 
114
                        Assert (vStringLength (vs) > 0);
 
115
                        result = xMalloc (vStringLength (vs) + 1, char);
 
116
                        strcpy (result, vStringValue (vs));
 
117
                }
 
118
                vStringDelete (vs);
 
119
        }
 
120
        return result;
 
121
}
 
122
 
 
123
static char* nextFileLine (FILE* const fp)
 
124
{
 
125
        char* result = NULL;
 
126
        if (! feof (fp))
 
127
        {
 
128
                vString* vs = vStringNew ();
 
129
                int c;
 
130
 
 
131
                Assert (fp != NULL);
 
132
                c = fgetc (fp);
 
133
                while (c != EOF)
 
134
                {
 
135
                        if (c != '\n'  &&  c != '\r')
 
136
                                vStringPut (vs, c);
 
137
                        else if (vStringLength (vs) > 0)
 
138
                                break;
 
139
                        c = fgetc (fp);
 
140
                }
 
141
                if (c != EOF  ||  vStringLength (vs) > 0)
 
142
                {
 
143
                        if (c == '\r')
 
144
                        {
 
145
                                c = fgetc (fp);
 
146
                                if (c != '\n')
 
147
                                        c = ungetc (c, fp);
 
148
                        }
 
149
                        vStringTerminate (vs);
 
150
                        result = xMalloc (vStringLength (vs) + 1, char);
 
151
                        strcpy (result, vStringValue (vs));
 
152
                        vStringDelete (vs);
 
153
                }
 
154
        }
 
155
        return result;
 
156
}
 
157
 
 
158
static char* nextFileString (const Arguments* const current, FILE* const fp)
 
159
{
 
160
        char* result;
 
161
        if (current->lineMode)
 
162
                result = nextFileLine (fp);
 
163
        else
 
164
                result = nextFileArg (fp);
 
165
        return result;
 
166
}
 
167
 
 
168
extern Arguments* argNewFromString (const char* const string)
 
169
{
 
170
        Arguments* result = xMalloc (1, Arguments);
 
171
        memset (result, 0, sizeof (Arguments));
 
172
        result->type = ARG_STRING;
 
173
        result->u.stringArgs.string = string;
 
174
        result->u.stringArgs.item = string;
 
175
        result->u.stringArgs.next = string;
 
176
        result->item = nextString (result, &result->u.stringArgs.next);
 
177
        return result;
 
178
}
 
179
 
 
180
extern Arguments* argNewFromArgv (char* const* const argv)
 
181
{
 
182
        Arguments* result = xMalloc (1, Arguments);
 
183
        memset (result, 0, sizeof (Arguments));
 
184
        result->type = ARG_ARGV;
 
185
        result->u.argvArgs.argv = argv;
 
186
        result->u.argvArgs.item = result->u.argvArgs.argv;
 
187
        result->item = *result->u.argvArgs.item;
 
188
        return result;
 
189
}
 
190
 
 
191
extern Arguments* argNewFromFile (FILE* const fp)
 
192
{
 
193
        Arguments* result = xMalloc (1, Arguments);
 
194
        memset (result, 0, sizeof (Arguments));
 
195
        result->type = ARG_FILE;
 
196
        result->u.fileArgs.fp = fp;
 
197
        result->item = nextFileString (result, result->u.fileArgs.fp);
 
198
        return result;
 
199
}
 
200
 
 
201
extern Arguments* argNewFromLineFile (FILE* const fp)
 
202
{
 
203
        Arguments* result = xMalloc (1, Arguments);
 
204
        memset (result, 0, sizeof (Arguments));
 
205
        result->type = ARG_FILE;
 
206
        result->lineMode = TRUE;
 
207
        result->u.fileArgs.fp = fp;
 
208
        result->item = nextFileString (result, result->u.fileArgs.fp);
 
209
        return result;
 
210
}
 
211
 
 
212
extern char *argItem (const Arguments* const current)
 
213
{
 
214
        Assert (current != NULL);
 
215
        Assert (! argOff (current));
 
216
        return current->item;
 
217
}
 
218
 
 
219
extern boolean argOff (const Arguments* const current)
 
220
{
 
221
        Assert (current != NULL);
 
222
        return (boolean) (current->item == NULL);
 
223
}
 
224
 
 
225
extern void argSetWordMode (Arguments* const current)
 
226
{
 
227
        Assert (current != NULL);
 
228
        current->lineMode = FALSE;
 
229
}
 
230
 
 
231
extern void argSetLineMode (Arguments* const current)
 
232
{
 
233
        Assert (current != NULL);
 
234
        current->lineMode = TRUE;
 
235
}
 
236
 
 
237
extern void argForth (Arguments* const current)
 
238
{
 
239
        Assert (current != NULL);
 
240
        Assert (! argOff (current));
 
241
        switch (current->type)
 
242
        {
 
243
                case ARG_STRING:
 
244
                        if (current->item != NULL)
 
245
                                eFree (current->item);
 
246
                        current->u.stringArgs.item = current->u.stringArgs.next;
 
247
                        current->item = nextString (current, &current->u.stringArgs.next);
 
248
                        break;
 
249
                case ARG_ARGV:
 
250
                        ++current->u.argvArgs.item;
 
251
                        current->item = *current->u.argvArgs.item;
 
252
                        break;
 
253
                case ARG_FILE:
 
254
                        if (current->item != NULL)
 
255
                                eFree (current->item);
 
256
                        current->item = nextFileString (current, current->u.fileArgs.fp);
 
257
                        break;
 
258
                default:
 
259
                        Assert ("Invalid argument type" == NULL);
 
260
                        break;
 
261
        }
 
262
}
 
263
 
 
264
extern void argDelete (Arguments* const current)
 
265
{
 
266
        Assert (current != NULL);
 
267
        if (current->type ==  ARG_STRING  &&  current->item != NULL)
 
268
                eFree (current->item);
 
269
        memset (current, 0, sizeof (Arguments));
 
270
        eFree (current);
 
271
}
 
272
 
 
273
/* vi:set tabstop=4 shiftwidth=4: */