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
|
//
// cbf_standardize_numbers.c
//
//
// Created by Herbert J. Bernstein on 12/31/15.
//
//
#include "cbf.h"
#include "cbf_standardize_numbers.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
char * fgetln(FILE *, size_t *);
void usage ( void ) {
fprintf(stderr,
"cbf_standardize_numbers: Usage: \n"
" cbf_standardize_numbers file [digits] \n"
" or cbf_standardize_numbers - [digits} for use as a filter for 6 digits");
return;
}
int main (int argc, char ** argv) {
FILE *file;
char format[10];
size_t lineln, ii;
char * line;
int c;
int bracket;
int termc;
int numcomp;
int spacestarted;
int baposstarted;
int digits = 6;
double number, anumber, ascale, fpart;
CBF_UNUSED( bracket );
CBF_UNUSED( fpart );
if (argc > 3 || argc < 2) {
usage();
return 1;
}
file = stdin;
if (strcmp(argv[1],"-") && ! (file = fopen(argv[1],"r"))) {
fprintf (stderr,"Couldn't open the input file %s\n", argv[1]);
exit (1);
}
if (argc > 2) digits = atoi(argv[2]);
sprintf(format,"%%.%dg ",(digits>0)?digits+1:0);
ascale = pow(10.,digits);
while (!feof(file) && !ferror(file) && (line=fgetln(file,&lineln))) {
bracket = '\0';
termc = '\0';
spacestarted = 1;
baposstarted = 0;
numcomp = 0;
while(lineln) {
c = *line;
c &= 0xFF;
if ( c == 'b' && line[1] == '\'' ) {
baposstarted = 1;
lineln -= 2;
line += 2;
continue;
}
if( c == '\'' && baposstarted ) {
baposstarted = 0;
lineln--;
line++;
continue;
}
if (isspace(c) || c == '[' || c == '(' || c == '{') {
bracket = c;
lineln--;
line++;
if (!spacestarted) fputc (' ',stdout);
switch (c) {
case '[': termc = ']';
termc &= 0xFF;
fputc (c,stdout); fputc (' ',stdout); spacestarted = 1; numcomp = 0; break;
case '{': termc = '}';
termc &= 0xFF;
fputc (c,stdout); fputc (' ',stdout); spacestarted = 1; numcomp = 0; break;
case '(': termc = ')';
termc &= 0xFF;
fputc (c,stdout); fputc (' ',stdout); spacestarted = 1; numcomp = 0; break;
default: spacestarted = 1; break;
}
continue;
}
if (isdigit(c) || c == '.' || c == '-' || c == '+') {
char text[lineln+1];
char * endptr;
for (ii=0; ii < lineln; ii++) text[ii] = line[ii];
text[lineln] = '\0';
if (!spacestarted) fputc (' ',stdout);
number = strtod(text,&endptr)*ascale;
anumber = fabs(number);
if (anumber < 0.5) number = 0.;
if (number < 0.) fpart=modf(number-0.5,&number);
if (number > 0.) fpart=modf(number+0.5,&number);
number /= ascale;
if (termc && numcomp > 0) fprintf(stdout,", ");
if (endptr == NULL || endptr != text) {
fprintf(stdout,format,number);
if (endptr == NULL) {
lineln=0;
} else {
line += (endptr-text);
lineln -= (endptr-text);
spacestarted = 1;
}
numcomp++;
} else {
fprintf(stdout,". ");
line ++;
lineln --;
spacestarted = 1;
numcomp++;
}
continue;
}
if (c == ',' || (termc && c == termc)) {
if (!spacestarted) fputc (' ',stdout);
fputc (c,stdout);
fputc (' ',stdout);
numcomp = 0;
line ++;
lineln --;
spacestarted = 0;
if (termc && c == termc) termc = '\0';
continue;
}
fputc (c,stdout);
line ++;
lineln --;
}
fputc ('\n', stdout);
}
return 0;
}
|