-
Notifications
You must be signed in to change notification settings - Fork 0
/
minicsv-test.c
142 lines (113 loc) · 2.51 KB
/
minicsv-test.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "minicsv.h"
typedef enum{
waitsu=0,
toshin,
tsuyinshan,
}SOLD_MODE;
static char code[16]={0};
static unsigned char price_cnt=0;
static SOLD_MODE stock_mode;
static int display_cols(char * const * const cols, const size_t cols_count);
static void help_func(void);
static int
display_cols(char * const * const cols, const size_t cols_count)
{
size_t i = (size_t) 0U;
char code_str[16]={0};
int ret=0,check_val;
check_val=(stock_mode==tsuyinshan)? 0:1;
sprintf(code_str,"%s",code);
while (i < cols_count)
{
if(i==check_val)
{
if(strcmp(code_str,cols[i])!=0)
return 0;
else
ret=1;
}
if(ret==1)
{
if(stock_mode==tsuyinshan)
{
if(i<2 || i>7)
printf("%s\t", cols[i]);
}
else
{
printf("%s\t", cols[i]);
}
}
i++;
}
//putchar('\n');
return ret;
}
static void
help_func(void)
{
printf("-h: help list. ex: ./mini -h\n");
printf("-p: code price research. ex: ./mini -p 201409_2349.csv 2014/09/11\n");
printf("-toshin: 3 people sold search. ex: ./mini -toshin 20140911.csv 2349\n");
printf("-waitsu: 3 people sold search. ex: ./mini -waitsu 20140911.csv 2349\n");
printf("-tsuyinshan: 3 people sold search. ex: ./mini -tsuyinshan 20140911.csv 2349\n");
}
int
main(int argc, char *argv[])
{
char *cols[20];
char *r;
size_t cols_count;
int recordcnt=0;
int scan_tmp1,scan_tmp2,scan_tmp3;
char tmp[1024]={0x0},type[16]={0x0};
FILE *in=(FILE*)NULL;
sprintf(type,"%s",argv[1]);
if(strcmp(type,"-h")==0)
{
help_func();
goto exit;
}
in=fopen(argv[2],"r");
if(in==NULL)
perror("File open error\n");
if(strcmp(type,"-toshin")==0 || strcmp(type,"-waitsu")==0 || strcmp(type,"-tsuyinshan")==0 )
{
if(strlen(argv[3])==4)
sprintf(code,"%s ",argv[3]);
else
sprintf(code,"%s",argv[3]);
if(strcmp(type,"-toshin")==0)
stock_mode=toshin;
else if(strcmp(type,"-waitsu")==0)
stock_mode=waitsu;
else if(strcmp(type,"-tsuyinshan")==0)
stock_mode=tsuyinshan;
}
else if(strcmp(type,"-p")==0)
{
sscanf(argv[3],"%04d/%02d/%02d",&scan_tmp1,&scan_tmp2,&scan_tmp3);
scan_tmp1-=1911;
sprintf(code," %d/%02d/%02d",scan_tmp1,scan_tmp2,scan_tmp3);
}
else
{
printf("error input\n");
goto exit;
}
while(fgets(tmp,sizeof(tmp),in)!=0) /* read a record */
{
int i=0;
recordcnt++;
r = minicsv_parse_line(tmp, cols, &cols_count, sizeof cols / sizeof cols[0]);
if(display_cols(cols, cols_count)==1)
break;
}
exit:
if(in)
fclose(in);
return 0;
}