summaryrefslogtreecommitdiff
path: root/src/config.c
blob: 1446c981a79e1e8f0cb3703cbfe7efe8a5fea0eb (plain)
    1 /**
    2  * I3cstat prints a configurable status bar for the i3 window manager
    3  * Copyright (C) 2022 Aaron Ball <nullspoon@oper.io>
    4  * 
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  * 
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  * 
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  */
   18 #include "config.h"
   19 
   20 int _ini_read_header(char* line, char* out) {
   21   int i = 0;
   22   if(line[0] != '[') {
   23     out = NULL;
   24     return -1;
   25   }
   26   while(line[i] != ']' && line[i] != '\0')
   27     i++;
   28 
   29   strncpy(out, &line[1], i - 1);
   30   out[i-1] = '\0';
   31   return 0;
   32 }
   33 
   34 int _ini_read_keyval(char* line, char* key, char* val) {
   35   int i = 0;
   36   char buf[256] = {'\0'};
   37   while(line[i] != '=')
   38     i++;
   39 
   40   // Copy the key
   41   strncpy(&buf[0], line, i);
   42   buf[i] = '\0';
   43   strcpy(key, trim(buf));
   44 
   45   // Copy the value
   46   strcpy(val, trim(&line[i+1]));
   47   return 0;
   48 }
   49 
   50 
   51 struct node* config_load(char* path) {
   52   FILE* fd;
   53   char buf[256] = {'\0'};
   54   char section[256] = {'\0'};
   55   char key[256] = {'\0'};
   56   char val[256] = {'\0'};
   57   fd = fopen(path, "r");
   58 
   59   struct node* list = NULL;
   60   struct node* cur = NULL;
   61 
   62   if(!fd)
   63     return NULL;
   64 
   65   while(fgets(buf, 256, fd) != NULL) {
   66     // Skip comments
   67     if(buf[0] == '#')
   68       continue;
   69 
   70     // If we've found a section
   71     if(buf[0] == '[') {
   72       _ini_read_header(buf, section);
   73 
   74       if(strcmp(section, "fs") == 0) {
   75         cur = node_new(&config_fs_init);
   76 
   77       } else if(strcmp(section, "net") == 0) {
   78         cur = node_new(&config_net_init);
   79 
   80       } else if(strcmp(section, "time") == 0) {
   81         cur = node_new(&config_time_init);
   82 
   83       } else if(strcmp(section, "date") == 0) {
   84         cur = node_new(&config_date_init);
   85 
   86       } else if(strcmp(section, "bat") == 0) {
   87         cur = node_new(&config_bat_init);
   88 
   89       } else if(strcmp(section, "mem") == 0) {
   90         cur = node_new(&config_mem_init);
   91 
   92       } else if(strcmp(section, "swap") == 0) {
   93         cur = node_new(&config_swap_init);
   94 
   95       } else if(strcmp(section, "cpu") == 0) {
   96         cur = node_new(&config_cpu_init);
   97 
   98       } else if(strcmp(section, "shell") == 0) {
   99         cur = node_new(&config_shell_init);
  100       }
  101 
  102       if(!list)
  103         list = cur;
  104       else
  105         lladd(list, cur);
  106 
  107       continue;
  108     }
  109 
  110     if(str_is_empty(buf))
  111       continue;
  112 
  113     _ini_read_keyval(buf, key, val);
  114 
  115     // Check if generic node key
  116     if(strcmp(key, "label") == 0 
  117        || strcmp(key, "text") == 0
  118        || strcmp(key, "color") == 0
  119        || strcmp(key, "label_color") == 0
  120        || strcmp(key, "display") == 0
  121        || strcmp(key, "width") == 0
  122        || strcmp(key, "interval") == 0) {
  123       _load_node_key(cur, key, val);
  124       continue;
  125     }
  126 
  127     cur->loadkey(cur, key, val);
  128   }
  129 
  130   fclose(fd);
  131   return list;
  132 }

Generated by cgit