-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.cpp
254 lines (221 loc) · 5.98 KB
/
io.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#ifdef WIN32
#include "lib_record.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <sys/timeb.h>
#include <errno.h>
#include <signal.h>
#include "lib_time.h"
#define MAX_LINE_LEN 4000
#define INLINE static __inline
#ifdef _DEBUG
#define PRINT printf
#else
#define PRINT(...)
#endif
static char g_result[2][MAX_LINE_LEN] = {"NA", ""};
static int relult_len[2] = {0, 0};
INLINE void write_file(const bool cover, const char * const buff, const char * const filename);
void record_result(const PATH_ID path_id, unsigned short edge)
{
if (relult_len[path_id - 1] > (MAX_LINE_LEN - 10))
return;
if (relult_len[path_id - 1] > 0)
relult_len[path_id - 1] += sprintf(g_result[path_id - 1] + relult_len[path_id - 1], "|");
relult_len[path_id - 1] += sprintf(g_result[path_id - 1] + relult_len[path_id - 1], "%d", edge);
}
void clear_result()
{
relult_len[0]
= 0;
relult_len[1] = 0;
sprintf(g_result[0], "NA");
}
void print_time(const char *head)
{
#ifdef _DEBUG
struct timeb rawtime;
struct tm * timeinfo;
ftime(&rawtime);
timeinfo = localtime(&rawtime.time);
static int ms = rawtime.millitm;
static unsigned long s = rawtime.time;
int out_ms = rawtime.millitm - ms;
unsigned long out_s = rawtime.time - s;
ms = rawtime.millitm;
s = rawtime.time;
if (out_ms < 0)
{
out_ms += 1000;
out_s -= 1;
}
printf("%s date/time is: %s \tused time is %lu s %d ms.\n", head, asctime(timeinfo), out_s, out_ms);
#endif
}
int read_file(char ** const buff, const unsigned int spec, const char * const filename)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
PRINT("Fail to open file %s, %s.\n", filename, strerror(errno));
return 0;
}
PRINT("Open file %s OK.\n", filename);
char line[MAX_LINE_LEN + 2];
unsigned int cnt = 0;
while ((cnt < spec) && !feof(fp))
{
line[0] = 0;
if (fgets(line, MAX_LINE_LEN + 2, fp) == NULL) continue;
if (line[0] == 0) continue;
buff[cnt] = (char *)malloc(MAX_LINE_LEN + 2);
strncpy(buff[cnt], line, MAX_LINE_LEN + 2 - 1);
buff[cnt][MAX_LINE_LEN + 1] = 0;
cnt++;
}
fclose(fp);
PRINT("There are %d lines in file %s.\n", cnt, filename);
return cnt;
}
void write_result(const char * const filename)
{
write_file(1, g_result[0], filename);
if (g_result[0][0] == 'N')
return;
write_file(0, g_result[1], filename);
}
void release_buff(char ** const buff, const int valid_item_num)
{
for (int i = 0; i < valid_item_num; i++)
free(buff[i]);
}
INLINE void write_file(const bool cover, const char * const buff, const char * const filename)
{
if (buff == NULL)
return;
const char *write_type = cover ? "w" : "a";//1:覆盖写文件,0:追加写文件
FILE *fp = fopen(filename, write_type);
if (fp == NULL)
{
PRINT("Fail to open file %s, %s.\n", filename, strerror(errno));
return;
}
PRINT("Open file %s OK.\n", filename);
fputs(buff, fp);
fputs("\n", fp);
fclose(fp);
}
#else
#include "lib_record.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <sys/timeb.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#define MAX_LINE_LEN 4000
#define INLINE static __inline
#ifdef _DEBUG
#define PRINT printf
#else
#define PRINT(...)
#endif
static char g_result[2][MAX_LINE_LEN] = { "NA", "" };
static int relult_len[2] = { 0, 0 };
INLINE void write_file(const bool cover, const char * const buff, const char * const filename);
void record_result(const PATH_ID path_id, unsigned short edge)
{
if (relult_len[path_id - 1] > (MAX_LINE_LEN - 10))
return;
if (relult_len[path_id - 1] > 0)
relult_len[path_id - 1] += sprintf(g_result[path_id - 1] + relult_len[path_id - 1], "|");
relult_len[path_id - 1] += sprintf(g_result[path_id - 1] + relult_len[path_id - 1], "%d", edge);
}
void clear_result()
{
relult_len[0]
= 0;
relult_len[1] = 0;
sprintf(g_result[0], "NA");
}
void print_time(const char *head)
{
#ifdef _DEBUG
struct timeb rawtime;
struct tm * timeinfo;
ftime(&rawtime);
timeinfo = localtime(&rawtime.time);
static int ms = rawtime.millitm;
static unsigned long s = rawtime.time;
int out_ms = rawtime.millitm - ms;
unsigned long out_s = rawtime.time - s;
ms = rawtime.millitm;
s = rawtime.time;
if (out_ms < 0)
{
out_ms += 1000;
out_s -= 1;
}
printf("%s date/time is: %s \tused time is %lu s %d ms.\n", head, asctime(timeinfo), out_s, out_ms);
#endif
}
int read_file(char ** const buff, const unsigned int spec, const char * const filename)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
PRINT("Fail to open file %s, %s.\n", filename, strerror(errno));
return 0;
}
PRINT("Open file %s OK.\n", filename);
char line[MAX_LINE_LEN + 2];
unsigned int cnt = 0;
while ((cnt < spec) && !feof(fp))
{
line[0] = 0;
if (fgets(line, MAX_LINE_LEN + 2, fp) == NULL) continue;
if (line[0] == 0) continue;
buff[cnt] = (char *)malloc(MAX_LINE_LEN + 2);
strncpy(buff[cnt], line, MAX_LINE_LEN + 2 - 1);
buff[cnt][MAX_LINE_LEN + 1] = 0;
cnt++;
}
fclose(fp);
PRINT("There are %d lines in file %s.\n", cnt, filename);
return cnt;
}
void write_result(const char * const filename)
{
write_file(1, g_result[0], filename);
if (g_result[0][0] == 'N')
return;
write_file(0, g_result[1], filename);
}
void release_buff(char ** const buff, const int valid_item_num)
{
for (int i = 0; i < valid_item_num; i++)
free(buff[i]);
}
INLINE void write_file(const bool cover, const char * const buff, const char * const filename)
{
if (buff == NULL)
return;
const char *write_type = cover ? "w" : "a";//1:覆盖写文件,0:追加写文件
FILE *fp = fopen(filename, write_type);
if (fp == NULL)
{
PRINT("Fail to open file %s, %s.\n", filename, strerror(errno));
return;
}
PRINT("Open file %s OK.\n", filename);
fputs(buff, fp);
fputs("\n", fp);
fclose(fp);
}
#endif