summaryrefslogtreecommitdiff
path: root/src/config.c
blob: 07bbc2fdd8a0cf228a90c5a05883b3cc8f8d4798 (plain)
    1 /**
    2  * I3cstatus prints a configurable status bar for the i3 window manager
    3  * Copyright (C) 2020 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, "cpu") == 0) {
   93         cur = node_new(&config_cpu_init);
   94 
   95       } else if(strcmp(section, "shell") == 0) {
   96         cur = node_new(&config_shell_init);
   97       }
   98 
   99       if(!list)
  100         list = cur;
  101       else
  102         lladd(list, cur);
  103 
  104       continue;
  105     }
  106 
  107     if(str_is_empty(buf))
  108       continue;
  109 
  110     _ini_read_keyval(buf, key, val);
  111 
  112     // Check if generic node key
  113     if(strcmp(key, "label") == 0 
  114        || strcmp(key, "text") == 0
  115        || strcmp(key, "color") == 0
  116        || strcmp(key, "label_color") == 0
  117        || strcmp(key, "display") == 0
  118        || strcmp(key, "interval") == 0) {
  119       _load_node_key(cur, key, val);
  120       continue;
  121     }
  122 
  123     cur->loadkey(cur, key, val);
  124   }
  125 
  126   fclose(fd);
  127   return list;
  128 }

Generated by cgit