]> git.draconx.ca Git - gob-dx.git/blob - src/out.c
Replace gnulib patch with new common helper macro.
[gob-dx.git] / src / out.c
1 /*
2  * GOB C Preprocessor
3  * Copyright (C) 1999,2000 the Free Software Foundation.
4  * Copyright (C) 2000 Eazel, Inc.
5  * Copyright (C) 2001-2011 George (Jiri) Lebl
6  *
7  * Author: George (Jiri) Lebl
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the  Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22  * USA.
23  */
24
25 #include <config.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <glib.h>
29
30 #include "main.h"
31
32 #include "out.h"
33
34 int outline = 1;
35 static gboolean in_out = TRUE;
36 int outhline = 1;
37 static gboolean in_outh = TRUE;
38 int outphline = 1;
39 static gboolean in_outph = TRUE;
40
41 extern char file_sep;
42
43 static int
44 strchrcnt(char *s, char c)
45 {
46         int cnt = 0;
47         for(;*s;s++)
48                 if(*s == c)
49                         cnt++;
50         return cnt;
51 }
52
53 void
54 out_printf(FILE *fp, const char *format,...)
55 {
56         va_list ap;
57         char *s;
58
59         if (no_write)
60                 return;
61
62         va_start(ap, format);
63         s = g_strdup_vprintf(format, ap);
64         va_end(ap);
65
66         if(fp == out)
67                 outline += strchrcnt(s, '\n');
68         else if(fp == outh)
69                 outhline += strchrcnt(s, '\n');
70         else if(fp == outph)
71                 outphline += strchrcnt(s, '\n');
72         else
73                 g_assert_not_reached();
74
75         fprintf(fp, "%s", s);
76         g_free(s);
77 }
78
79 void
80 out_addline_infile(FILE *fp, int line)
81 {
82         if(no_lines || no_write)
83                 return;
84
85         if(fp == out) {
86                 outline++;
87                 in_out = FALSE;
88         } else if(fp == outh) {
89                 outhline++;
90                 in_outh = FALSE;
91         } else if(fp == outph) {
92                 outphline++;
93                 in_outph = FALSE;
94         } else
95                 g_assert_not_reached();
96
97         fprintf(fp, "#line %d \"%s\"\n", line, filename);
98 }
99
100 void
101 out_addline_outfile(FILE *fp)
102 {
103         if(no_lines || no_write)
104                 return;
105
106         if(fp == out) {
107                 if(in_out) return;
108                 outline++;
109                 if(!for_cpp)
110                         fprintf(fp,"#line %d \"%s.c\"\n",outline,filebase);
111                 else
112                         fprintf(fp,"#line %d \"%s.cc\"\n",outline,filebase);
113                 in_out = TRUE;
114         } else if(fp == outh) {
115                 if(in_outh) return;
116                 outhline++;
117                 fprintf(fp,"#line %d \"%s.h\"\n",outhline,filebase);
118                 in_outh = TRUE;
119         } else if(fp == outph) {
120                 char sep[2] = {0,0};
121                 if(in_outph) return;
122                 outphline++;
123                 if (file_sep != 0)
124                         sep[0] = file_sep;
125                 fprintf(fp,"#line %d \"%s%sprivate.h\"\n",outphline,filebase,sep);
126                 in_outph = TRUE;
127         } else
128                 g_assert_not_reached();
129 }