/[dtapublic]/projs/emts/trunk/src/lib_c/c_cmode/fcmiof.c
ViewVC logotype

Annotation of /projs/emts/trunk/src/lib_c/c_cmode/fcmiof.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 244 - (hide annotations) (download)
Sun Aug 5 19:05:46 2018 UTC (6 years, 2 months ago) by dashley
Original Path: projs/ets/trunk/src/lib_c/c_cmode/fcmiof.c
File MIME type: text/plain
File size: 8148 byte(s)
Reorganize.  Get formatted string functions working.
1 dashley 71 //$Header$
2 dashley 244 //{723c099f-de0c-46b4-93a9-ca89bc796de7}
3 dashley 71 //-------------------------------------------------------------------------------------------------
4 dashley 244 //This file is part of "Embedded Tool Set", a tool set designed to facilitate embedded system
5     //software and hardware development.
6 dashley 71 //-------------------------------------------------------------------------------------------------
7     //This source code and any program in which it is compiled/used is provided under the MIT License,
8     //reproduced below.
9     //-------------------------------------------------------------------------------------------------
10     //Permission is hereby granted, free of charge, to any person obtaining a copy of
11     //this software and associated documentation files(the "Software"), to deal in the
12     //Software without restriction, including without limitation the rights to use,
13     //copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the
14     //Software, and to permit persons to whom the Software is furnished to do so,
15     //subject to the following conditions :
16     //
17     //The above copyright notice and this permission notice shall be included in all
18     //copies or substantial portions of the Software.
19     //
20     //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21     //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22     //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23     //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24     //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25     //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26     //SOFTWARE.
27     //-------------------------------------------------------------------------------------------------
28     #define MODULE_FCMIOF
29    
30     #include <assert.h>
31     #include <stdio.h>
32 dashley 244 #include <stdint.h>
33 dashley 71 #include <string.h>
34     #include <time.h>
35    
36     #include "fcmiof.h"
37    
38     #define FCMIOF_HORIZONTAL_BAR_SEP_CHAR ('-')
39    
40    
41 dashley 244 int FCMIOF_stream_repchar(FILE *s, char c, size_t n)
42     {
43     int rv = 0;
44 dashley 71
45 dashley 244 assert(s != NULL);
46 dashley 71
47 dashley 244 while ((n--) && (rv >= 0))
48 dashley 71 {
49 dashley 244 rv = fprintf(s, "%c", c);
50 dashley 71 }
51    
52 dashley 244 return(rv);
53     }
54 dashley 71
55    
56 dashley 244 int FCMIOF_repchar(char c, size_t n)
57     {
58     int rv = 0;
59 dashley 71
60 dashley 244 while ((n--) && (rv >= 0))
61 dashley 71 {
62 dashley 244 rv = printf("%c", c);
63 dashley 71 }
64    
65 dashley 244 return(rv);
66     }
67 dashley 71
68 dashley 244
69     int FCMIOF_stream_hline(FILE *s, size_t line_len)
70     {
71     int rv;
72    
73 dashley 71 assert(s != NULL);
74    
75 dashley 244 rv = FCMIOF_stream_repchar(s, FCMIOF_HORIZONTAL_BAR_SEP_CHAR, line_len);
76 dashley 71
77 dashley 244 if (rv >= 0)
78     rv = fprintf(s, "\n");
79 dashley 71
80 dashley 244 return(rv);
81     }
82 dashley 71
83    
84 dashley 244 int FCMIOF_hline(size_t line_len)
85     {
86     int rv;
87    
88     rv = FCMIOF_repchar(FCMIOF_HORIZONTAL_BAR_SEP_CHAR, line_len);
89    
90     if (rv >= 0) //If no error from previous output attempts.
91     rv = printf("\n"); //Do the final printf().
92    
93     return(rv);
94     }
95    
96    
97     int FCMIOF_stream_bannerheading(FILE *f,
98     char *s,
99     size_t line_len,
100     size_t n_extra_lines)
101     {
102     size_t lr_padding = 4;
103     //The number of spaces on each side of what is printed.
104     size_t i;
105     //General iteration variable.
106     size_t length_limit;
107     //The maximum length-related value that will be used for
108     //calculation. This is to prevent overflow and incorrect
109     //results in logical expressions.
110     size_t input_arg_len;
111     //Length of the banner heading provided by the caller. May
112     //be clipped to prevent logical errors.
113     size_t total_padding_chars, total_padding_chars_left, total_padding_chars_right;
114     //The total number of characters available for padding, and the
115     //number that will be used on the left, and on the right. This
116     //includes asterisks, and spaces.
117     size_t n_left_asterisks, n_right_asterisks;
118     //The number of asterisks that will be printed on the left and right, respectively.
119    
120     int rv = 0;
121    
122     //Check the file pointer, string pointer, and other parameters.
123 dashley 71 assert(f != NULL);
124     assert(s != NULL);
125    
126 dashley 244 //Figure out how many asterisks to print on each side of the
127     //argument, and how many spaces. We also need to figure out
128     //how many characters of the input argument to print--if there
129     //are too many characters, we need to truncate.
130 dashley 71
131 dashley 244 //Figure out the length of the string passed by the caller.
132 dashley 71 input_arg_len = strlen(s);
133    
134 dashley 244 //We must trim the parameters involving length to a sane value, otherwise
135     //the arithemtic expressions below will break down. There are other ways
136     //to handle this, with more branches and less clarity. The C standard
137     //specifies that SIZE_MAX must be at least 65535, so SIZE_MAX/32 >= 2047.
138     length_limit = SIZE_MAX / 32;
139     if (line_len > length_limit)
140     line_len = length_limit;
141     if (lr_padding > length_limit)
142     lr_padding = length_limit;
143     if (input_arg_len > length_limit)
144     input_arg_len = length_limit;
145 dashley 71
146 dashley 244 //Figure out the number of characters available for padding on the left
147     //and right. If the total number of characters available is odd, we want
148     //few characters on the right.
149     if (line_len > input_arg_len)
150     total_padding_chars = line_len - input_arg_len;
151     else
152     total_padding_chars = 0;
153 dashley 71
154 dashley 244 if ((total_padding_chars % 2) == 0)
155     {
156     //Even.
157     total_padding_chars_left = total_padding_chars_right = total_padding_chars / 2;
158     }
159 dashley 71 else
160 dashley 244 {
161     //Odd.
162     total_padding_chars_left = total_padding_chars / 2;
163     total_padding_chars_right = total_padding_chars_left + 1;
164     }
165 dashley 71
166 dashley 244 //Figure out the number of asterisks on the left.
167     if (total_padding_chars_left > lr_padding)
168     n_left_asterisks = total_padding_chars_left - lr_padding;
169     else
170     n_left_asterisks = 0;
171 dashley 71
172 dashley 244 //Figure out the number of asterisks on the right.
173     if (total_padding_chars_right > lr_padding)
174     n_right_asterisks = total_padding_chars_right - lr_padding;
175     else
176     n_right_asterisks = 0;
177    
178     // Print the correct number of solid lines of asterisks to the
179     // standard output.
180     for (i = 0; (i < n_extra_lines) && (rv >= 0); i++)
181     {
182     rv = FCMIOF_stream_repchar(f, '*', line_len);
183     if (rv >= 0)
184     rv = fprintf(f, "\n");
185     }
186    
187     //return(0);
188    
189     //If there has been an error in the printing, return.
190     if (rv < 0)
191     return(rv);
192    
193     //Left asterisks of banner.
194     rv = FCMIOF_stream_repchar(f, '*', n_left_asterisks);
195     if (rv < 0)
196     return(rv);
197    
198     //return(0);
199    
200     //Left spaces of banner.
201     rv = FCMIOF_stream_repchar(f, ' ', lr_padding);
202     if (rv < 0)
203     return(rv);
204    
205     //The banner text itself.
206     for (i = 0; (i < input_arg_len) && (rv >= 0); i++)
207     {
208     rv = fprintf(f, "%c", s[i]);
209     }
210    
211     if (rv < 0)
212     return(rv);
213    
214     //Right spaces of banner.
215     rv = FCMIOF_stream_repchar(f, ' ', lr_padding);
216     if (rv < 0)
217     return(rv);
218    
219     //Right asterisks of banner.
220     rv = FCMIOF_stream_repchar(f, '*', n_right_asterisks);
221     if (rv < 0)
222     return(rv);
223    
224     //Terminating newline.
225     rv = fprintf(f, "\n");
226     if (rv < 0)
227     return(rv);
228    
229 dashley 71 /* Print the right number of solid lines of asterisks to the
230     ** standard output.
231     */
232 dashley 244 for (i = 0; (i < n_extra_lines) && (rv >= 0); i++)
233     {
234     rv = FCMIOF_stream_repchar(f, '*', line_len);
235     if (rv >= 0)
236     rv = fprintf(f, "\n");
237 dashley 71 }
238    
239 dashley 244 return(rv);
240     }
241 dashley 71
242 dashley 244 int FCMIOF_bannerheading(char *s, size_t line_len, size_t n_extra_lines)
243 dashley 71 {
244 dashley 244 int rv;
245    
246 dashley 71 assert(s != NULL);
247    
248 dashley 244 rv = FCMIOF_stream_bannerheading(stdout, s, line_len, n_extra_lines);
249    
250     return(rv);
251 dashley 71 }
252    
253     void FCMIOF_time_stream(FILE *s, time_t ltime)
254     {
255     char *p;
256    
257     assert(s != NULL);
258    
259     time(&ltime);
260    
261     p = ctime(&ltime);
262    
263     if (p)
264     {
265     int i;
266    
267     for (i=11; i<19; i++)
268     fprintf(s, "%c", p[i]);
269     fprintf(s, " ");
270     for (i=0; i<10; i++)
271     fprintf(s, "%c", p[i]);
272     fprintf(s, " ");
273     for (i=20; i<24; i++)
274     fprintf(s, "%c", p[i]);
275     }
276     else
277     {
278     fprintf(s, "??? ??? ?? ??:??:?? ????");
279     }
280     }
281    
282    
283    
284     //08/16/01: Visual inspection OK.
285     const char *FCMIOF_cvcinfo(void)
286     {
287     return("$Header$");
288     }
289    
290    
291     //08/16/01: Visual inspection OK.
292     const char *FCMIOF_hvcinfo(void)
293     {
294     return(FCMIOF_H_VERSION);
295     }
296    
297     //End of fcmiof.c.

Properties

Name Value
svn:eol-style native
svn:keywords Header

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25