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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

projs/trunk/shared_source/c_datd/fcmiof.c revision 56 by dashley, Sat Oct 29 01:53:01 2016 UTC projs/emts/trunk/src/lib_c/c_cmode/fcmiof.c revision 269 by dashley, Sat Jun 1 21:29:58 2019 UTC
# Line 1  Line 1 
1  //$Header$  //$Header$
2  //-------------------------------------------------------------------------------------------------  //{723c099f-de0c-46b4-93a9-ca89bc796de7}
3  //This file is part of "David T. Ashley's Shared Source Code", a set of shared components  //-------------------------------------------------------------------------------------------------
4  //integrated into many of David T. Ashley's projects.  //This file is part of "Embedded Tool Set", a tool set designed to facilitate embedded system
5  //-------------------------------------------------------------------------------------------------  //software and hardware development.
6  //This source code and any program in which it is compiled/used is provided under the MIT License,  //-------------------------------------------------------------------------------------------------
7  //reproduced below.  //This source code and any program in which it is compiled/used is provided under the MIT License,
8  //-------------------------------------------------------------------------------------------------  //reproduced below.
9  //Permission is hereby granted, free of charge, to any person obtaining a copy of  //-------------------------------------------------------------------------------------------------
10  //this software and associated documentation files(the "Software"), to deal in the  //Permission is hereby granted, free of charge, to any person obtaining a copy of
11  //Software without restriction, including without limitation the rights to use,  //this software and associated documentation files(the "Software"), to deal in the
12  //copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the  //Software without restriction, including without limitation the rights to use,
13  //Software, and to permit persons to whom the Software is furnished to do so,  //copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the
14  //subject to the following conditions :  //Software, and to permit persons to whom the Software is furnished to do so,
15  //  //subject to the following conditions :
16  //The above copyright notice and this permission notice shall be included in all  //
17  //copies or substantial portions of the Software.  //The above copyright notice and this permission notice shall be included in all
18  //  //copies or substantial portions of the Software.
19  //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  //
20  //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE  //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23  //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  //SOFTWARE.  //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  //-------------------------------------------------------------------------------------------------  //SOFTWARE.
27  #define MODULE_FCMIOF  //-------------------------------------------------------------------------------------------------
28    #define MODULE_FCMIOF
29  #include <assert.h>  
30  #include <stdio.h>  #include <assert.h>
31  #include <string.h>  #include <stdio.h>
32  #include <time.h>  #include <stdint.h>
33    #include <string.h>
34  #include "fcmiof.h"  #include <time.h>
35    
36    #include "fcmiof.h"
37  #define FCMIOF_HORIZONTAL_BAR_SEP_CHAR    ('-')  
38  #define FCMIOF_LINE_LEN                   (78)  #define FCMIOF_HORIZONTAL_BAR_SEP_CHAR    ('-')
39    
40    
41  //08/16/01:  Visual inspection OK.  int FCMIOF_stream_repchar(FILE *s, char c, size_t n)
42  int FCMIOF_get_line_len(void)  {
43     {     int rv = 0;
44     return(FCMIOF_LINE_LEN);  
45     }     assert(s != NULL);
46    
47       while ((n--) && (rv >= 0))
48  //08/16/01:  Visual inspection OK.     {
49  void FCMIOF_stream_repchar(FILE *s, char c, unsigned n)        rv = fprintf(s, "%c", c);
50     {     }
51     assert(s != NULL);  
52       return(rv);
53     while(n--)  }
54             fprintf(s, "%c", c);  
55     }  
56    int FCMIOF_repchar(char c, size_t n)
57    {
58  //08/16/01:  Visual inspection OK.     int rv = 0;
59  void FCMIOF_repchar(char c, unsigned n)  
60     {     while ((n--) && (rv >= 0))
61     while(n--)     {
62             printf("%c", c);        rv = printf("%c", c);
63     }     }
64    
65       return(rv);
66  //08/16/01:  Visual inspection OK.  }
67  void FCMIOF_hline(void)  
68     {  
69     FCMIOF_repchar(FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN);  int FCMIOF_stream_hline(FILE *s, size_t line_len)
70     printf("\n");  {
71     }     int rv;
72    
73       assert(s != NULL);
74  //08/16/01:  Visual inspection OK.  
75  void FCMIOF_stream_hline(FILE *s)     rv = FCMIOF_stream_repchar(s, FCMIOF_HORIZONTAL_BAR_SEP_CHAR, line_len);
76     {  
77     assert(s != NULL);     if (rv >= 0)
78          rv = fprintf(s, "\n");
79     FCMIOF_stream_repchar(s, FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN);  
80     fprintf(s, "\n");     return(rv);
81     }  }
82    
83    
84  //08/16/01:  Visual inspection OK.  int FCMIOF_hline(size_t line_len)
85  void FCMIOF_stream_bannerheading(FILE *f,  {
86                                   char *s,     int rv;
87                                   int n_extra_lines)  
88     {     rv = FCMIOF_repchar(FCMIOF_HORIZONTAL_BAR_SEP_CHAR, line_len);
89     const int lr_padding = 3;  
90        /* The number of spaces on each side of what is printed.     if (rv >= 0)           //If no error from previous output attempts.
91        */        rv = printf("\n");  //Do the final printf().
92     int i;  
93        /* General iteration variable.     return(rv);
94        */  }
95    
96     int n_asterisks;  
97     int input_arg_len;  int FCMIOF_stream_bannerheading(FILE *f,
98     int n_left_spaces;                                  char *s,
99     int n_right_spaces;                                  size_t line_len,
100                                    size_t n_extra_lines)
101     /* Check the file pointer, string pointer, and other par.  {
102     */     size_t lr_padding = 4;
103     assert(f != NULL);        //The number of spaces on each side of what is printed.
104     assert(s != NULL);     size_t i;
105     assert(n_extra_lines >= 0);        //General iteration variable.
106       size_t length_limit;
107     /* Print the right number of solid lines of asterisks to the        //The maximum length-related value that will be used for
108     ** standard output.        //calculation.  This is to prevent overflow and incorrect
109     */        //results in logical expressions.
110     for (i=0; i<n_extra_lines; i++)     size_t input_arg_len;
111        {        //Length of the banner heading provided by the caller.  May
112        FCMIOF_stream_repchar(f, '*', FCMIOF_LINE_LEN);        //be clipped to prevent logical errors.
113        fprintf(f, "\n");     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     /* Figure out how many asterisks to print on each side of the        //includes asterisks, and spaces.
117     ** argument, and how many spaces.  We also need to figure out     size_t n_left_asterisks, n_right_asterisks;
118     ** how many characters of the input argument to print--if there        //The number of asterisks that will be printed on the left and right, respectively.
119     ** are too many characters, we need to truncate.  
120     */     int rv = 0;
121     input_arg_len = strlen(s);  
122     if(input_arg_len > (FCMIOF_LINE_LEN - 2 * lr_padding - 2))     //Check the file pointer, string pointer, and other parameters.
123        input_arg_len = FCMIOF_LINE_LEN - 2 * lr_padding - 2;     assert(f != NULL);
124       assert(s != NULL);
125     n_asterisks = (FCMIOF_LINE_LEN - 2*lr_padding - input_arg_len)/2;  
126       //Figure out how many asterisks to print on each side of the
127     n_left_spaces = lr_padding;     //argument, and how many spaces.  We also need to figure out
128       //how many characters of the input argument to print--if there
129     if ((FCMIOF_LINE_LEN - 2*lr_padding - input_arg_len) % 2)     //are too many characters, we need to truncate.
130        {  
131        /* Odd, need to pad the right by one. */     //Figure out the length of the string passed by the caller.
132        n_right_spaces = lr_padding+1;     input_arg_len = strlen(s);
133        }  
134     else     //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        n_right_spaces = lr_padding;     //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     /* Print the text. */     if (line_len > length_limit)
140     FCMIOF_stream_repchar(f, '*', n_asterisks);        line_len = length_limit;
141     FCMIOF_stream_repchar(f, ' ', n_left_spaces);     if (lr_padding > length_limit)
142     for (i=0; i<input_arg_len; i++)        lr_padding = length_limit;
143        fprintf(f, "%c", s[i]);     if (input_arg_len > length_limit)
144     FCMIOF_stream_repchar(f, ' ', n_right_spaces);        input_arg_len = length_limit;
145     FCMIOF_stream_repchar(f, '*', n_asterisks);  
146     fprintf(f, "\n");     //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     /* Print the right number of solid lines of asterisks to the     //few characters on the right.
149     ** standard output.     if (line_len > input_arg_len)
150     */        total_padding_chars = line_len - input_arg_len;
151     for (i=0; i<n_extra_lines; i++)     else
152        {        total_padding_chars = 0;
153        FCMIOF_stream_repchar(f, '*', FCMIOF_LINE_LEN);  
154        fprintf(f, "\n");     if ((total_padding_chars % 2) == 0)
155        }     {
156     }        //Even.
157          total_padding_chars_left = total_padding_chars_right = total_padding_chars / 2;
158       }
159  //08/16/01:  Visual inspection OK.     else
160  void FCMIOF_bannerheading(char *s, int n_extra_lines)     {
161     {        //Odd.
162     assert(s != NULL);        total_padding_chars_left = total_padding_chars / 2;
163     assert(n_extra_lines >= 0);        total_padding_chars_right = total_padding_chars_left + 1;
164       }
165     FCMIOF_stream_bannerheading(stdout, s, n_extra_lines);  
166     }     //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  void FCMIOF_time_stream(FILE *s, time_t ltime)     else
170     {        n_left_asterisks = 0;
171     char *p;  
172       //Figure out the number of asterisks on the right.
173     assert(s != NULL);     if (total_padding_chars_right > lr_padding)
174          n_right_asterisks = total_padding_chars_right - lr_padding;
175     time(&ltime);     else
176          n_right_asterisks = 0;
177     p = ctime(&ltime);  
178       // Print the correct number of solid lines of asterisks to the
179     if (p)     // standard output.
180        {     for (i = 0; (i < n_extra_lines) && (rv >= 0); i++)
181        int i;     {
182          rv = FCMIOF_stream_repchar(f, '*', line_len);
183        for (i=11; i<19; i++)        if (rv >= 0)
184           fprintf(s, "%c", p[i]);           rv = fprintf(f, "\n");
185        fprintf(s, " ");     }
186        for (i=0; i<10; i++)  
187           fprintf(s, "%c", p[i]);     //return(0);
188        fprintf(s, " ");  
189        for (i=20; i<24; i++)     //If there has been an error in the printing, return.
190           fprintf(s, "%c", p[i]);     if (rv < 0)
191        }        return(rv);
192     else  
193        {     //Left asterisks of banner.
194        fprintf(s, "??? ??? ?? ??:??:?? ????");     rv = FCMIOF_stream_repchar(f, '*', n_left_asterisks);
195        }     if (rv < 0)
196     }        return(rv);
197    
198       //return(0);
199    
200  //08/16/01:  Visual inspection OK.     //Left spaces of banner.
201  const char *FCMIOF_cvcinfo(void)     rv = FCMIOF_stream_repchar(f, ' ', lr_padding);
202     {     if (rv < 0)
203     return("$Header$");        return(rv);
204     }  
205       //The banner text itself.
206       for (i = 0; (i < input_arg_len) && (rv >= 0); i++)
207  //08/16/01:  Visual inspection OK.     {
208  const char *FCMIOF_hvcinfo(void)        rv = fprintf(f, "%c", s[i]);
209     {     }
210     return(FCMIOF_H_VERSION);  
211     }     if (rv < 0)
212          return(rv);
213  //End of fcmiof.c.  
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       /* Print the right number of solid lines of asterisks to the
230       ** standard output.
231       */
232       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       }
238    
239       return(rv);
240    }
241    
242    int FCMIOF_bannerheading(char *s, size_t line_len, size_t n_extra_lines)
243       {
244       int rv;
245    
246       assert(s != NULL);
247    
248       rv = FCMIOF_stream_bannerheading(stdout, s, line_len, n_extra_lines);
249    
250       return(rv);
251       }
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.

Legend:
Removed from v.56  
changed lines
  Added in v.269

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25