~ubuntu-branches/ubuntu/trusty/teeworlds/trusty-updates

« back to all changes in this revision

Viewing changes to bam/src/dep_cpp_2.c

  • Committer: Bazaar Package Importer
  • Author(s): Gonéri Le Bouder
  • Date: 2009-04-12 02:32:37 UTC
  • mfrom: (3.2.1 sid)
  • Revision ID: james.westby@ubuntu.com-20090412023237-ufmf1xn0rkjmx6f3
Tags: 0.5.1-2
* Fix the ouput of teeworlds-server --help with /bin/sh ->
  /bin/bash (Closes: #511600)
* Standard version 3.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <stdlib.h>
3
 
 
4
 
static int processline(char *line, char **start, char **end, int *systemheader)
5
 
{
6
 
        const char *include_text = "include";
7
 
        char *current = line;
8
 
        *start = 0;
9
 
        *end = 0;
10
 
        *systemheader = 0;
11
 
        
12
 
        /* search for # */
13
 
        while(*current != '#')
14
 
        {
15
 
                if(*current == ' ' || *current == '\t')
16
 
                        current++; /* next char */
17
 
                else
18
 
                        return 0; /* this catches \0 aswell */
19
 
        }
20
 
        
21
 
        current++; /* skip # */
22
 
        
23
 
        /* search for first character */
24
 
        while(1)
25
 
        {
26
 
                if(*current == ' ' || *current == '\t')
27
 
                        current++;
28
 
                else if(*current == 0)
29
 
                        return 0;
30
 
                else
31
 
                        break;
32
 
        }
33
 
        
34
 
        /* match "include" */
35
 
        while(*include_text)
36
 
        {
37
 
                if(*current == *include_text)
38
 
                {
39
 
                        current++;
40
 
                        include_text++;
41
 
                }
42
 
                else
43
 
                        return 0;
44
 
        }
45
 
        
46
 
        /* search for first character */
47
 
        while(1)
48
 
        {
49
 
                if(*current == ' ' || *current == '\t')
50
 
                        current++;
51
 
                else if(*current == 0)
52
 
                        return 0;
53
 
                else
54
 
                        break;
55
 
        }
56
 
 
57
 
        /* match starting < or " */
58
 
        *start = current+1;
59
 
        if(*current == '<')
60
 
                *systemheader = 1;
61
 
        else if(*current == '"')
62
 
                *systemheader = 0;
63
 
        else
64
 
                return 0;
65
 
        
66
 
        /* skip < or " */
67
 
        current++;
68
 
        
69
 
        /* search for < or " to end it */
70
 
        while(1)
71
 
        {
72
 
                if(*current == '>' || *current == '"')
73
 
                        break;
74
 
                else if(*current == 0)
75
 
                        return 0;
76
 
                else
77
 
                        current++;
78
 
        }
79
 
        
80
 
        *end = current; 
81
 
 
82
 
        return 1;
83
 
}
84
 
 
85
 
/* dependency calculator for c/c++ preprocessor */
86
 
int dependency_cpp_run(const char *filename, 
87
 
                int (*callback)(void *, const char *, int), void *userdata)
88
 
{
89
 
        const int debug = 0;
90
 
        char *linestart;
91
 
        char *includestart;
92
 
        char *includeend;
93
 
        int systemheader;
94
 
        int errorcode = 0;
95
 
        int linecount = 0;
96
 
 
97
 
        /* open file */
98
 
        long filesize;
99
 
        long readitems;
100
 
        char *filebuf;
101
 
        char *filebufcur;
102
 
        char *filebufend;
103
 
        FILE *file;
104
 
        
105
 
        if(debug)
106
 
                printf("cpp-dep: running on %s\n", filename);
107
 
 
108
 
        file = fopen(filename, "rb");
109
 
        if(!file)
110
 
        {
111
 
                /* printf("cpp-dep: error opening %s\n", filename); */
112
 
                return 0;
113
 
        }
114
 
        
115
 
        /* read the whole file */               
116
 
        fseek(file, 0, SEEK_END);
117
 
        filesize = ftell(file);
118
 
        fseek(file, 0, SEEK_SET);
119
 
        
120
 
        filebuf = malloc(filesize+1); /* +1 for null termination */
121
 
        
122
 
        if(!filebuf)
123
 
        {
124
 
                printf("cpp-dep: error allocating %ld bytes\n", filesize);
125
 
                fclose(file);
126
 
                return 1;
127
 
        }
128
 
                
129
 
        /* read the file and close it */
130
 
        readitems = fread(filebuf, 1, filesize, file);
131
 
        fclose(file);
132
 
 
133
 
        if(readitems != filesize)
134
 
        {
135
 
                printf("cpp-dep: error reading the complete file. %ld of %ld bytes read\n", readitems, filesize);
136
 
                free(filebuf);
137
 
                return 1;
138
 
        }
139
 
        
140
 
        filebufcur = filebuf;
141
 
        filebufend = filebuf+filesize;
142
 
        
143
 
        while(filebufcur < filebufend)
144
 
        {
145
 
                /* search for next line */
146
 
                linestart = filebufcur;
147
 
                while(filebufcur != filebufend && *filebufcur != '\n' && *filebufcur != '\r')
148
 
                        filebufcur++;
149
 
                *filebufcur = 0;
150
 
                filebufcur++;
151
 
                linecount++;
152
 
 
153
 
                if(processline(linestart, &includestart, &includeend, &systemheader))
154
 
                {
155
 
                        *includeend = 0;
156
 
                        if(debug) printf("INCLUDE: %s\n", includestart);
157
 
                        
158
 
                        /* run callback */
159
 
                        errorcode = callback(userdata, includestart, systemheader);
160
 
                        if(errorcode)
161
 
                        {
162
 
                                printf("cpp-dep: error %d during callback\n", errorcode);
163
 
                                break;
164
 
                        }
165
 
                }
166
 
        }
167
 
 
168
 
        if(debug)
169
 
                printf("cpp-dep: %s=%d lines\n", filename, linecount);
170
 
        
171
 
        /* clean up */
172
 
        free(filebuf);
173
 
        return errorcode;
174
 
}