]> git.draconx.ca Git - gob-dx.git/blob - src/out.c
ee1d3289d3853cda215208b390f3900e0d6acd9b
[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 int outhline = 1;
18 int outphline = 1;
19
20 static int
21 strchrcnt(char *s, char c)
22 {
23         int cnt = 0;
24         for(;*s;s++)
25                 if(*s == c)
26                         cnt++;
27         return cnt;
28 }
29
30 void
31 out_printf(FILE *fp,char *format,...)
32 {
33         va_list ap;
34         char *s;
35
36         va_start(ap,format);
37         s = g_strdup_vprintf(format,ap);
38         va_end(ap);
39
40         if(fp == out)
41                 outline += strchrcnt(s,'\n');
42         else if(fp == outh)
43                 outhline += strchrcnt(s,'\n');
44         else if(fp == outph)
45                 outphline += strchrcnt(s,'\n');
46         else
47                 g_assert_not_reached();
48
49         fprintf(fp,"%s",s);
50         g_free(s);
51 }
52
53 void
54 out_addline_infile(FILE *fp, int line)
55 {
56         if(fp == out)
57                 outline++;
58         else if(fp == outh)
59                 outhline++;
60         else if(fp == outph)
61                 outphline++;
62         else
63                 g_assert_not_reached();
64
65         fprintf(fp,"#line %d \"%s\"\n",line,filename);
66 }
67
68 void
69 out_addline_outfile(FILE *fp)
70 {
71         if(fp == out) {
72                 outline++;
73                 if(!for_cpp)
74                         fprintf(fp,"#line %d \"%s.c\"\n",outline,filebase);
75                 else
76                         fprintf(fp,"#line %d \"%s.cc\"\n",outline,filebase);
77         } else if(fp == outh) {
78                 outhline++;
79                 fprintf(fp,"#line %d \"%s.h\"\n",outhline,filebase);
80         } else if(fp == outph) {
81                 outphline++;
82                 fprintf(fp,"#line %d \"%s-private.h\"\n",outphline,filebase);
83         } else
84                 g_assert_not_reached();
85
86 }