source: UsbWattMeter/trunk/ecnl_lwip/fbs_string.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 6 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 5.5 KB
RevLine 
[164]1/*
2 * TOPPERS Software
3 * Toyohashi Open Platform for Embedded Real-Time Systems
4 *
5 * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
6 * Toyohashi Univ. of Technology, JAPAN
7 * Copyright (C) 2004-2011 by Embedded and Real-Time Systems Laboratory
8 * Graduate School of Information Science, Nagoya Univ., JAPAN
9 * Copyright (C) 2015 Cores Co., Ltd. Japan
10 *
11 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
12 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
13 * •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
14 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
15 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
16 * ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
17 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
18 * —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
19ƒƒ“ƒgi—˜—p
20 * ŽÒƒ}ƒjƒ…
21ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
22 * ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
23 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
24 * —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
25 * ‚ƁD
26 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
27ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
28ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
29 * ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
30 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
31 * •ñ‚·‚邱‚ƁD
32 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
33 * ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
34 * ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
35 * —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
36 * –Ɛӂ·‚邱‚ƁD
37 *
38 * –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
39 * ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
40 * ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
41 * ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
42 * ‚̐ӔC‚𕉂í‚È‚¢D
43 *
44 * @(#) $Id: fbs_string.c 164 2016-03-07 11:33:50Z coas-nagasima $
45 */
46
47#include <stdint.h>
48#include <limits.h>
49#include <stdarg.h>
50#include "fbs_string.h"
51
52/*
53 * ”’l‚𕶎š—ñ‚É•ÏŠ·
54 */
55#define CONVERT_BUFLEN ((sizeof(uintptr_t) * CHAR_BIT + 2) / 3)
56 /* uintptr_tŒ^‚̐”’l‚̍ő啶Žš” */
57static ER
58convert(ECN_FBS_ID fbs, ECN_FBS_SSIZE_T *pos, uintptr_t val, uint_t radix, const char *radchar,
59 uint_t width, bool_t minus, bool_t padzero)
60{
61 char buf[CONVERT_BUFLEN];
62 uint_t i, j;
63 ER ret;
64
65 i = 0U;
66 do {
67 buf[i++] = radchar[val % radix];
68 val /= radix;
69 } while (i < CONVERT_BUFLEN && val != 0);
70
71 if (minus && width > 0) {
72 width -= 1;
73 }
74 if (minus && padzero) {
75 ret = _ecn_fbs_poke(fbs, *pos, '-');
76 if(ret != E_OK)
77 return ret;
78 (*pos)++;
79 }
80 for (j = i; j < width; j++) {
81 ret = _ecn_fbs_poke(fbs, *pos, padzero ? '0' : ' ');
82 if(ret != E_OK)
83 return ret;
84 (*pos)++;
85 }
86 if (minus && !padzero) {
87 ret = _ecn_fbs_poke(fbs, *pos, '-');
88 if(ret != E_OK)
89 return ret;
90 (*pos)++;
91 }
92 while (i > 0U) {
93 ret = _ecn_fbs_poke(fbs, *pos, buf[--i]);
94 if(ret != E_OK)
95 return ret;
96 (*pos)++;
97 }
98 return E_OK;
99}
100
101/*
102 * •¶Žš—ñ®Œ`o—Í
103 */
104static const char raddec[] = "0123456789";
105static const char radhex[] = "0123456789abcdef";
106static const char radHEX[] = "0123456789ABCDEF";
107
108ECN_FBS_SSIZE_T
109fbs_printf(ECN_FBS_ID fbs, ECN_FBS_SSIZE_T *pos, const char *format, ...)
110{
111 char c;
112 bool_t lflag;
113 uint_t width;
114 bool_t padzero;
115 intptr_t val;
116 const char *str, *head;
117 va_list ap;
118 ER ret;
119
120 va_start(ap, format);
121
122 while ((c = *format++) != '\0') {
123 if (c != '%') {
124 ret = _ecn_fbs_poke(fbs, *pos, c);
125 if (ret != E_OK)
126 return ret;
127 (*pos)++;
128 continue;
129 }
130
131 lflag = false;
132 width = 0U;
133 padzero = false;
134 if ((c = *format++) == '0') {
135 padzero = true;
136 c = *format++;
137 }
138 while ('0' <= c && c <= '9') {
139 width = width * 10U + c - '0';
140 c = *format++;
141 }
142 if (c == 'l') {
143 lflag = true;
144 c = *format++;
145 }
146 switch (c) {
147 case 'd':
148 val = lflag ? (intptr_t) va_arg(ap, long_t)
149 : (intptr_t) va_arg(ap, int_t);
150 if (val >= 0) {
151 ret = convert(fbs, pos, (uintptr_t) val, 10U, raddec,
152 width, false, padzero);
153 if (ret != E_OK)
154 return ret;
155 }
156 else {
157 ret = convert(fbs, pos, (uintptr_t)(-val), 10U, raddec,
158 width, true, padzero);
159 if (ret != E_OK)
160 return ret;
161 }
162 break;
163 case 'u':
164 val = lflag ? (intptr_t) va_arg(ap, ulong_t)
165 : (intptr_t) va_arg(ap, uint_t);
166 ret = convert(fbs, pos, (uintptr_t) val, 10U, raddec, width, false, padzero);
167 if (ret != E_OK)
168 return ret;
169 break;
170 case 'x':
171 case 'p':
172 val = lflag ? (intptr_t) va_arg(ap, ulong_t)
173 : (intptr_t) va_arg(ap, uint_t);
174 ret = convert(fbs, pos, (uintptr_t) val, 16U, radhex, width, false, padzero);
175 if (ret != E_OK)
176 return ret;
177 break;
178 case 'X':
179 val = (intptr_t)va_arg(ap, void *);
180 ret = convert(fbs, pos, (uintptr_t) val, 16U, radHEX, width, false, padzero);
181 if (ret != E_OK)
182 return ret;
183 break;
184 case 'c':
185 ret = _ecn_fbs_poke(fbs, *pos, (char)(intptr_t)va_arg(ap, int));
186 if (ret != E_OK)
187 return ret;
188 (*pos)++;
189 break;
190 case 's':
191 str = head = (const char *)va_arg(ap, const char *);
192 while ((c = *str++) != '\0') {
193 ret = _ecn_fbs_poke(fbs, *pos, c);
194 if (ret != E_OK)
195 return ret;
196 (*pos)++;
197 }
198 break;
199 case '%':
200 ret = _ecn_fbs_poke(fbs, *pos, '%');
201 if (ret != E_OK)
202 return ret;
203 (*pos)++;
204 break;
205 case '\0':
206 format--;
207 break;
208 default:
209 break;
210 }
211 }
212 va_end(ap);
213
214 return E_OK;
215}
Note: See TracBrowser for help on using the repository browser.