]> git.draconx.ca Git - gob-dx.git/blob - src/out.c
3fdf7cf35df9b1847c192cda319d01a910ccaf8e
[gob-dx.git] / src / out.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <glib.h>
4
5 #include "out.h"
6
7 extern FILE *out;
8 extern FILE *outh;
9 extern FILE *outph;
10
11 extern gboolean for_cpp;
12
13 extern char *filename;
14 extern char *filebase;
15
16 int outline = 1;
17 static gboolean in_out = TRUE;
18 int outhline = 1;
19 static gboolean in_outh = TRUE;
20 int outphline = 1;
21 static gboolean in_outph = TRUE;
22
23 static int
24 strchrcnt(char *s, char c)
25 {
26         int cnt = 0;
27         for(;*s;s++)
28                 if(*s == c)
29                         cnt++;
30         return cnt;
31 }
32
33 void
34 out_printf(FILE *fp,char *format,...)
35 {
36         va_list ap;
37         char *s;
38
39         va_start(ap,format);
40         s = g_strdup_vprintf(format,ap);
41         va_end(ap);
42
43         if(fp == out)
44                 outline += strchrcnt(s,'\n');
45         else if(fp == outh)
46                 outhline += strchrcnt(s,'\n');
47         else if(fp == outph)
48                 outphline += strchrcnt(s,'\n');
49         else
50                 g_assert_not_reached();
51
52         fprintf(fp,"%s",s);
53         g_free(s);
54 }
55
56 void
57 out_addline_infile(FILE *fp, int line)
58 {
59         if(fp == out) {
60                 outline++;
61                 in_out = FALSE;
62         } else if(fp == outh) {
63                 outhline++;
64                 in_outh = FALSE;
65         } else if(fp == outph) {
66                 outphline++;
67                 in_outph = FALSE;
68         } else
69                 g_assert_not_reached();
70
71         fprintf(fp,"#line %d \"%s\"\n",line,filename);
72 }
73
74 void
75 out_addline_outfile(FILE *fp)
76 {
77         if(fp == out) {
78                 if(in_out) return;
79                 outline++;
80                 if(!for_cpp)
81                         fprintf(fp,"#line %d \"%s.c\"\n",outline,filebase);
82                 else
83                         fprintf(fp,"#line %d \"%s.cc\"\n",outline,filebase);
84                 in_out = TRUE;
85         } else if(fp == outh) {
86                 if(in_outh) return;
87                 outhline++;
88                 fprintf(fp,"#line %d \"%s.h\"\n",outhline,filebase);
89                 in_outh = TRUE;
90         } else if(fp == outph) {
91                 if(in_outph) return;
92                 outphline++;
93                 fprintf(fp,"#line %d \"%s-private.h\"\n",outphline,filebase);
94                 in_outph = TRUE;
95         } else
96                 g_assert_not_reached();
97 }