-
Notifications
You must be signed in to change notification settings - Fork 3
/
io.cpp
139 lines (118 loc) · 3.15 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
#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 INLINE static __inline
#define PRINT printf
#define MAX_RECORD 100
#define MAX_LINE_LEN 4000
typedef struct
{
char *record[MAX_RECORD];
int head;
int tail;
int cnt;
}RECORD_QUEUE_S;
char g_result[MAX_LINE_LEN] = "NA";
INLINE void write_file(const bool cover, const char * const buff, const char * const filename);
void record_result(unsigned short edge)
{
static int len = 0;
if (len > (MAX_LINE_LEN - 10))
return;
if (len > 0)
len += sprintf(g_result + len, "|");
len += sprintf(g_result + len, "%d", edge);
}
void print_time(const char *head)
{
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;
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);
}
unsigned long get_miltime()
{
struct timeb rawtime;
struct tm * timeinfo;
ftime(&rawtime);
static int ms = rawtime.millitm;
static unsigned long s = rawtime.time;
// int ms = rawtime.millitm;
// unsigned long s = rawtime.time;
int out_ms = rawtime.millitm - ms;
unsigned long out_s = rawtime.time - s;
if (out_ms < 0)
{
out_ms += 1000;
out_s -= 1;
}
return out_ms+out_s*1000;
}
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;
fgets(line, MAX_LINE_LEN + 2, fp);
if (line[0] == 0) continue;
buff[cnt] = (char *)malloc(MAX_LINE_LEN + 2);
strncpy(buff[cnt], line, MAX_LINE_LEN + 2 - 1);
buff[cnt][4001] = 0;
cnt++;
}
fclose(fp);
// PRINT("There are %d lines in file %s.\n", cnt, filename);
return cnt;
}
void write_result(const char * const filename)
{
if (g_result[0] == '\0')
return;
write_file(1, g_result, 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";// overrite : append
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);
}