Rev 5840 | Rev 6738 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5840 | Rev 5950 | ||
---|---|---|---|
Line 5... | Line 5... | ||
5 | * This is all amateur scriblings... So no copyrights. * |
5 | * This is all amateur scriblings... So no copyrights. * |
6 | * This source code file, and compiled objects derived from it, * |
6 | * This source code file, and compiled objects derived from it, * |
7 | * can be used and distributed without restriction, including for commercial use * |
7 | * can be used and distributed without restriction, including for commercial use * |
8 | * No warrenty whatsoever * |
8 | * No warrenty whatsoever * |
9 | ********************************************************************************* |
9 | ********************************************************************************* |
- | 10 | ||
- | 11 | 12/11/2012 |
|
- | 12 | Added support for numbers like 12345*10^12 |
|
- | 13 | 12345*10^12 --> 12345E12 ---> 1.2345*10^16 |
|
10 | 14 | ||
11 | 18/10/2012 : |
15 | 18/10/2012 : |
12 | Added Mathml output |
16 | Added Mathml output |
13 | Added option significance=-1 |
17 | Added option significance=-1 |
14 | To be used when there is no significance known ; just tries to print the number in science notation |
18 | To be used when there is no significance known ; just tries to print the number in science notation |
Line 64... | Line 68... | ||
64 | #include <stdlib.h> |
68 | #include <stdlib.h> |
65 | #define MICRO "µ" |
69 | #define MICRO "µ" |
66 | #define PREFIX_START (-24) |
70 | #define PREFIX_START (-24) |
67 | #define PREFIX_END 24 |
71 | #define PREFIX_END 24 |
68 | #define MAX_CONV 256 |
72 | #define MAX_CONV 256 |
- | 73 | #define MAX_STRING 32 |
|
69 | 74 | ||
- | 75 | char *str_replace ( const char *word, const char *sub_word, const char *rep_word ){ |
|
- | 76 | if(strlen(word) > MAX_STRING){return NULL;} |
|
- | 77 | char *part_word = NULL; |
|
- | 78 | char *new_word = NULL; |
|
- | 79 | char *old_word = NULL; |
|
- | 80 | char *head = NULL; |
|
- | 81 | /* if either sub_word or rep_word is NULL, duplicate word a let caller handle it */ |
|
- | 82 | if ( sub_word == NULL || rep_word == NULL ) return strdup(word); |
|
- | 83 | new_word = strdup (word); |
|
- | 84 | head = new_word; |
|
- | 85 | while ( (part_word = strstr ( head, sub_word ))){ |
|
- | 86 | old_word = new_word; |
|
- | 87 | new_word = malloc ( strlen ( old_word ) - strlen ( sub_word ) + strlen ( rep_word ) + 1 ); |
|
- | 88 | /*failed to alloc mem, free old word and return NULL */ |
|
- | 89 | if ( new_word == NULL ){ |
|
- | 90 | free (old_word);return NULL; |
|
- | 91 | } |
|
- | 92 | memcpy ( new_word, old_word, part_word - old_word ); |
|
- | 93 | memcpy ( new_word + (part_word - old_word), rep_word, strlen ( rep_word ) ); |
|
- | 94 | memcpy ( new_word + (part_word - old_word) + strlen( rep_word ), part_word + strlen ( sub_word ), strlen ( old_word ) - strlen ( sub_word ) - ( part_word - old_word ) ); |
|
- | 95 | memset ( new_word + strlen ( old_word ) - strlen ( sub_word ) + strlen ( rep_word ) , 0, 1 ); |
|
- | 96 | /* move back head right after the last replacement */ |
|
- | 97 | head = new_word + (part_word - old_word) + strlen( rep_word ); |
|
- | 98 | free (old_word); |
|
- | 99 | } |
|
- | 100 | return new_word; |
|
- | 101 | } |
|
- | 102 | ||
70 | char *printscience(double value, int sig, int format , int cnt ,int size){ |
103 | char *printscience(double value, int sig, int format , int cnt ,int size){ |
71 | static char *prefix[] = { "y", "z", "a", "f", "p", "n", MICRO, "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y" }; |
104 | static char *prefix[] = { "y", "z", "a", "f", "p", "n", MICRO, "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y" }; |
72 | double display, fract; |
105 | double display, fract; |
73 | char *sign = NULL; |
106 | char *sign = NULL; |
74 | int exponent10; |
107 | int exponent10; |
Line 91... | Line 124... | ||
91 | while(value < 1){ |
124 | while(value < 1){ |
92 | value=value*10; |
125 | value=value*10; |
93 | exponent10--; |
126 | exponent10--; |
94 | /* need to set a limit to number of while loops ! */ |
127 | /* need to set a limit to number of while loops ! */ |
95 | if(exponent10 <-100){fprintf(stdout,"error : number too small (exponent < -100)\n");return 0;} |
128 | if(exponent10 <-100){fprintf(stdout,"error : number too small (exponent < -100)\n");return 0;} |
96 | } |
129 | } |
97 | } |
130 | } |
98 | } |
131 | } |
99 | else |
132 | else |
100 | { |
133 | { |
101 | if(value < 0.0) {sign = "-";value = -value;} else {sign = "";} |
134 | if(value < 0.0) {sign = "-";value = -value;} else {sign = "";} |
102 | exponent10 = lrint( floor( log10(value) ) );/* correctly round to desired precision */ |
135 | exponent10 = lrint( floor( log10(value) ) );/* correctly round to desired precision */ |
103 | value *= pow(10.0, sig - 1 - exponent10); |
136 | value *= pow(10.0, sig - 1 - exponent10); |
Line 150... | Line 183... | ||
150 | } |
183 | } |
151 | else |
184 | else |
152 | { |
185 | { |
153 | if(format == 3 && ((exponent10 < PREFIX_START) || (exponent10 > PREFIX_END))){ |
186 | if(format == 3 && ((exponent10 < PREFIX_START) || (exponent10 > PREFIX_END))){ |
154 | format = 1; /* if no prefix available, revert to html presentation */ |
187 | format = 1; /* if no prefix available, revert to html presentation */ |
155 | } |
188 | } |
156 | } |
189 | } |
157 | 190 | ||
158 | } |
191 | } |
159 | if(sig < 1){sig = 1;} |
192 | if(sig < 1){sig = 1;} |
160 | if(format != 3 ){ |
193 | if(format != 3 ){ |
161 | if( format == 0){ /* 'calculable' presentation */ |
194 | if( format == 0){ /* 'calculable' presentation */ |
Line 195... | Line 228... | ||
195 | double number = 0; |
228 | double number = 0; |
196 | int significance = 0,type = 0,idx = 0,cnt = 1,size = 0; |
229 | int significance = 0,type = 0,idx = 0,cnt = 1,size = 0; |
197 | char *input = "\0",*ptr = "\0"; |
230 | char *input = "\0",*ptr = "\0"; |
198 | 231 | ||
199 | /* test for illegal characters */ |
232 | /* test for illegal characters */ |
200 | const char *invalid_characters = "\n\"\' |
233 | const char *invalid_characters = "\n\"\'!=ABCDFGHIJKLMNOPQRSTUVWXYZabcdfghijklmnopqrstuvwxyz@#$%&()[]{};:~><?/\\|"; |
201 | /* Ee +- are allowed : 12.34e+05 12.34e-08 */ |
234 | /* Ee +- are allowed : 12.34e+05 12.34e-08 */ |
202 | 235 | ||
203 | /* walk through argument 1 to end, and call function scienceprint(a,b,c) */ |
236 | /* walk through argument 1 to end, and call function scienceprint(a,b,c) */ |
204 | input = argv[cnt]; |
237 | input = argv[cnt]; |
205 | while( input != NULL ){ |
238 | while( input != NULL ){ |
Line 210... | Line 243... | ||
210 | return 0; |
243 | return 0; |
211 | } |
244 | } |
212 | input++; |
245 | input++; |
213 | } |
246 | } |
214 | /* reset input to actual value */ |
247 | /* reset input to actual value */ |
215 | input = argv[cnt]; |
248 | input = argv[cnt]; |
216 | ptr = (char *) strtok(input,","); |
249 | ptr = (char *) strtok(input,","); |
217 | idx = 0; |
250 | idx = 0; |
218 | type = 0; |
251 | type = 0; |
219 | size = 0; |
252 | size = 0; |
220 | while( ptr != NULL ){ |
253 | while( ptr != NULL ){ |
221 | switch( idx ){ |
254 | switch( idx ){ |
222 | case 0: |
255 | case 0: |
223 | /* size only interesting when 'significance=-1' |
256 | /* size only interesting when 'significance=-1' |
224 | determine number of digits : 1.23445e+23 -> size = 6 |
257 | determine number of digits : 1.23445e+23 -> size = 6 |
225 | */ |
258 | */ |
- | 259 | size = strlen(ptr); |
|
226 |
|
260 | if( strstr(ptr,".") != NULL){size = size - 1 ;} |
- | 261 | if( strstr(ptr,"*10^") != NULL){ |
|
- | 262 | ptr = str_replace(ptr,"*10^","E"); |
|
- | 263 | if(ptr == NULL){ |
|
- | 264 | fprintf(stdout,"error : in replacement of 10^ notation\n"); |
|
- | 265 | return 0; |
|
- | 266 | } |
|
- | 267 | size = size - 3; |
|
- | 268 | } |
|
227 | if( strstr(ptr,"E") != NULL){size = size - strlen(strstr(ptr,"E"));} |
269 | if( strstr(ptr,"E") != NULL){size = size - strlen(strstr(ptr,"E"));} |
228 | if( strstr(ptr,"e") != NULL){size = size - strlen(strstr(ptr,"e"));} |
270 | if( strstr(ptr,"e") != NULL){size = size - strlen(strstr(ptr,"e"));} |
- | 271 | number = atof(ptr); |
|
229 | break; |
272 | break; |
230 | case 1: significance = atoi(ptr); break; |
273 | case 1: significance = atoi(ptr); break; |
231 | case 2: type = atoi(ptr); if(type < 0 || type > 4 ){type = 0;} break; |
274 | case 2: type = atoi(ptr); if(type < 0 || type > 4 ){type = 0;} break; |
232 | default: break; |
275 | default: break; |
233 | } |
276 | } |
Line 242... | Line 285... | ||
242 | input = argv[cnt]; |
285 | input = argv[cnt]; |
243 | } |
286 | } |
244 | fprintf(stdout,"\n"); |
287 | fprintf(stdout,"\n"); |
245 | return 0; |
288 | return 0; |
246 | } |
289 | } |
- | 290 | ||
- | 291 | ||
- | 292 |