These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers printf-stdarg.c Source File

printf-stdarg.c

00001 /*
00002     Copyright 2001, 2002 Georges Menie (www.menie.org)
00003     stdarg version contributed by Christian Ettinger
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU Lesser General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 /*
00021     putchar is the only external dependency for this file,
00022     if you have a working putchar, leave it commented out.
00023     If not, uncomment the define below and
00024     replace outbyte(c) by your own function call.
00025 
00026 */
00027 
00028 
00029 //#define putchar(c) c
00030 extern int sendchar (int ch);
00031 #define putchar(c)  sendchar(c)
00032 
00033 #include <stdarg.h>
00034 
00035 static void printchar(char **str, int c)
00036 {
00037     //extern int putchar(int c);
00038 
00039     if (str) {
00040         **str = (char)c;
00041         ++(*str);
00042     }
00043     else
00044     {
00045         (void)putchar(c);
00046     }
00047 }
00048 
00049 #define PAD_RIGHT 1
00050 #define PAD_ZERO 2
00051 
00052 static int prints(char **out, const char *string, int width, int pad)
00053 {
00054     register int pc = 0, padchar = ' ';
00055 
00056     if (width > 0) {
00057         register int len = 0;
00058         register const char *ptr;
00059         for (ptr = string; *ptr; ++ptr) ++len;
00060         if (len >= width) width = 0;
00061         else width -= len;
00062         if (pad & PAD_ZERO) padchar = '0';
00063     }
00064     if (!(pad & PAD_RIGHT)) {
00065         for ( ; width > 0; --width) {
00066             printchar (out, padchar);
00067             ++pc;
00068         }
00069     }
00070     for ( ; *string ; ++string) {
00071         printchar (out, *string);
00072         ++pc;
00073     }
00074     for ( ; width > 0; --width) {
00075         printchar (out, padchar);
00076         ++pc;
00077     }
00078 
00079     return pc;
00080 }
00081 
00082 /* the following should be enough for 32 bit int */
00083 #define PRINT_BUF_LEN 12
00084 
00085 static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
00086 {
00087     char print_buf[PRINT_BUF_LEN];
00088     register char *s;
00089     register int t, neg = 0, pc = 0;
00090     register unsigned int u = (unsigned int)i;
00091 
00092     if (i == 0) {
00093         print_buf[0] = '0';
00094         print_buf[1] = '\0';
00095         return prints (out, print_buf, width, pad);
00096     }
00097 
00098     if (sg && b == 10 && i < 0) {
00099         neg = 1;
00100         u = (unsigned int)-i;
00101     }
00102 
00103     s = print_buf + PRINT_BUF_LEN-1;
00104     *s = '\0';
00105 
00106     while (u) {
00107         t = (int)u % b;
00108         if( t >= 10 )
00109             t += letbase - '0' - 10;
00110         *--s = (char)(t + '0');
00111         u /= b;
00112     }
00113 
00114     if (neg) {
00115         if( width && (pad & PAD_ZERO) ) {
00116             printchar (out, '-');
00117             ++pc;
00118             --width;
00119         }
00120         else {
00121             *--s = '-';
00122         }
00123     }
00124 
00125     return pc + prints (out, s, width, pad);
00126 }
00127 
00128 static int print( char **out, const char *format, va_list args )
00129 {
00130     register int width, pad;
00131     register int pc = 0;
00132     char scr[2];
00133 
00134     for (; *format != 0; ++format) {
00135         if (*format == '%') {
00136             ++format;
00137             width = pad = 0;
00138             if (*format == '\0') break;
00139             if (*format == '%') goto out;
00140             if (*format == '-') {
00141                 ++format;
00142                 pad = PAD_RIGHT;
00143             }
00144             while (*format == '0') {
00145                 ++format;
00146                 pad |= PAD_ZERO;
00147             }
00148             for ( ; *format >= '0' && *format <= '9'; ++format) {
00149                 width *= 10;
00150                 width += *format - '0';
00151             }
00152             if( *format == 's' ) {
00153                 register char *s = (char *)va_arg( args, int );
00154                 pc += prints (out, s?s:"(null)", width, pad);
00155                 continue;
00156             }
00157             if( *format == 'd' ) {
00158                 pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');
00159                 continue;
00160             }
00161             if( *format == 'x' ) {
00162                 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');
00163                 continue;
00164             }
00165             if( *format == 'X' ) {
00166                 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A');
00167                 continue;
00168             }
00169             if( *format == 'u' ) {
00170                 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');
00171                 continue;
00172             }
00173             if( *format == 'c' ) {
00174                 /* char are converted to int then pushed on the stack */
00175                 scr[0] = (char)va_arg( args, int );
00176                 scr[1] = '\0';
00177                 pc += prints (out, scr, width, pad);
00178                 continue;
00179             }
00180         }
00181         else {
00182         out:
00183             printchar (out, *format);
00184             ++pc;
00185         }
00186     }
00187     if (out) **out = '\0';
00188     va_end( args );
00189     return pc;
00190 }
00191 
00192 int printf(const char *format, ...)
00193 {
00194         va_list args;
00195 
00196         va_start( args, format );
00197         return print( 0, format, args );
00198 }
00199 
00200 int sprintf(char *out, const char *format, ...)
00201 {
00202         va_list args;
00203 
00204         va_start( args, format );
00205         return print( &out, format, args );
00206 }
00207 
00208 
00209 int snprintf( char *buf, unsigned int count, const char *format, ... )
00210 {
00211         va_list args;
00212 
00213         ( void ) count;
00214 
00215         va_start( args, format );
00216         return print( &buf, format, args );
00217 }
00218 
00219 
00220 #ifdef TEST_PRINTF
00221 int main(void)
00222 {
00223     char *ptr = "Hello world!";
00224     char *np = 0;
00225     int i = 5;
00226     unsigned int bs = sizeof(int)*8;
00227     int mi;
00228     char buf[80];
00229 
00230     mi = (1 << (bs-1)) + 1;
00231     printf("%s\n", ptr);
00232     printf("printf test\n");
00233     printf("%s is null pointer\n", np);
00234     printf("%d = 5\n", i);
00235     printf("%d = - max int\n", mi);
00236     printf("char %c = 'a'\n", 'a');
00237     printf("hex %x = ff\n", 0xff);
00238     printf("hex %02x = 00\n", 0);
00239     printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);
00240     printf("%d %s(s)%", 0, "message");
00241     printf("\n");
00242     printf("%d %s(s) with %%\n", 0, "message");
00243     sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);
00244     sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);
00245     sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);
00246     sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);
00247     sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);
00248     sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);
00249     sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);
00250     sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);
00251 
00252     return 0;
00253 }
00254 
00255 /*
00256  * if you compile this file with
00257  *   gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c
00258  * you will get a normal warning:
00259  *   printf.c:214: warning: spurious trailing `%' in format
00260  * this line is testing an invalid % at the end of the format string.
00261  *
00262  * this should display (on 32bit int machine) :
00263  *
00264  * Hello world!
00265  * printf test
00266  * (null) is null pointer
00267  * 5 = 5
00268  * -2147483647 = - max int
00269  * char a = 'a'
00270  * hex ff = ff
00271  * hex 00 = 00
00272  * signed -3 = unsigned 4294967293 = hex fffffffd
00273  * 0 message(s)
00274  * 0 message(s) with %
00275  * justif: "left      "
00276  * justif: "     right"
00277  *  3: 0003 zero padded
00278  *  3: 3    left justif.
00279  *  3:    3 right justif.
00280  * -3: -003 zero padded
00281  * -3: -3   left justif.
00282  * -3:   -3 right justif.
00283  */
00284 
00285 #endif
00286 
00287 
00288 /* To keep linker happy. */
00289 int write( int i, char* c, int n)
00290 {
00291     (void)i;
00292     (void)n;
00293     (void)c;
00294     return 0;
00295 }
00296