Browse Source

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

Improve coding style - consistency of not placing a whitespace after `if`, and `while`
Fredrik Svantesson 4 years ago
parent
commit
ae7a6ebb45
No account linked to committer's email
4 changed files with 28 additions and 28 deletions
  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 View File

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

+ 2
- 2
src/helpers.h View File

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

+ 14
- 14
src/light.c View File

248
             // Options
248
             // Options
249
             
249
             
250
             case 'v':
250
             case 'v':
251
-                if (sscanf(optarg, "%i", &log_level) != 1)
251
+                if(sscanf(optarg, "%i", &log_level) != 1)
252
                 {
252
                 {
253
                     fprintf(stderr, "-v argument is not an integer.\n\n");
253
                     fprintf(stderr, "-v argument is not an integer.\n\n");
254
                     _light_print_usage();
254
                     _light_print_usage();
255
                     return false;
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
                     fprintf(stderr, "-v argument must be between 0 and 3.\n\n");
260
                     fprintf(stderr, "-v argument must be between 0 and 3.\n\n");
261
                     _light_print_usage();
261
                     _light_print_usage();
358
 
358
 
359
     if(need_value || need_float_value)
359
     if(need_value || need_float_value)
360
     {
360
     {
361
-        if ( (argc - optind) != 1)
361
+        if( (argc - optind) != 1)
362
         {
362
         {
363
             fprintf(stderr, "please specify a <value> for this command.\n\n");
363
             fprintf(stderr, "please specify a <value> for this command.\n\n");
364
             _light_print_usage();
364
             _light_print_usage();
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
                 fprintf(stderr, "<value> is not an integer.\n\n");
375
                 fprintf(stderr, "<value> is not an integer.\n\n");
376
                 _light_print_usage();
376
                 _light_print_usage();
380
         else
380
         else
381
         {
381
         {
382
             double percent_value = 0.0;
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
                 fprintf(stderr, "<value> is not a decimal.\n\n");
385
                 fprintf(stderr, "<value> is not a decimal.\n\n");
386
                 _light_print_usage();
386
                 _light_print_usage();
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
             fprintf(stderr, "<value> is not a float.\n\n");
407
             fprintf(stderr, "<value> is not a float.\n\n");
408
             _light_print_usage();
408
             _light_print_usage();
433
 
433
 
434
     // Setup the configuration folder
434
     // Setup the configuration folder
435
     // If we are root, use the system-wide configuration folder, otherwise try to find a user-specific folder, or fall back to ~/.config
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
         snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s", "/etc/light");
438
         snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s", "/etc/light");
439
     }
439
     }
441
     {
441
     {
442
         char *xdg_conf = getenv("XDG_CONFIG_HOME");
442
         char *xdg_conf = getenv("XDG_CONFIG_HOME");
443
         
443
         
444
-        if (xdg_conf != NULL)
444
+        if(xdg_conf != NULL)
445
         {
445
         {
446
             snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf);
446
             snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf);
447
         }
447
         }
453
     
453
     
454
     // Make sure the configuration folder exists, otherwise attempt to create it
454
     // Make sure the configuration folder exists, otherwise attempt to create it
455
     int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
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
         LIGHT_WARN("couldn't create configuration directory");
458
         LIGHT_WARN("couldn't create configuration directory");
459
         return false;
459
         return false;
789
     
789
     
790
     // Make sure the target folder exists, otherwise attempt to create it
790
     // Make sure the target folder exists, otherwise attempt to create it
791
     int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
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
         LIGHT_ERR("couldn't create target directory for minimum brightness");
794
         LIGHT_ERR("couldn't create target directory for minimum brightness");
795
         return false;
795
         return false;
994
     
994
     
995
     // Make sure the target folder exists, otherwise attempt to create it
995
     // Make sure the target folder exists, otherwise attempt to create it
996
     int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
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
         LIGHT_ERR("couldn't create target directory for save brightness");
999
         LIGHT_ERR("couldn't create target directory for save brightness");
1000
         return false;
1000
         return false;

+ 2
- 2
src/main.c View File

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