1 module nogcio;
2 
3 import core.stdc.wchar_ : fwprintf;
4 import core.stdc.stdio; // : stdout, fprintf;
5 
6 //
7 //  MAKE SURE TO SET YOUR LOCALE!  IT WILL SCREW UP OTHERWISE!
8 //  setlocale(LC_CTYPE, "");    generally works.
9 //
10 
11 //Implements the basic printing functionality
12 void print(T...)(T input) @nogc
13 {
14     //The loop prints each argument, separating them with a space
15     bool first = true;
16     foreach (x; input)
17     {
18         if(!first)
19             printInternal(stdout, " ");
20         else
21             first = false;
22         printInternal(stdout, x);
23     }
24 }
25 
26 //Just like "print", but prints to the specified file
27 void fprint(T...)(FILE* file, T input)
28 {
29     //The loop prints each argument, separating them with a space
30     bool first = true;
31     foreach (x; input)
32     {
33         if(!first)
34             printInternal(file, " ");
35         else
36             first = false;
37         printInternal(file, x);
38     }
39 }
40 
41 //Just like "print", but appends a newline
42 void println(T...)(T input) @nogc
43 {
44     pragma(inline, true) print(input);
45     printInternal(stdout, "\n");
46 }
47 
48 //Just like "println", but prints to the specified file
49 void fprintln(T...)(FILE* file, T input) @nogc
50 {
51     pragma(inline, true) fprint(file, input);
52     printInternal(file, "\n");
53 }
54 
55 //Wrapper for printing to a file
56 pragma(inline, true)
57 private void printInternal(FILE* file, string s) @nogc
58 {
59     fprintf(file, "%s", s.ptr);
60 }
61 
62 //Char support
63 pragma(inline, true)
64 private void printInternal(FILE* file, char c) @nogc
65 {
66     fprintf(file, "%c", c);
67 }
68 
69 //Wchar support
70 pragma(inline, true)
71 private void printInternal(FILE* file, wchar c) @nogc
72 {
73     printInternal(file, cast(char) c);
74 }
75 
76 //Dchar support
77 pragma(inline, true)
78 private void printInternal(FILE* file, dchar c) @nogc
79 {
80     printInternal(file, cast(char) c);
81 }
82 
83 //Bool support
84 pragma(inline, true)
85 private void printInternal(FILE* file, bool b) @nogc
86 {
87     if(b)
88         fprintf(file, "true");
89     else
90         fprintf(file, "false");
91 }
92 
93 //Int support
94 pragma(inline, true)
95 private void printInternal(FILE* file, int i) @nogc
96 {
97     fprintf(file, "%d", i);
98 }
99 
100 //Float/Double support
101 pragma(inline, true)
102 private void printInternal(FILE* file, double d) @nogc
103 {
104     fprintf(file, "%f", d);
105 }
106 
107 //Pointer support
108 pragma(inline, true)
109 private void printInternal(FILE* file, void* p) @nogc
110 {
111     fprintf(file, "%p", p);
112 }
113 
114 
115 
116 
117 
118 //It's "print" with dstring support
119 void wprint(T...)(T input) @nogc
120 {
121     //The loop prints each argument, separating them with a space
122     bool first = true;
123     foreach (x; input)
124     {
125         if(!first)
126             wprintInternal(stdout, " ");
127         else
128             first = false;
129         wprintInternal(stdout, x);
130     }
131 }
132 
133 //Just like "wprint", but prints to the specified file
134 void fwprint(T...)(FILE* file, T input)
135 {
136     //The loop prints each argument, separating them with a space
137     bool first = true;
138     foreach (x; input)
139     {
140         if(!first)
141             wprintInternal(file, " ");
142         else
143             first = false;
144         wprintInternal(file, x);
145     }
146 }
147 
148 //Just like "wprint", but appends a newline
149 void wprintln(T...)(T input) @nogc
150 {
151     pragma(inline, true) wprint(input);
152     wprintInternal(stdout, "\n");
153 }
154 
155 //Just like "wprintln", but prints to the specified file
156 void fwprintln(T...)(FILE* file, T input) @nogc
157 {
158     pragma(inline, true) fwprint(file, input);
159     wprintInternal(file, "\n");
160 }
161 
162 //Wrapper for printing to a file
163 pragma(inline, true)
164 private void wprintInternal(FILE* file, dstring s) @nogc
165 {
166     fwprintf(file, "%ls", s.ptr);
167 }
168 
169 //Dchar support
170 pragma(inline, true)
171 private void wprintInternal(FILE* file, dchar c) @nogc
172 {
173     fwprintf(file, "%c", c);
174 }
175 
176 //Char support
177 pragma(inline, true)
178 private void wprintInternal(FILE* file, char c) @nogc
179 {
180     wprintInternal(file, cast(dchar) c);
181 }
182 
183 //Wchar support
184 pragma(inline, true)
185 private void wprintInternal(FILE* file, wchar c) @nogc
186 {
187     wprintInternal(file, cast(dchar) c);
188 }
189 
190 //Bool support
191 pragma(inline, true)
192 private void wprintInternal(FILE* file, bool b) @nogc
193 {
194     if(b)
195         fwprintf(file, "true");
196     else
197         fwprintf(file, "false");
198 }
199 
200 //Int support
201 pragma(inline, true)
202 private void wprintInternal(FILE* file, int i) @nogc
203 {
204     fwprintf(file, "%d", i);
205 }
206 
207 //Float/Double support
208 pragma(inline, true)
209 private void wprintInternal(FILE* file, double d) @nogc
210 {
211     fwprintf(file, "%f", d);
212 }
213 
214 //Pointer support
215 pragma(inline, true)
216 private void wprintInternal(FILE* file, void* p) @nogc
217 {
218     fwprintf(file, "%p", p);
219 }