File: printf.c

package info (click to toggle)
flint 2.5.2-19
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 30,308 kB
  • sloc: ansic: 289,367; cpp: 11,210; python: 1,280; sh: 649; makefile: 283
file content (198 lines) | stat: -rw-r--r-- 5,471 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*=============================================================================

    This file is part of FLINT.

    FLINT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    FLINT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FLINT; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

=============================================================================*/
/******************************************************************************

    Copyright (C) 2013 William Hart
    Copyright (C) 2014 Ashish Kedia

******************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include "flint.h"

/* return number of arguments called for by a specific format specifier */
int parse_fmt(int * floating, const char * fmt)
{
   int args = 1;

   fmt++; /* skip % */

   if (fmt[0] == '%')
      return 0; /* special case, print % */

   if (fmt[0] == ' ' || fmt[0] == '+' || fmt[0] == '-')
      fmt++; /* skip flag */

   if (fmt[0] == '*')
   {
      args++;
      fmt++; /* skip * */
   } else
      while (isdigit((unsigned char) fmt[0]))
         fmt++; /* skip width */

   if (fmt[0] == '.')
   {
      fmt++; /* skip . */
      if (fmt[0] == '*')
      {
         args++;
         fmt++; /* skip * */
      } else
         while (isdigit((unsigned char) fmt[0]))
            fmt++; /* skip precision */
   }

   if (fmt[0] == 'h' || fmt[0] == 'l' || fmt[0] == 'L')
      fmt++; /* skip length */

   if (fmt[0] == 'e' || fmt[0] == 'E' || fmt[0] == 'f' || fmt[0] == 'g' || fmt[0] == 'G')
      (*floating) = 1;
   else
      (*floating) = 0;

   return args;
}

int flint_printf(const char * str, ...)
{
   va_list ap;
   size_t len = strlen(str);
   char * str2 = flint_malloc(len + 1);
   int w1 = 0, w2 = 0;
   void * w3;
   double d;
   ulong wu;
   slong w;
   int args, floating, width = 0, have_width, digits;
   size_t ret;

   /* deal with first substring */
   size_t n = strcspn(str, "%");
   strncpy(str2, str, n);
   str2[n] = '\0';
   ret = printf("%s", str2);
   len -= n;
   str += n;

   va_start(ap, str);

   while (len) /* deal with fmt spec prefixed strings */
   {
      have_width = 0;
      if (isdigit((unsigned char) str[1]))
      {
         width = atoi(str + 1);
         have_width = 1;
         digits = strspn(str + 1, "0123456789");
         if (str[digits + 1] == 'w')
         {
            str += digits;
            len -= digits;
         }
      }

      n = strcspn(str + 2, "%") + 2; /* be sure to skip a %% */
      strncpy(str2, str, n);
      str2[n] = '\0';

      switch (str[1])
      {
      case 'w':
         if (str[2] == 'x')
         {
            wu = (ulong) va_arg(ap, ulong);
            if (have_width)
                ret += printf(WORD_WIDTH_FMT "x", width, wu);
            else
                ret += printf(WORD_FMT "x", wu);
            ret += printf("%s", str2 + 3);
         } else if (str[2] == 'u')
         {
            wu = (ulong) va_arg(ap, ulong);
            if (have_width)
                ret += printf(WORD_WIDTH_FMT "u", width, wu);
            else
                ret += printf(WORD_FMT "u", wu);
            ret += printf("%s", str2 + 3);
         } else if (str[2] == 'd')
         {
            w = (slong) va_arg(ap, slong);
            if (have_width)
                ret += printf(WORD_WIDTH_FMT "d", width, w);
            else
                ret += printf(WORD_FMT "d", w);
            ret += printf("%s", str2 + 3);
         } else
         {
            w = (slong) va_arg(ap, slong);
            if (have_width)
                ret += printf(WORD_WIDTH_FMT "d", width, w);
            else
                ret += printf(WORD_FMT "d", w);
            ret += printf("%s", str2 + 2);
         }
         break;
      case '%': /*Special Case to handle %%*/
        ret += printf("%s",str2+1);
        break;
      default: /* pass to printf */
         args = parse_fmt(&floating, str2);
         if (args)
         {
            if (args == 3)
               w1 = va_arg(ap, int);
            if (args >= 2)
               w2 = va_arg(ap, int);
            if (floating)
            {
               d = va_arg(ap, double);
               if (args == 2)
                  ret += printf(str2, w2, d);
               else if (args == 3)
                  ret += printf(str2, w1, w2, d);
               else
                  ret += printf(str2, d);
            } else
            {
               w3 = va_arg(ap, void *);
               if (args == 2)
                  ret += printf(str2, w2, w3);
               else if (args == 3)
                  ret += printf(str2, w1, w2, w3);
               else
                  ret += printf(str2, w3);
            }
         } else ret += printf("%s", str2); /* zero args */
      }

      len -= n;
      str += n;
   }

   va_end(ap);
   flint_free(str2);

   return (int) ret;
}