Welcome to little lamb

Code » qmdoc » commit 235a1a6

Refactor to remove magic numbers

author Olivier Brunel
2023-01-02 13:15:43 UTC
committer Olivier Brunel
2023-01-04 15:10:17 UTC
parent e32870d18283baf864620e50af4d0f884b828f7d

Refactor to remove magic numbers

main.c +12 -7

diff --git a/main.c b/main.c
index 5555294..aa23cec 100644
--- a/main.c
+++ b/main.c
@@ -34,10 +34,15 @@ enum {
     CODE_BUFFERED   = (CODE_LINES | CODE_HIGHLIGHT),
 };
 
+enum {
+    CSS_QMDOC,
+    CSS_CUSTOM,
+    NB_CSS
+};
 static struct css {
     const char *file;
     size_t offset;
-} css[2] = {
+} css[NB_CSS] = {
     { "qmdoc.css" }
 };
 
@@ -308,7 +313,7 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                         || !raw_str(ctx, "</title>"))
                     return ERR_PARSER_ENTER_BLOCK;
                 if (ctx->options & OPT_INLINE_CSS) {
-                    for (int i = 0; i < 2; ++i) {
+                    for (int i = 0; i < NB_CSS; ++i) {
                         if (ctx->css[i].file &&
                                 (
                                     !raw_str(ctx, "<style>")
@@ -318,8 +323,8 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                             return ERR_PARSER_ENTER_BLOCK;
                     }
                 } else {
-                    for (int i = 0; i < 2; ++i) {
-                        if ((i == 1 || !(ctx->options & OPT_NO_CSS))
+                    for (int i = 0; i < NB_CSS; ++i) {
+                        if ((i == CSS_CUSTOM || !(ctx->options & OPT_NO_CSS))
                                 && ctx->css[i].file &&
                                 (
                                     !raw_str(ctx, "<link rel=\"stylesheet\" href=\"")
@@ -1133,7 +1138,7 @@ main (int argc, char *argv[])
             ctx.options |= OPT_NO_CSS;
             break;
         case 'c':
-            ctx.css[1].file = optarg;
+            ctx.css[CSS_CUSTOM].file = optarg;
             break;
         case 'd':
             destdir = optarg;
@@ -1206,10 +1211,10 @@ main (int argc, char *argv[])
         }
     }
 
-    if (!(ctx.options & OPT_NO_CSS) || ctx.css[1].file) {
+    if (!(ctx.options & OPT_NO_CSS) || ctx.css[CSS_CUSTOM].file) {
         outs((ctx.options & OPT_INLINE_CSS) ? "Loading" : "Copying");
         outse(" CSS files...");
-        for (int i = 0; i < 2; ++i) {
+        for (int i = 0; i < NB_CSS; ++i) {
             if (!css[i].file) continue;
             if (ctx.options & OPT_INLINE_CSS) {
                 css[i].offset = ctx.sa.len;