-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.cpp
executable file
·100 lines (92 loc) · 2.11 KB
/
tools.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
#include "tools.h"
#include "assert.h"
#include <stdio.h>
#include <time.h>
// return true if the machine is little endian
bool IsLittleIndian()
{
union temp {
char buf[4];
int i;
};
temp l;
l.i = 0x01020304;
return (l.buf[0] == 4);
}
#include <sys/time.h>
// return a string of local time to the ms level, only for linux now, e.g., 2012-08-30 07:09:28.589
// the caller should prepare the buffer no shorter than 24
void GetLocalTime(char *iBuf)
{
assert(iBuf != NULL);
timeval lTime;
::gettimeofday(&lTime, NULL);
struct tm now;
::localtime_r(&lTime.tv_sec, &now);
sprintf(iBuf, "%d-%02d-%02d %02d:%02d:%02d.%03ld\0",
now.tm_year + 1900,
now.tm_mon + 1,
now.tm_mday,
now.tm_hour, now.tm_min, now.tm_sec, lTime.tv_usec / 1000);
}
void WriteToFile(char *iFile, char *iBuf, unsigned int iLen)
{
if (NULL == iBuf)
return;
FILE *lOutput;
unsigned int lToWrite = iLen, lWritten = 0;
if (lOutput = fopen(iFile, "wb")) {
while (lToWrite > 0) {
lWritten = fwrite(iBuf, sizeof(char), iLen, lOutput);
if (lWritten > 0)
lToWrite -= lWritten;
else {
perror("stop writing");
break;
}
}
fclose(lOutput);
}
}
// the size should not be bigger than an unsigned int can hold
void ReadFileToSingleBuffer(char *iFile, char **oBuf, unsigned int &oLen)
{
FILE *lInput;
if (lInput = fopen(iFile, "rb")) {
fseek(lInput, 0, SEEK_END);
//unsigned int lLen = ftell(lInput);
// get the number of bytes from beginning to end
oLen = ftell(lInput);
//printf("size is %ud\n", lLen);
if (oLen == 0) {
perror("file is empty");
return;
}
char *lBuf = new char[oLen];
if (NULL == lBuf) {
oLen = 0;
perror("not enough memory");
return;
} else {
unsigned int lToRead = oLen;
unsigned int lRead = 0, lIndex = 0;
fseek(lInput, 0, SEEK_SET);
while (lToRead > 0) {
if (lRead > 0)
printf("to read one more time\n");
lRead =
fread(lBuf, sizeof(char), lToRead, lInput);
if (lRead > 0) {
lToRead -= lRead;
lIndex += lRead;
lBuf += lRead;
} else {
perror("stop reading");
}
}
lBuf -= lIndex;
*oBuf = lBuf;
}
fclose(lInput);
}
}