-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.c
74 lines (56 loc) · 1.24 KB
/
utils.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "utils.h"
/* File must be opened. */
long getFileLength(FILE * file)
{
long length;
long pos = ftell(file);
fseek(file, 0L, SEEK_END);
length = ftell(file);
fseek(file, pos, SEEK_SET);
return length;
}
/* Based on: see LINKS file: [4]. */
char * getTextFileContent(const char * path, long * lengthP)
{
char * content = NULL;
FILE * file;
long length = 0;
if (path == NULL)
{
return NULL;
}
file = fopen(path, "rt");
if (file == NULL)
{
return NULL;
}
length = getFileLength(file);
if (length <= 0)
{
fclose(file);
return NULL;
}
content = (char *) malloc(sizeof(char) * (length + 1));
length = fread(content, sizeof(char), length, file);
content[length] = '\0';
if (lengthP != NULL)
{
*lengthP = length;
}
return content;
}
float timeval_diff_replace(struct timeval * tv1)
{
struct timeval tv2;
float res;
gettimeofday(&tv2, NULL);
res = (tv2.tv_sec - tv1->tv_sec) +
(tv2.tv_usec - tv1->tv_usec) / 1000000.0;
tv1->tv_sec = tv2.tv_sec;
tv1->tv_usec = tv2.tv_usec;
return res;
}