Subversion Repositories wimsdev

Rev

Rev 13932 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 13932 Rev 14856
Line 32... Line 32...
32
(e+8 -> *10^8 ; e-8 -> *10^-8)
32
(e+8 -> *10^8 ; e-8 -> *10^-8)
33
Extra syntax error signal (using more than 1 'e')
33
Extra syntax error signal (using more than 1 'e')
34
 
34
 
35
27/2014
35
27/2014
36
Modified avoiding truncating of numbers like 15.625 --> 16.62
36
Modified avoiding truncating of numbers like 15.625 --> 16.62
37
 
37
4/2020
-
 
38
Modified to use ";" and "," as separators...preserving the sequence
-
 
39
moneyprint 1,2,3;4,5,6;7,8,9 -> 1.00,2.00,3.00;4.00,5.00,6.00;7.00,8.00,9.00
38
*/
40
*/
-
 
41
 
39
 
42
 
40
#include <stdio.h>
43
#include <stdio.h>
41
#include <stdlib.h>
44
#include <stdlib.h>
42
#include <string.h>
45
#include <string.h>
43
#include <math.h>
46
#include <math.h>
Line 48... Line 51...
48
    if( argc != 2 && argc != 3){
51
    if( argc != 2 && argc != 3){
49
        fprintf(stdout,"error !\nusage:\n!exec moneyprint $your_wims_item_list $precision_word\nexample:\nmoney=!exec moneyprint 1.2,30.1,.4,-.23123456 2\nThe result is a comma separated list: 1.20,30.10,0.40,-0.23\n using 2 decimals\nNote: no calculations are done.\nNo spaces allowed \n");
52
        fprintf(stdout,"error !\nusage:\n!exec moneyprint $your_wims_item_list $precision_word\nexample:\nmoney=!exec moneyprint 1.2,30.1,.4,-.23123456 2\nThe result is a comma separated list: 1.20,30.10,0.40,-0.23\n using 2 decimals\nNote: no calculations are done.\nNo spaces allowed \n");
50
        exit(0);
53
        exit(0);
51
    }
54
    }
52
    /* test for illegal characters */
55
    /* test for illegal characters */
53
    const char *invalid_characters = "\n\"\'!=ABCDFGHIJKLMNOPQRSTUVWYZabcdfghijklmnopqrstuvwyz@#$%&()[]{};:~><?/\\|";
56
    const char *invalid_characters = "\n\"\'!=ABCDFGHIJKLMNOPQRSTUVWYZabcdfghijklmnopqrstuvwyz@#$%&()[]{}:~><?/\\|";
54
    /* +-*^XxEe are allowed : 12.34e+05  12.34e-08 1x10^5 1.234*10^123*/
57
    /* ;+-*^XxEe are allowed : 12.34e+05  12.34e-08 1x10^5 1.234*10^123*/
55
    char *input;
58
    char *input;
56
    input = argv[1];
59
    input = argv[1];
57
    while (*input){
60
    while (*input){
58
        if ( strchr(invalid_characters, *input) ){
61
        if ( strchr(invalid_characters, *input) ){
59
            fprintf(stdout,"error !\nfound illegal character \"%c\" in argument\n",*input);
62
            fprintf(stdout,"error !\nfound illegal character \"%c\" in argument\n",*input);
60
            exit(0);
63
            exit(0);
61
        }
64
        }
62
        input++;
65
        input++;
63
    }
66
    }
64
    int DECIMALS;
67
    int DECIMALS;
65
    if(argv[2] != NULL){
68
    if(argv[2] != NULL){
66
        DECIMALS = atoi(argv[2]);
69
        DECIMALS = atoi(argv[2]);
67
        if(DECIMALS > MAX_DIGITS){
70
        if(DECIMALS > MAX_DIGITS){
68
            fprintf(stdout,"error ! maximum amount of decimals is %d \n",MAX_DIGITS);exit(0);
71
            fprintf(stdout,"error ! maximum amount of decimals is %d \n",MAX_DIGITS);exit(0);
Line 74... Line 77...
74
    }
77
    }
75
    char *ptr;
78
    char *ptr;
76
    char word[MAX_DIGITS];
79
    char word[MAX_DIGITS];
77
    char exponent[MAX_DIGITS];
80
    char exponent[MAX_DIGITS];
78
    char number[MAX_DIGITS];
81
    char number[MAX_DIGITS];
79
    int cnt = 1;
82
    int cnt = 0;
80
    int powE = 0;
83
    int powE = 0;
81
    int idx1 = 0;
84
    int idx1 = 0;
82
    int idx2 = 0;
85
    int idx2 = 0;
83
    int length= 0;
86
    int length= 0;
84
    int i = 0;
87
    int i = 0;
85
    int pow10 = 0;
88
    int pow10 = 0;
86
    double correction = 1/(pow(10,DECIMALS+6)); /* a reasonable guess...to avoid truncating 15.625 --> 16.63 */
89
    double correction = 1/(pow(10,DECIMALS+6)); /* a reasonable guess...to avoid truncating 15.625 --> 16.63 */
87
    double ascii_number;
90
    double ascii_number;
88
    input = argv[1];
91
    input = argv[1];
-
 
92
    /* 14/4/2020 replace ';' by ','  BPR */
-
 
93
    int idx3[MAX_CONV];
-
 
94
    for(i = 0; i < strlen(input); i++){
-
 
95
     if(input[i] == ';'){
-
 
96
      input[i] = ',';
-
 
97
      idx3[cnt] = 1;
-
 
98
      cnt++;
-
 
99
     }
-
 
100
     else
-
 
101
     {
-
 
102
      if(input[i] == ',' ){
-
 
103
       idx3[cnt] = 0;
-
 
104
       cnt++;
-
 
105
      }
-
 
106
     }
-
 
107
    }
-
 
108
    char *sep = ",";
-
 
109
    cnt = 1;
89
    ptr = strtok(input,",");
110
    ptr = strtok(input,",");
90
    while( ptr != NULL){
111
    while( ptr != NULL){
91
        if(  cnt > MAX_CONV ){
112
        if(  cnt > MAX_CONV ){
92
            fprintf(stdout,"ERROR too many (> %d)conversion \n",MAX_CONV);
113
            fprintf(stdout,"ERROR too many (> %d)conversion \n",MAX_CONV);
93
            exit(0);
114
            exit(0);
94
        }
115
        }
95
        // next item in input argv[1]
116
        /* next item in input argv[1] */
96
        strncpy( word, ptr, MAX_DIGITS);
117
        strncpy( word, ptr, MAX_DIGITS-1);
97
        length = strlen(ptr);
118
        length = strlen(ptr);
98
        if(length > MAX_DIGITS-1){
119
        if(length > MAX_DIGITS-1){
99
            fprintf(stdout,"ERROR string too large\n");
120
            fprintf(stdout,"ERROR string too large\n");
100
            exit(0);
121
            exit(0);
101
        }
122
        }
102
        //reset counters
123
        /* reset counters */
103
        powE = 0;
124
        powE = 0;
104
        pow10 = 0;
125
        pow10 = 0;
105
        idx1 = 0;
126
        idx1 = 0;
106
        idx2 = 0;
127
        idx2 = 0;
107
        i = 0;
128
        i = 0;
Line 154... Line 175...
154
        {
175
        {
155
            ascii_number = ascii_number - correction;
176
            ascii_number = ascii_number - correction;
156
        }
177
        }
157
        if( powE == 1 || pow10> 0 ){
178
        if( powE == 1 || pow10> 0 ){
158
            if(cnt > 1){
179
            if(cnt > 1){
159
                fprintf( stdout , ",%.*f*10^%s" , DECIMALS , ascii_number , exponent );
180
                fprintf( stdout , "%s%.*f*10^%s" ,sep, DECIMALS , ascii_number , exponent );
160
            }
181
            }
161
            else
182
            else
162
            {
183
            {
163
                fprintf( stdout , "%.*f*10^%s" , DECIMALS , ascii_number , exponent );
184
                fprintf( stdout , "%.*f*10^%s" , DECIMALS , ascii_number , exponent );
164
            }
185
            }
165
        }
186
        }
166
        else
187
        else
167
        {
188
        {
168
            if(cnt > 1 ){
189
            if(cnt > 1 ){
169
                fprintf( stdout , ",%.*f" , DECIMALS , ascii_number);  
190
                fprintf( stdout , "%s%.*f" , sep ,DECIMALS , ascii_number);    
170
            }
191
            }
171
            else
192
            else
172
            {
193
            {
173
                fprintf( stdout , "%.*f" , DECIMALS , ascii_number);   
194
                fprintf( stdout , "%.*f" , DECIMALS , ascii_number);   
174
            }
195
            }
175
        }
196
        }
-
 
197
        if(idx3[cnt-1] == 1 ){sep = ";";}else{sep = ",";}
176
        cnt++;
198
        cnt++;
177
        ptr = strtok(NULL,",");
199
        ptr = strtok(NULL,",");
178
    }
200
    }
179
    fprintf(stdout,"\n");
201
    fprintf(stdout,"\n");
180
    return 0;
202
    return 0;