-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupportLib.c
More file actions
108 lines (76 loc) · 2.06 KB
/
Copy pathsupportLib.c
File metadata and controls
108 lines (76 loc) · 2.06 KB
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
#include "supportLib.h"
unsigned char *DoubleArrayToByteArray(double *data, size_t length){
unsigned char *out;
size_t i;
out = (unsigned char*)malloc(sizeof(unsigned char)*length);
for(i = 0; i < length; i++){
out[i] = data[i];
}
return out;
}
void WriteToFile(double *data, size_t dataLength, char *filename){
unsigned char *bytes;
bytes = DoubleArrayToByteArray(data, dataLength);
FILE* file = fopen(filename, "wb");
fwrite(bytes, 1, dataLength, file);
free(bytes);
}
double *ByteArrayToDoubleArray(unsigned char *data, size_t length){
double *out;
size_t i;
out = (double*)malloc(sizeof(double)*length);
for(i = 0; i < length; i++){
out[i] = data[i];
}
return out;
}
_Bool ReadTextFromFile(char *filename, wchar_t **text, size_t *textLength){
FILE *file;
unsigned char *bytes;
size_t bytesLength;
const char *tmp;
_Bool success;
file = fopen(filename, "rb");
if(file != NULL){
fseek(file, 0, SEEK_END);
bytesLength = ftell(file);
rewind(file);
// +4 extra bytes for 0-termination
bytes = (unsigned char *)malloc((bytesLength + 4) * sizeof(unsigned char));
bytesLength = fread(bytes, 1, bytesLength, file);
fclose(file);
bytes[bytesLength + 0] = '\0';
bytes[bytesLength + 1] = '\0';
bytes[bytesLength + 2] = '\0';
bytes[bytesLength + 3] = '\0';
//fprintf(stderr, "bytes: %ld\n", bytesLength);
setlocale(LC_CTYPE, "en_US.utf8");
*text = (wchar_t *)malloc(bytesLength * sizeof(wchar_t));
tmp = bytes;
*textLength = mbsrtowcs(*text, &tmp, bytesLength, NULL);
if(*textLength != -1){
//fprintf(stderr, "chars: %ld", *textLength);
free(bytes);
success = true;
}else{
fprintf(stderr, "errno: %d, %ld\n", errno, ((char*)tmp - (char*)bytes));
success = false;
}
}else{
success = false;
}
return success;
}
void PrintString(wchar_t *text, size_t textLength){
int i;
for(i = 0; i < textLength; i++){
putwchar(text[i]);
}
}
void PrintStringToStderr(wchar_t *text, size_t textLength){
int i;
for(i = 0; i < textLength; i++){
fprintf(stderr, "%lc", text[i]);
//fputwc(text[i], stderr);
}
}