Bläddra i källkod

Coding style: de-camelize, simplify and refactor

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
Joachim Nilsson 6 år sedan
förälder
incheckning
1aa2b217eb
5 ändrade filer med 510 tillägg och 670 borttagningar
  1. 1
    1
      src/Makefile.am
  2. 47
    151
      src/helpers.c
  3. 29
    67
      src/helpers.h
  4. 400
    383
      src/light.c
  5. 33
    68
      src/light.h

+ 1
- 1
src/Makefile.am Visa fil

@@ -1,7 +1,7 @@
1 1
 bin_PROGRAMS   = light
2 2
 light_SOURCES  = main.c light.c light.h helpers.c helpers.h
3 3
 light_CPPFLAGS = -I../include -D_XOPEN_SOURCE=500
4
-light_CFLAGS   = -W -Wall -Wextra -pedantic -std=c99
4
+light_CFLAGS   = -W -Wall -Wextra -std=gnu99
5 5
 
6 6
 install-exec-hook:
7 7
 	chmod 4755 $(DESTDIR)$(bindir)/light

+ 47
- 151
src/helpers.c Visa fil

@@ -6,209 +6,105 @@
6 6
 #include <sys/types.h>
7 7
 #include <dirent.h>
8 8
 
9
-bool light_readUInt(char const *filename, unsigned int *i)
9
+bool light_file_read_val(char const *filename, unsigned long *val)
10 10
 {
11
-	FILE *fileHandle;
12
-	unsigned int iCopy;
11
+	FILE *fp;
12
+	unsigned long data;
13 13
 
14
-	fileHandle = fopen(filename, "r");
15
-
16
-	if (!fileHandle) {
14
+	fp = fopen(filename, "r");
15
+	if (!fp) {
17 16
 		LIGHT_PERMERR("reading");
18 17
 		return false;
19 18
 	}
20 19
 
21
-	if (fscanf(fileHandle, "%u", &iCopy) != 1) {
22
-		LIGHT_ERR_FMT("Couldn't parse a positive integer number from '%s'", filename);
23
-		fclose(fileHandle);
20
+	if (fscanf(fp, "%lu", &data) != 1) {
21
+		LIGHT_ERR("Couldn't parse a positive integer number from '%s'", filename);
22
+		fclose(fp);
24 23
 		return false;
25 24
 	}
26 25
 
27
-	*i = iCopy;
26
+	*val = data;
28 27
 
29
-	fclose(fileHandle);
28
+	fclose(fp);
30 29
 	return true;
31 30
 }
32 31
 
33
-bool light_writeUInt(char const *filename, unsigned int i)
32
+bool light_file_write_val(char const *filename, unsigned long val)
34 33
 {
35
-	FILE *fileHandle;
36
-
37
-	fileHandle = fopen(filename, "w");
34
+	FILE *fp;
38 35
 
39
-	if (!fileHandle) {
36
+	fp = fopen(filename, "w");
37
+	if (!fp) {
40 38
 		LIGHT_PERMERR("writing");
41 39
 		return false;
42 40
 	}
43 41
 
44
-	if (fprintf(fileHandle, "%u", i) < 0) {
42
+	if (fprintf(fp, "%lu", val) < 0) {
45 43
 		LIGHT_ERR("fprintf failed");
46
-		fclose(fileHandle);
44
+		fclose(fp);
47 45
 		return false;
48 46
 	}
49 47
 
50
-	fclose(fileHandle);
48
+	fclose(fp);
51 49
 	return true;
52 50
 }
53 51
 
54
-bool light_readULong(char const *filename, unsigned long *i)
52
+/* Returns true if file is writable, false otherwise */
53
+bool light_file_is_writable(char const *filename)
55 54
 {
56
-	FILE *fileHandle;
57
-	unsigned long iCopy;;
58
-
59
-	fileHandle = fopen(filename, "r");
60
-
61
-	if (!fileHandle) {
62
-		LIGHT_PERMERR("reading");
63
-		return false;
64
-	}
65
-
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
-	}
71
-
72
-	*i = iCopy;
73
-
74
-	fclose(fileHandle);
75
-	return true;
76
-}
77
-
78
-bool light_writeULong(char const *filename, unsigned long i)
79
-{
80
-	FILE *fileHandle;
81
-
82
-	fileHandle = fopen(filename, "w");
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
-}
98
-
99
-bool light_readString(char const *filename, char *buffer, long *size)
100
-{
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
-	}
55
+	FILE *fp;
110 56
 
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;
152
-}
153
-
154
-bool light_isDir(char const *path)
155
-{
156
-	DIR *dirHandle = opendir(path);
157
-
158
-	if (!dirHandle) {
159
-		return false;
160
-	}
161
-
162
-	closedir(dirHandle);
163
-	return true;
164
-}
165
-
166
-bool light_isWritable(char const *filename)
167
-{
168
-	FILE *fileHandle = fopen(filename, "r+");
169
-
170
-	if (!fileHandle) {
57
+	fp = fopen(filename, "r+");
58
+	if (!fp) {
171 59
 		LIGHT_PERMWARN("writing");
172 60
 		return false;
173 61
 	}
174 62
 
175
-	fclose(fileHandle);
63
+	fclose(fp);
176 64
 	return true;
177 65
 }
178 66
 
179
-bool light_isReadable(char const *filename)
67
+/* Returns true if file is readable, false otherwise */
68
+bool light_file_is_readable(char const *filename)
180 69
 {
181
-	FILE *fileHandle = fopen(filename, "r");
70
+	FILE *fp;
182 71
 
183
-	if (!fileHandle) {
72
+	fp = fopen(filename, "r");
73
+	if (!fp) {
184 74
 		LIGHT_PERMWARN("reading");
185 75
 		return false;
186 76
 	}
187 77
 
188
-	fclose(fileHandle);
78
+	fclose(fp);
189 79
 	return true;
190 80
 }
191 81
 
192
-unsigned long light_logInfClamp(unsigned long x)
82
+/* Prints a notice about a value which was below `x` and was adjusted to it */
83
+unsigned long light_log_clamp_min(unsigned long min)
193 84
 {
194
-	LIGHT_NOTE_FMT("specified value is inferior to %lu (raw), so adjusting it to this mininum value", x);
195
-	return x;
85
+	LIGHT_NOTE("too small value, adjusting to mininum %lu (raw)", min);
86
+	return min;
196 87
 }
197 88
 
198
-unsigned long light_logSupClamp(unsigned long x)
89
+/* Prints a notice about a value which was above `x` and was adjusted to it */
90
+unsigned long light_log_clamp_max(unsigned long max)
199 91
 {
200
-	LIGHT_NOTE_FMT("specified value is superior to %lu (raw), so adjusting it to this maximum value", x);
201
-	return x;
92
+	LIGHT_NOTE("too large value, adjusting to mavalimum %lu (raw)", max);
93
+	return max;
202 94
 }
203 95
 
204
-double light_clampPercent(double p)
96
+/* Clamps the `percent` value between 0% and 100% */
97
+double light_percent_clamp(double val)
205 98
 {
206
-	if (p < 0.0) {
207
-		LIGHT_WARN_FMT("specified value %g%% is not valid, adjusting it to 0%%", p);
99
+	if (val < 0.0) {
100
+		LIGHT_WARN("specified value %g%% is not valid, adjusting it to 0%%", val);
208 101
 		return 0.0;
209
-	} else if (p > 100.0) {
210
-		LIGHT_WARN_FMT("specified value %g%% is not valid, adjusting it to 100%%", p);
102
+	}
103
+
104
+	if (val > 100.0) {
105
+		LIGHT_WARN("specified value %g%% is not valid, adjusting it to 100%%", val);
211 106
 		return 100.0;
212 107
 	}
213
-	return p;
108
+
109
+	return val;
214 110
 }

+ 29
- 67
src/helpers.h Visa fil

@@ -3,21 +3,14 @@
3 3
 
4 4
 #include <stdbool.h>
5 5
 
6
-/* Clamps x(value) between y(min) and z(max) in a nested ternary operation. 
7
- * if(x < y)
8
- * {
9
- *  y;
10
- * }else{
11
- *  if(x>z)
12
- *  {
13
- *    z;
14
- *  }else{
15
- *    x;
16
- *  }
17
- * }*/
18
-#define LIGHT_CLAMP(x, y, z) ((x<y) ? (light_logInfClamp(y)) : ((x>z) ? (light_logSupClamp(z)) : x ))
6
+/* Clamps x(value) between y(min) and z(max) in a nested ternary operation */
7
+#define LIGHT_CLAMP(val, min, max)					\
8
+	(val < min							\
9
+	 ? light_log_clamp_min(min)					\
10
+	 : (val > max							\
11
+	    ? light_log_clamp_max(max)					\
12
+	    : val ))
19 13
 
20
-#define LIGHT_LOG_FMT_BUF_SIZE 1024
21 14
 /* Verbosity levels: 
22 15
  * 0 - No output
23 16
  * 1 - Errors
@@ -30,64 +23,33 @@ typedef enum {
30 23
 	LIGHT_NOTE_LEVEL
31 24
 } light_loglevel_t;
32 25
 
33
-light_loglevel_t light_verbosity;
34
-char light_log_buffer[LIGHT_LOG_FMT_BUF_SIZE];
26
+light_loglevel_t light_loglevel;
35 27
 
36
-#define LIGHT_LOG(lvl,f,t,x)if(light_verbosity >= lvl){fprintf(f,t": \"%s\", in \"%s\" on line %u.\n", x, __FILE__, __LINE__);}
28
+#define LIGHT_LOG(lvl, fp, fmt, args...)				\
29
+	if (light_loglevel >= lvl)					\
30
+		fprintf(fp, "%s:%d:" fmt, __FILE__, __LINE__, ##args)
37 31
 
38
-#define LIGHT_NOTE(x)LIGHT_LOG(LIGHT_NOTE_LEVEL,stdout,"notice",x)
32
+#define LIGHT_NOTE(fmt, args...) LIGHT_LOG(LIGHT_NOTE_LEVEL,  stdout, "NOTE:" fmt, ##args)
33
+#define LIGHT_WARN(fmt, args...) LIGHT_LOG(LIGHT_WARN_LEVEL,  stderr, "WARN:" fmt, ##args)
34
+#define LIGHT_ERR(fmt, args...)  LIGHT_LOG(LIGHT_ERROR_LEVEL, stderr, "!ERR:" fmt, ##args)
35
+#define LIGHT_MEMERR()           LIGHT_ERR("memory error");
36
+#define LIGHT_PERMLOG(act, log)						\
37
+	do {								\
38
+		log("could not open '%s' for " act, filename);		\
39
+		log("Verify it exists with the right permissions");	\
40
+	} while (0)
41
+#define LIGHT_PERMERR(x)         LIGHT_PERMLOG(x, LIGHT_ERR)
42
+#define LIGHT_PERMWARN(x)        LIGHT_PERMLOG(x, LIGHT_WARN)
39 43
 
40
-#define LIGHT_WARN(x)LIGHT_LOG(LIGHT_WARN_LEVEL,stderr,"warning",x)
44
+bool light_file_write_val   (char const *filename, unsigned long val);
45
+bool light_file_read_val    (char const *filename, unsigned long *val);
41 46
 
42
-#define LIGHT_ERR(x)LIGHT_LOG(LIGHT_ERROR_LEVEL,stderr,"error",x)
47
+bool light_file_is_writable (char const *filename);
48
+bool light_file_is_readable (char const *filename);
43 49
 
44
-#define LIGHT_LOG_FMT(x,s,f)if(snprintf(light_log_buffer, LIGHT_LOG_FMT_BUF_SIZE,x,s) > 0){f(light_log_buffer);}
50
+unsigned long light_log_clamp_min(unsigned long min);
51
+unsigned long light_log_clamp_max(unsigned long max);
45 52
 
46
-#define LIGHT_NOTE_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_NOTE);
47
-
48
-#define LIGHT_WARN_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_WARN);
49
-
50
-#define LIGHT_ERR_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_ERR);
51
-
52
-#define LIGHT_MEMERR() LIGHT_ERR("memory error");
53
-
54
-#define LIGHT_PERMLOG(x,f)f##_FMT("could not open '%s' for "x,filename); f("check if this file exists or if you have the right permissions");
55
-
56
-#define LIGHT_PERMERR(x) LIGHT_PERMLOG(x,LIGHT_ERR)
57
-
58
-#define LIGHT_PERMWARN(x) LIGHT_PERMLOG(x,LIGHT_WARN)
59
-
60
-/* Reads an unsigned integer from a file into `i` if able, otherwise returns false and leaves `i` untouched */
61
-bool light_readUInt(char const *filename, unsigned int *v);
62
-
63
-/* Writes an unsigned integer `i` into file `filename` if able, otherwise returns false */
64
-bool light_writeUInt(char const *filename, unsigned int v);
65
-
66
-bool light_writeULong(char const *filename, unsigned long v);
67
-bool light_readULong(char const *filename, unsigned long *v);
68
-
69
-/* Reads a file into null-terminated `buffer` if able, otherwise returns false
70
- * If `size` isn't NULL, it will be set to the read size.
71
- *
72
- * WARNING: `buffer` HAS to be freed by the user, also make sure it is NULL before passed */
73
-bool light_readString(char const *filename, char *buffer, long *size);
74
-
75
-/* Returns true if `path` is a valid directory, false otherwise */
76
-bool light_isDir(char const *path);
77
-
78
-/* Returns true if file is writable, false otherwise */
79
-bool light_isWritable(char const *filename);
80
-
81
-/* Returns true if file is readable, false otherwise */
82
-bool light_isReadable(char const *filename);
83
-
84
-/* Clamps the `percent` value between 0% and 100% */
85
-double light_clampPercent(double percent);
86
-
87
-/* Prints a notice about a value which was below `x` and was adjusted to it */
88
-unsigned long light_logInfClamp(unsigned long x);
89
-
90
-/* Prints a notice about a value which was above `x` and was adjusted to it */
91
-unsigned long light_logSupClamp(unsigned long x);
53
+double light_percent_clamp(double percent);
92 54
 
93 55
 #endif /* LIGHT_HELPERS_H */

+ 400
- 383
src/light.c
Filskillnaden har hållits tillbaka eftersom den är för stor
Visa fil


+ 33
- 68
src/light.h Visa fil

@@ -12,18 +12,18 @@
12 12
 #define LIGHT_YEAR   "2012-2018"
13 13
 #define LIGHT_AUTHOR "Fredrik Haikarainen"
14 14
 
15
-#define ASSERT_SET(t,v)							\
15
+#define ASSERT_SET(t, v)						\
16 16
 	if (v) {							\
17
-		fprintf(stderr, t" arguments cannot be used in conjunction.\n"); \
17
+		fprintf(stderr, t " cannot be used more than once.\n"); \
18 18
 		return false;						\
19 19
 	}								\
20 20
 	v = true;
21 21
 
22
-#define ASSERT_OPSET() ASSERT_SET("Operation", opSet)
23
-#define ASSERT_TARGETSET() ASSERT_SET("Target", targetSet)
24
-#define ASSERT_FIELDSET() ASSERT_SET("Field", fieldSet)
25
-#define ASSERT_CTRLSET() ASSERT_SET("Controller", ctrlSet)
26
-#define ASSERT_VALSET() ASSERT_SET("Value", valSet)
22
+#define ASSERT_CMDSET()    ASSERT_SET("Commands", cmd_set)
23
+#define ASSERT_TARGETSET() ASSERT_SET("Targets", target_set)
24
+#define ASSERT_FIELDSET()  ASSERT_SET("Fields", field_set)
25
+#define ASSERT_CTRLSET()   ASSERT_SET("Controllers", ctrl_set)
26
+#define ASSERT_VALSET()    ASSERT_SET("Values", val_set)
27 27
 
28 28
 typedef enum {
29 29
 	LIGHT_BRIGHTNESS = 0,
@@ -59,79 +59,44 @@ typedef enum {
59 59
 	LIGHT_PERCENT
60 60
 } light_val_mode_t;
61 61
 
62
-typedef struct light_runtimeArguments_s {
62
+typedef struct {
63 63
 	/* Which controller to use */
64
-	light_ctrl_mode_t controllerMode;
65
-	char specifiedController[NAME_MAX + 1];
64
+	light_ctrl_mode_t ctrl;
65
+	char              ctrl_name[NAME_MAX + 1];
66 66
 
67 67
 	/* What to do with the controller */
68
-	light_cmd_t operationMode;
69
-	light_val_mode_t valueMode;
70
-	unsigned long specifiedValueRaw;	/* The specified value in raw mode */
71
-	double specifiedValuePercent;	/* The specified value in percent */
68
+	light_cmd_t        cmd;
69
+	light_val_mode_t   val_mode;
70
+	unsigned long      val_raw;	/* The specified value in raw mode */
71
+	double             val_percent;	/* The specified value in percent */
72 72
 
73
-	light_target_t target;
74
-	light_field_t field;
73
+	light_target_t     target;
74
+	light_field_t      field;
75 75
 
76 76
 	/* Cache data */
77
-	bool hasCachedMaxBrightness;
78
-	unsigned long cachedMaxBrightness;
79
-
80
-} light_runtimeArguments, *light_runtimeArguments_p;
77
+	bool               has_cached_brightness_max;
78
+	unsigned long      cached_brightness_max;
79
+} light_ctx_t;
81 80
 
82 81
 /* -- Global variable holding the settings for the current run -- */
83
-light_runtimeArguments light_Configuration;
84
-
85
-/* Sets default values for the configuration */
86
-void light_defaultConfig();
87
-
88
-/* Parses the program arguments and sets the configuration accordingly (unsanitized) */
89
-bool light_parseArguments(int argc, char **argv);
90
-
91
-/* Prints a header if verbosity level > 0 */
92
-void light_printVersion(void);
93
-
94
-/* Prints help regardless of verbosity level */
95
-void light_printHelp(void);
96
-
97
-/* -- SECTION: Main code -- */
98
-
99
-/* Initializes the application */
100
-bool light_initialize(int argc, char **argv);
101
-
102
-/* Does the work */
103
-bool light_execute(void);
104
-
105
-/* Frees up resources */
106
-void light_free();
107
-
108
-/* SECTION: Controller functionality */
109
-
110
-/* WARNING: `buffer` HAS to be freed by the user if not null once returned!
111
- * Size is always NAME_MAX + 1 */
112
-bool light_genPath(char const *controller, light_target_t target, light_field_t type, char **buffer);
113
-
114
-bool light_validControllerName(char const *controller);
115
-
116
-bool light_getBrightness(char const *controller, unsigned long *v);
117
-
118
-bool light_getMaxBrightness(char const *controller, unsigned long *v);
119
-
120
-bool light_setBrightness(char const *controller, unsigned long v);
121
-
122
-bool light_controllerAccessible(char const *controller);
123
-
124
-/* WARNING: `controller` HAS to be at most NAME_MAX, otherwise fails */
125
-bool light_getBestController(char *controller);
82
+light_ctx_t ctx;
126 83
 
127
-bool light_getMinCap(char const *controller, bool * hasMinCap, unsigned long *minCap);
84
+bool light_initialize              (int argc, char **argv);
85
+bool light_execute                 (void);
86
+void light_free                    (void);
128 87
 
129
-bool light_setMinCap(char const *controller, unsigned long v);
88
+bool light_ctrl_list               (void);
89
+bool light_ctrl_probe              (char *controller);
90
+bool light_ctrl_exist              (char const *controller);
130 91
 
131
-bool light_listControllers();
92
+bool light_ctrl_get_brightness     (char const *controller, unsigned long *v);
93
+bool light_ctrl_set_brightness     (char const *controller, unsigned long v);
94
+bool light_ctrl_get_brightness_max (char const *controller, unsigned long *v);
132 95
 
133
-bool light_saveBrightness(char const *controller, unsigned long v);
96
+bool light_ctrl_get_cap_min        (char const *controller, bool *hasMinCap, unsigned long *minCap);
97
+bool light_ctrl_set_cap_min        (char const *controller, unsigned long val);
134 98
 
135
-bool light_restoreBrightness(char const *controller);
99
+bool light_ctrl_save_brightness    (char const *controller, unsigned long val);
100
+bool light_ctrl_restore_brightness (char const *controller);
136 101
 
137 102
 #endif /* LIGHT_H_ */