瀏覽代碼

Merge pull request #91 from aesophor/improve-coding-style

Improve coding style - consistency of not placing a whitespace after `if`, and `while`
Fredrik Svantesson 5 年之前
父節點
當前提交
ae7a6ebb45
沒有帳戶連結到提交者的電子郵件
共有 4 個文件被更改,包括 28 次插入28 次删除
  1. 10
    10
      src/helpers.c
  2. 2
    2
      src/helpers.h
  3. 14
    14
      src/light.c
  4. 2
    2
      src/main.c

+ 10
- 10
src/helpers.c 查看文件

@@ -16,13 +16,13 @@ bool light_file_read_uint64(char const *filename, uint64_t *val)
16 16
     uint64_t data;
17 17
 
18 18
     fp = fopen(filename, "r");
19
-    if (!fp)
19
+    if(!fp)
20 20
     {
21 21
         LIGHT_PERMERR("reading");
22 22
         return false;
23 23
     }
24 24
 
25
-    if (fscanf(fp, "%lu", &data) != 1)
25
+    if(fscanf(fp, "%lu", &data) != 1)
26 26
     {
27 27
         LIGHT_ERR("Couldn't parse an unsigned integer from '%s'", filename);
28 28
         fclose(fp);
@@ -40,13 +40,13 @@ bool light_file_write_uint64(char const *filename, uint64_t val)
40 40
     FILE *fp;
41 41
 
42 42
     fp = fopen(filename, "w");
43
-    if (!fp)
43
+    if(!fp)
44 44
     {
45 45
         LIGHT_PERMERR("writing");
46 46
         return false;
47 47
     }
48 48
 
49
-    if (fprintf(fp, "%lu", val) < 0)
49
+    if(fprintf(fp, "%lu", val) < 0)
50 50
     {
51 51
         LIGHT_ERR("fprintf failed");
52 52
         fclose(fp);
@@ -68,7 +68,7 @@ bool light_file_is_writable(char const *filename)
68 68
     FILE *fp;
69 69
 
70 70
     fp = fopen(filename, "r+");
71
-    if (!fp)
71
+    if(!fp)
72 72
     {
73 73
         LIGHT_PERMWARN("writing");
74 74
         return false;
@@ -84,7 +84,7 @@ bool light_file_is_readable(char const *filename)
84 84
     FILE *fp;
85 85
 
86 86
     fp = fopen(filename, "r");
87
-    if (!fp)
87
+    if(!fp)
88 88
     {
89 89
         LIGHT_PERMWARN("reading");
90 90
         return false;
@@ -111,13 +111,13 @@ uint64_t light_log_clamp_max(uint64_t max)
111 111
 /* Clamps the `percent` value between 0% and 100% */
112 112
 double light_percent_clamp(double val)
113 113
 {
114
-    if (val < 0.0)
114
+    if(val < 0.0)
115 115
     {
116 116
         LIGHT_WARN("specified value %g%% is not valid, adjusting it to 0%%", val);
117 117
         return 0.0;
118 118
     }
119 119
 
120
-    if (val > 100.0)
120
+    if(val > 100.0)
121 121
     {
122 122
         LIGHT_WARN("specified value %g%% is not valid, adjusting it to 100%%", val);
123 123
         return 100.0;
@@ -130,13 +130,13 @@ int light_mkpath(char *dir, mode_t mode)
130 130
 {
131 131
     struct stat sb;
132 132
 
133
-    if (!dir)
133
+    if(!dir)
134 134
     {
135 135
         errno = EINVAL;
136 136
         return -1;
137 137
     }
138 138
 
139
-    if (!stat(dir, &sb))
139
+    if(!stat(dir, &sb))
140 140
         return 0;
141 141
 
142 142
     char *tempdir = strdup(dir);

+ 2
- 2
src/helpers.h 查看文件

@@ -24,7 +24,7 @@ typedef enum {
24 24
 light_loglevel_t light_loglevel;
25 25
 
26 26
 #define LIGHT_LOG(lvl, fp, fmt, args...)\
27
-    if (light_loglevel >= lvl)\
27
+    if(light_loglevel >= lvl)\
28 28
         fprintf(fp, "%s:%d:" fmt "\n", __FILE__, __LINE__, ##args)
29 29
 
30 30
 #define LIGHT_NOTE(fmt, args...) LIGHT_LOG(LIGHT_NOTE_LEVEL,  stdout, " Notice: " fmt, ##args)
@@ -35,7 +35,7 @@ light_loglevel_t light_loglevel;
35 35
     do {\
36 36
         log("could not open '%s' for " act, filename);\
37 37
         log("Verify it exists with the right permissions");\
38
-    } while (0)
38
+    } while(0)
39 39
 
40 40
 #define LIGHT_PERMERR(x)         LIGHT_PERMLOG(x, LIGHT_ERR)
41 41
 #define LIGHT_PERMWARN(x)        LIGHT_PERMLOG(x, LIGHT_WARN)

+ 14
- 14
src/light.c 查看文件

@@ -248,14 +248,14 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
248 248
             // Options
249 249
             
250 250
             case 'v':
251
-                if (sscanf(optarg, "%i", &log_level) != 1)
251
+                if(sscanf(optarg, "%i", &log_level) != 1)
252 252
                 {
253 253
                     fprintf(stderr, "-v argument is not an integer.\n\n");
254 254
                     _light_print_usage();
255 255
                     return false;
256 256
                 }
257 257
                 
258
-                if (log_level < 0 || log_level > 3)
258
+                if(log_level < 0 || log_level > 3)
259 259
                 {
260 260
                     fprintf(stderr, "-v argument must be between 0 and 3.\n\n");
261 261
                     _light_print_usage();
@@ -358,7 +358,7 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
358 358
 
359 359
     if(need_value || need_float_value)
360 360
     {
361
-        if ( (argc - optind) != 1)
361
+        if( (argc - optind) != 1)
362 362
         {
363 363
             fprintf(stderr, "please specify a <value> for this command.\n\n");
364 364
             _light_print_usage();
@@ -366,11 +366,11 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
366 366
         }
367 367
     }
368 368
 
369
-    if (need_value)
369
+    if(need_value)
370 370
     {
371
-        if (ctx->run_params.raw_mode)
371
+        if(ctx->run_params.raw_mode)
372 372
         {
373
-            if (sscanf(argv[optind], "%lu", &ctx->run_params.value) != 1)
373
+            if(sscanf(argv[optind], "%lu", &ctx->run_params.value) != 1)
374 374
             {
375 375
                 fprintf(stderr, "<value> is not an integer.\n\n");
376 376
                 _light_print_usage();
@@ -380,7 +380,7 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
380 380
         else
381 381
         {
382 382
             double percent_value = 0.0;
383
-            if (sscanf(argv[optind], "%lf", &percent_value) != 1)
383
+            if(sscanf(argv[optind], "%lf", &percent_value) != 1)
384 384
             {
385 385
                 fprintf(stderr, "<value> is not a decimal.\n\n");
386 386
                 _light_print_usage();
@@ -400,9 +400,9 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
400 400
         }
401 401
     }
402 402
 
403
-    if (need_float_value)
403
+    if(need_float_value)
404 404
     {
405
-        if (sscanf(argv[optind], "%f", &ctx->run_params.float_value) != 1)
405
+        if(sscanf(argv[optind], "%f", &ctx->run_params.float_value) != 1)
406 406
         {
407 407
             fprintf(stderr, "<value> is not a float.\n\n");
408 408
             _light_print_usage();
@@ -433,7 +433,7 @@ light_context_t* light_initialize(int argc, char **argv)
433 433
 
434 434
     // Setup the configuration folder
435 435
     // If we are root, use the system-wide configuration folder, otherwise try to find a user-specific folder, or fall back to ~/.config
436
-    if (geteuid() == 0)
436
+    if(geteuid() == 0)
437 437
     {
438 438
         snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s", "/etc/light");
439 439
     }
@@ -441,7 +441,7 @@ light_context_t* light_initialize(int argc, char **argv)
441 441
     {
442 442
         char *xdg_conf = getenv("XDG_CONFIG_HOME");
443 443
         
444
-        if (xdg_conf != NULL)
444
+        if(xdg_conf != NULL)
445 445
         {
446 446
             snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf);
447 447
         }
@@ -453,7 +453,7 @@ light_context_t* light_initialize(int argc, char **argv)
453 453
     
454 454
     // Make sure the configuration folder exists, otherwise attempt to create it
455 455
     int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
456
-    if (rc && errno != EEXIST)
456
+    if(rc && errno != EEXIST)
457 457
     {
458 458
         LIGHT_WARN("couldn't create configuration directory");
459 459
         return false;
@@ -789,7 +789,7 @@ bool light_cmd_set_min_brightness(light_context_t *ctx)
789 789
     
790 790
     // Make sure the target folder exists, otherwise attempt to create it
791 791
     int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
792
-    if (rc && errno != EEXIST)
792
+    if(rc && errno != EEXIST)
793 793
     {
794 794
         LIGHT_ERR("couldn't create target directory for minimum brightness");
795 795
         return false;
@@ -994,7 +994,7 @@ bool light_cmd_save_brightness(light_context_t *ctx)
994 994
     
995 995
     // Make sure the target folder exists, otherwise attempt to create it
996 996
     int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
997
-    if (rc && errno != EEXIST)
997
+    if(rc && errno != EEXIST)
998 998
     {
999 999
         LIGHT_ERR("couldn't create target directory for save brightness");
1000 1000
         return false;

+ 2
- 2
src/main.c 查看文件

@@ -11,12 +11,12 @@
11 11
 int main(int argc, char **argv)
12 12
 {
13 13
     light_context_t *light_ctx = light_initialize(argc, argv);
14
-    if (light_ctx == NULL) {
14
+    if(light_ctx == NULL) {
15 15
         LIGHT_ERR("Initialization failed");
16 16
         return LIGHT_RETURNVAL_INITFAIL;
17 17
     }
18 18
 
19
-    if (!light_execute(light_ctx)) {
19
+    if(!light_execute(light_ctx)) {
20 20
         LIGHT_ERR("Execution failed");
21 21
         light_free(light_ctx);
22 22
         return LIGHT_RETURNVAL_EXECFAIL;