|
@@ -6,229 +6,209 @@
|
6
|
6
|
#include <sys/types.h>
|
7
|
7
|
#include <dirent.h>
|
8
|
8
|
|
9
|
|
-LIGHT_BOOL light_readUInt(char const * filename, unsigned int *i)
|
|
9
|
+bool light_readUInt(char const *filename, unsigned int *i)
|
10
|
10
|
{
|
11
|
|
- FILE* fileHandle;
|
12
|
|
- unsigned int iCopy;
|
13
|
|
-
|
14
|
|
- fileHandle = fopen(filename, "r");
|
15
|
|
-
|
16
|
|
- if(!fileHandle)
|
17
|
|
- {
|
18
|
|
- LIGHT_PERMERR("reading");
|
19
|
|
- return FALSE;
|
20
|
|
- }
|
21
|
|
-
|
22
|
|
- if(fscanf(fileHandle, "%u", &iCopy) != 1)
|
23
|
|
- {
|
24
|
|
- LIGHT_ERR_FMT("Couldn't parse a positive integer number from '%s'", filename);
|
25
|
|
- fclose(fileHandle);
|
26
|
|
- return FALSE;
|
27
|
|
- }
|
28
|
|
-
|
29
|
|
- *i = iCopy;
|
30
|
|
-
|
31
|
|
- fclose(fileHandle);
|
32
|
|
- return TRUE;
|
33
|
|
-}
|
|
11
|
+ FILE *fileHandle;
|
|
12
|
+ unsigned int iCopy;
|
34
|
13
|
|
35
|
|
-LIGHT_BOOL light_writeUInt(char const * filename, unsigned int i)
|
36
|
|
-{
|
37
|
|
- FILE* fileHandle;
|
|
14
|
+ fileHandle = fopen(filename, "r");
|
38
|
15
|
|
39
|
|
- fileHandle = fopen(filename, "w");
|
|
16
|
+ if (!fileHandle) {
|
|
17
|
+ LIGHT_PERMERR("reading");
|
|
18
|
+ return false;
|
|
19
|
+ }
|
40
|
20
|
|
41
|
|
- if(!fileHandle)
|
42
|
|
- {
|
43
|
|
- LIGHT_PERMERR("writing");
|
44
|
|
- return FALSE;
|
45
|
|
- }
|
|
21
|
+ if (fscanf(fileHandle, "%u", &iCopy) != 1) {
|
|
22
|
+ LIGHT_ERR_FMT("Couldn't parse a positive integer number from '%s'", filename);
|
|
23
|
+ fclose(fileHandle);
|
|
24
|
+ return false;
|
|
25
|
+ }
|
46
|
26
|
|
47
|
|
- if(fprintf(fileHandle, "%u", i) < 0)
|
48
|
|
- {
|
49
|
|
- LIGHT_ERR("fprintf failed");
|
50
|
|
- fclose(fileHandle);
|
51
|
|
- return FALSE;
|
52
|
|
- }
|
|
27
|
+ *i = iCopy;
|
53
|
28
|
|
54
|
|
- fclose(fileHandle);
|
55
|
|
- return TRUE;
|
|
29
|
+ fclose(fileHandle);
|
|
30
|
+ return true;
|
56
|
31
|
}
|
57
|
32
|
|
58
|
|
-
|
59
|
|
-LIGHT_BOOL light_readULong(char const * filename, unsigned long *i)
|
|
33
|
+bool light_writeUInt(char const *filename, unsigned int i)
|
60
|
34
|
{
|
61
|
|
- FILE* fileHandle;
|
62
|
|
- unsigned long iCopy;;
|
63
|
|
-
|
64
|
|
- fileHandle = fopen(filename, "r");
|
65
|
|
-
|
66
|
|
- if(!fileHandle)
|
67
|
|
- {
|
68
|
|
- LIGHT_PERMERR("reading");
|
69
|
|
- return FALSE;
|
70
|
|
- }
|
71
|
|
-
|
72
|
|
- if(fscanf(fileHandle, "%lu", &iCopy) != 1)
|
73
|
|
- {
|
74
|
|
- LIGHT_ERR_FMT("Couldn't parse a positive integer number from '%s'", filename);
|
75
|
|
- fclose(fileHandle);
|
76
|
|
- return FALSE;
|
77
|
|
- }
|
78
|
|
-
|
79
|
|
- *i = iCopy;
|
80
|
|
-
|
81
|
|
- fclose(fileHandle);
|
82
|
|
- return TRUE;
|
|
35
|
+ FILE *fileHandle;
|
|
36
|
+
|
|
37
|
+ fileHandle = fopen(filename, "w");
|
|
38
|
+
|
|
39
|
+ if (!fileHandle) {
|
|
40
|
+ LIGHT_PERMERR("writing");
|
|
41
|
+ return false;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ if (fprintf(fileHandle, "%u", i) < 0) {
|
|
45
|
+ LIGHT_ERR("fprintf failed");
|
|
46
|
+ fclose(fileHandle);
|
|
47
|
+ return false;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ fclose(fileHandle);
|
|
51
|
+ return true;
|
83
|
52
|
}
|
84
|
53
|
|
85
|
|
-LIGHT_BOOL light_writeULong(char const * filename, unsigned long i)
|
|
54
|
+bool light_readULong(char const *filename, unsigned long *i)
|
86
|
55
|
{
|
87
|
|
- FILE* fileHandle;
|
|
56
|
+ FILE *fileHandle;
|
|
57
|
+ unsigned long iCopy;;
|
88
|
58
|
|
89
|
|
- fileHandle = fopen(filename, "w");
|
|
59
|
+ fileHandle = fopen(filename, "r");
|
90
|
60
|
|
91
|
|
- if(!fileHandle)
|
92
|
|
- {
|
93
|
|
- LIGHT_PERMERR("writing");
|
94
|
|
- return FALSE;
|
95
|
|
- }
|
|
61
|
+ if (!fileHandle) {
|
|
62
|
+ LIGHT_PERMERR("reading");
|
|
63
|
+ return false;
|
|
64
|
+ }
|
96
|
65
|
|
97
|
|
- if(fprintf(fileHandle, "%lu", i) < 0)
|
98
|
|
- {
|
99
|
|
- LIGHT_ERR("fprintf failed");
|
100
|
|
- fclose(fileHandle);
|
101
|
|
- return FALSE;
|
102
|
|
- }
|
|
66
|
+ if (fscanf(fileHandle, "%lu", &iCopy) != 1) {
|
|
67
|
+ LIGHT_ERR_FMT("Couldn't parse a positive integer number from '%s'", filename);
|
|
68
|
+ fclose(fileHandle);
|
|
69
|
+ return false;
|
|
70
|
+ }
|
103
|
71
|
|
104
|
|
- fclose(fileHandle);
|
105
|
|
- return TRUE;
|
|
72
|
+ *i = iCopy;
|
|
73
|
+
|
|
74
|
+ fclose(fileHandle);
|
|
75
|
+ return true;
|
106
|
76
|
}
|
107
|
77
|
|
|
78
|
+bool light_writeULong(char const *filename, unsigned long i)
|
|
79
|
+{
|
|
80
|
+ FILE *fileHandle;
|
|
81
|
+
|
|
82
|
+ fileHandle = fopen(filename, "w");
|
108
|
83
|
|
|
84
|
+ if (!fileHandle) {
|
|
85
|
+ LIGHT_PERMERR("writing");
|
|
86
|
+ return false;
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ if (fprintf(fileHandle, "%lu", i) < 0) {
|
|
90
|
+ LIGHT_ERR("fprintf failed");
|
|
91
|
+ fclose(fileHandle);
|
|
92
|
+ return false;
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ fclose(fileHandle);
|
|
96
|
+ return true;
|
|
97
|
+}
|
109
|
98
|
|
110
|
|
-LIGHT_BOOL light_readString(char const * filename, char *buffer, long* size)
|
|
99
|
+bool light_readString(char const *filename, char *buffer, long *size)
|
111
|
100
|
{
|
112
|
|
- FILE *fileHandle;
|
113
|
|
- long fileSize;
|
114
|
|
- long readSize;
|
115
|
|
-
|
116
|
|
- /* Make sure buffer pointer is null */
|
117
|
|
- if(buffer != NULL)
|
118
|
|
- {
|
119
|
|
- LIGHT_ERR("buffer passed to function isn't NULL");
|
120
|
|
- return FALSE;
|
121
|
|
- }
|
122
|
|
-
|
123
|
|
- /* Open file */
|
124
|
|
- fileHandle = fopen(filename, "r");
|
125
|
|
-
|
126
|
|
- if(!fileHandle)
|
127
|
|
- {
|
128
|
|
- LIGHT_PERMERR("reading");
|
129
|
|
- return FALSE;
|
130
|
|
- }
|
131
|
|
-
|
132
|
|
- /* Get the file size */
|
133
|
|
- fseek(fileHandle, 0L, SEEK_END);
|
134
|
|
- fileSize = ftell(fileHandle);
|
135
|
|
- rewind(fileHandle);
|
136
|
|
-
|
137
|
|
- /* Allocate the string and null-terminate it */
|
138
|
|
- buffer = malloc(sizeof(char)*(fileSize+1));
|
139
|
|
- memset(buffer, '\0', fileSize);
|
140
|
|
-
|
141
|
|
- if(buffer == NULL)
|
142
|
|
- {
|
143
|
|
- LIGHT_MEMERR();
|
144
|
|
- fclose(fileHandle);
|
145
|
|
- return FALSE;
|
146
|
|
- }
|
147
|
|
-
|
148
|
|
- /* Read the file */
|
149
|
|
- readSize = fread(buffer, sizeof(char), fileSize, fileHandle);
|
150
|
|
-
|
151
|
|
- if(readSize != fileSize)
|
152
|
|
- {
|
153
|
|
- LIGHT_ERR("read error");
|
154
|
|
- free(buffer);
|
155
|
|
- fclose(fileHandle);
|
156
|
|
- return FALSE;
|
157
|
|
- }
|
158
|
|
-
|
159
|
|
- /* Set the size */
|
160
|
|
- if(size != NULL)
|
161
|
|
- {
|
162
|
|
- *size = readSize;
|
163
|
|
- }
|
164
|
|
-
|
165
|
|
- /* All well, close handle and return TRUE */
|
166
|
|
- fclose(fileHandle);
|
167
|
|
- return TRUE;
|
|
101
|
+ FILE *fileHandle;
|
|
102
|
+ long fileSize;
|
|
103
|
+ long readSize;
|
|
104
|
+
|
|
105
|
+ /* Make sure buffer pointer is null */
|
|
106
|
+ if (buffer != NULL) {
|
|
107
|
+ LIGHT_ERR("buffer passed to function isn't NULL");
|
|
108
|
+ return false;
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ /* Open file */
|
|
112
|
+ fileHandle = fopen(filename, "r");
|
|
113
|
+
|
|
114
|
+ if (!fileHandle) {
|
|
115
|
+ LIGHT_PERMERR("reading");
|
|
116
|
+ return false;
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ /* Get the file size */
|
|
120
|
+ fseek(fileHandle, 0L, SEEK_END);
|
|
121
|
+ fileSize = ftell(fileHandle);
|
|
122
|
+ rewind(fileHandle);
|
|
123
|
+
|
|
124
|
+ /* Allocate the string and null-terminate it */
|
|
125
|
+ buffer = malloc(sizeof(char) * (fileSize + 1));
|
|
126
|
+ memset(buffer, '\0', fileSize);
|
|
127
|
+
|
|
128
|
+ if (buffer == NULL) {
|
|
129
|
+ LIGHT_MEMERR();
|
|
130
|
+ fclose(fileHandle);
|
|
131
|
+ return false;
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ /* Read the file */
|
|
135
|
+ readSize = fread(buffer, sizeof(char), fileSize, fileHandle);
|
|
136
|
+
|
|
137
|
+ if (readSize != fileSize) {
|
|
138
|
+ LIGHT_ERR("read error");
|
|
139
|
+ free(buffer);
|
|
140
|
+ fclose(fileHandle);
|
|
141
|
+ return false;
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ /* Set the size */
|
|
145
|
+ if (size != NULL) {
|
|
146
|
+ *size = readSize;
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ /* All well, close handle and return true */
|
|
150
|
+ fclose(fileHandle);
|
|
151
|
+ return true;
|
168
|
152
|
}
|
169
|
153
|
|
170
|
|
-LIGHT_BOOL light_isDir(char const * path)
|
|
154
|
+bool light_isDir(char const *path)
|
171
|
155
|
{
|
172
|
|
- DIR *dirHandle = opendir(path);
|
|
156
|
+ DIR *dirHandle = opendir(path);
|
173
|
157
|
|
174
|
|
- if(!dirHandle)
|
175
|
|
- {
|
176
|
|
- return FALSE;
|
177
|
|
- }
|
|
158
|
+ if (!dirHandle) {
|
|
159
|
+ return false;
|
|
160
|
+ }
|
178
|
161
|
|
179
|
|
- closedir(dirHandle);
|
180
|
|
- return TRUE;
|
|
162
|
+ closedir(dirHandle);
|
|
163
|
+ return true;
|
181
|
164
|
}
|
182
|
165
|
|
183
|
|
-LIGHT_BOOL light_isWritable(char const * filename)
|
|
166
|
+bool light_isWritable(char const *filename)
|
184
|
167
|
{
|
185
|
|
- FILE* fileHandle = fopen(filename, "r+");
|
|
168
|
+ FILE *fileHandle = fopen(filename, "r+");
|
186
|
169
|
|
187
|
|
- if(!fileHandle)
|
188
|
|
- {
|
189
|
|
- LIGHT_PERMWARN("writing");
|
190
|
|
- return FALSE;
|
191
|
|
- }
|
|
170
|
+ if (!fileHandle) {
|
|
171
|
+ LIGHT_PERMWARN("writing");
|
|
172
|
+ return false;
|
|
173
|
+ }
|
192
|
174
|
|
193
|
|
- fclose(fileHandle);
|
194
|
|
- return TRUE;
|
|
175
|
+ fclose(fileHandle);
|
|
176
|
+ return true;
|
195
|
177
|
}
|
196
|
178
|
|
197
|
|
-LIGHT_BOOL light_isReadable(char const * filename)
|
|
179
|
+bool light_isReadable(char const *filename)
|
198
|
180
|
{
|
199
|
|
- FILE* fileHandle = fopen(filename, "r");
|
|
181
|
+ FILE *fileHandle = fopen(filename, "r");
|
200
|
182
|
|
201
|
|
- if(!fileHandle)
|
202
|
|
- {
|
203
|
|
- LIGHT_PERMWARN("reading");
|
204
|
|
- return FALSE;
|
205
|
|
- }
|
|
183
|
+ if (!fileHandle) {
|
|
184
|
+ LIGHT_PERMWARN("reading");
|
|
185
|
+ return false;
|
|
186
|
+ }
|
206
|
187
|
|
207
|
|
- fclose(fileHandle);
|
208
|
|
- return TRUE;
|
|
188
|
+ fclose(fileHandle);
|
|
189
|
+ return true;
|
209
|
190
|
}
|
210
|
191
|
|
211
|
192
|
unsigned long light_logInfClamp(unsigned long x)
|
212
|
193
|
{
|
213
|
|
- LIGHT_NOTE_FMT("specified value is inferior to %lu (raw), so adjusting it to this mininum value", x);
|
214
|
|
- return x;
|
|
194
|
+ LIGHT_NOTE_FMT("specified value is inferior to %lu (raw), so adjusting it to this mininum value", x);
|
|
195
|
+ return x;
|
215
|
196
|
}
|
216
|
197
|
|
217
|
198
|
unsigned long light_logSupClamp(unsigned long x)
|
218
|
199
|
{
|
219
|
|
- LIGHT_NOTE_FMT("specified value is superior to %lu (raw), so adjusting it to this maximum value", x);
|
220
|
|
- return x;
|
|
200
|
+ LIGHT_NOTE_FMT("specified value is superior to %lu (raw), so adjusting it to this maximum value", x);
|
|
201
|
+ return x;
|
221
|
202
|
}
|
222
|
203
|
|
223
|
204
|
double light_clampPercent(double p)
|
224
|
205
|
{
|
225
|
|
- if(p < 0.0)
|
226
|
|
- {
|
227
|
|
- LIGHT_WARN_FMT("specified value %g%% is not valid, adjusting it to 0%%", p);
|
228
|
|
- return 0.0;
|
229
|
|
- }else if(p > 100.0){
|
230
|
|
- LIGHT_WARN_FMT("specified value %g%% is not valid, adjusting it to 100%%", p);
|
231
|
|
- return 100.0;
|
232
|
|
- }
|
233
|
|
- return p;
|
|
206
|
+ if (p < 0.0) {
|
|
207
|
+ LIGHT_WARN_FMT("specified value %g%% is not valid, adjusting it to 0%%", p);
|
|
208
|
+ return 0.0;
|
|
209
|
+ } else if (p > 100.0) {
|
|
210
|
+ LIGHT_WARN_FMT("specified value %g%% is not valid, adjusting it to 100%%", p);
|
|
211
|
+ return 100.0;
|
|
212
|
+ }
|
|
213
|
+ return p;
|
234
|
214
|
}
|