1 | #include <iostream> |
---|
2 | #include <fstream> |
---|
3 | #include <cstring> |
---|
4 | #include <errno.h> |
---|
5 | #include <sys/stat.h> |
---|
6 | |
---|
7 | // size_t indent = 0; |
---|
8 | // size_t tabstop = 4; |
---|
9 | |
---|
10 | int testStringVal( |
---|
11 | const char *testname, |
---|
12 | const char *desc, |
---|
13 | const char *expected, |
---|
14 | const char *received) |
---|
15 | { |
---|
16 | if ((!expected && received) || |
---|
17 | (expected && !received) || |
---|
18 | (expected && received && strcmp(expected,received) != 0)) { |
---|
19 | printf("Error: %s\n", testname); |
---|
20 | printf("\t%s\n", desc); |
---|
21 | printf("\texpected \"%s\"\n",expected); |
---|
22 | printf("\treceived \"%s\"\n",received); |
---|
23 | return 1; |
---|
24 | } |
---|
25 | return 0; |
---|
26 | } |
---|
27 | |
---|
28 | int testDoubleVal( |
---|
29 | const char *testname, |
---|
30 | const char *desc, |
---|
31 | double expected, |
---|
32 | double received) |
---|
33 | { |
---|
34 | if (expected != received) { |
---|
35 | printf("Error: %s\n", testname); |
---|
36 | printf("\t%s\n", desc); |
---|
37 | printf("\texpected \"%g\"\n",expected); |
---|
38 | printf("\treceived \"%g\"\n",received); |
---|
39 | return 1; |
---|
40 | } |
---|
41 | return 0; |
---|
42 | } |
---|
43 | |
---|
44 | size_t |
---|
45 | readFile ( |
---|
46 | const char *filePath, |
---|
47 | const char **buf) |
---|
48 | { |
---|
49 | if (buf == NULL) { |
---|
50 | fprintf(stderr,"buf is NULL while opening file \"%s\"", filePath); |
---|
51 | return 0; |
---|
52 | } |
---|
53 | |
---|
54 | FILE *f; |
---|
55 | f = fopen(filePath, "rb"); |
---|
56 | if (f == NULL) { |
---|
57 | fprintf(stderr,"can't open \"%s\": %s", filePath, strerror(errno)); |
---|
58 | return 0; |
---|
59 | } |
---|
60 | struct stat stat; |
---|
61 | if (fstat(fileno(f), &stat) < 0) { |
---|
62 | fprintf(stderr,"can't stat \"%s\": %s", filePath, strerror(errno)); |
---|
63 | return 0; |
---|
64 | } |
---|
65 | off_t size; |
---|
66 | size = stat.st_size; |
---|
67 | char* memblock; |
---|
68 | memblock = new char [size+1]; |
---|
69 | if (memblock == NULL) { |
---|
70 | fprintf(stderr,"can't allocate %zu bytes for file \"%s\": %s", |
---|
71 | (size_t)size, filePath, strerror(errno)); |
---|
72 | fclose(f); |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | size_t nRead; |
---|
77 | nRead = fread(memblock, sizeof(char), size, f); |
---|
78 | fclose(f); |
---|
79 | |
---|
80 | if (nRead != (size_t)size) { |
---|
81 | fprintf(stderr,"can't read %zu bytes from \"%s\": %s", |
---|
82 | (size_t) size, filePath, strerror(errno)); |
---|
83 | return 0; |
---|
84 | } |
---|
85 | |
---|
86 | memblock[size] = '\0'; |
---|
87 | *buf = memblock; |
---|
88 | return nRead; |
---|
89 | } |
---|
90 | |
---|