easyconfig.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Dmitry Smirnov, 2013
  2. // Valentin Dudouyt, 2014
  3. //
  4. // This program is free software; you can redistribute it and/or modify it
  5. // under the terms of the GNU General Public License as published by the
  6. // Free Software Foundation; either version 2, or (at your option) any
  7. // later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include "easyconfig.h"
  23. #define LINE_LENGTH 200
  24. FILE *pFile;
  25. char param_value[50];
  26. char* config_path;
  27. int config_lines_qty=0;
  28. struct conf{
  29. char config_line[LINE_LENGTH];
  30. char param_name[40];
  31. char param_value[40];
  32. } *config_content;
  33. static char *str_trim_left(char *str) {
  34. while(isspace(*str)) str++;
  35. return str;
  36. }
  37. static char *str_trim_right(char *str) {
  38. char *end = str + strlen(str) - 1;
  39. while(isspace(*end) && end >= str) {
  40. *end = '\0'; end--;
  41. }
  42. return str;
  43. }
  44. int Config_init(const char *path) {
  45. pFile=fopen(path,"w");
  46. if (pFile == NULL) { return 1; }
  47. return Config_open(path);
  48. }
  49. int Config_open(const char *path) {
  50. int ret=0, counter=0, counter1=1;
  51. char temp[LINE_LENGTH], *result;
  52. config_content = (struct conf*)calloc(1, sizeof(struct conf));
  53. config_path = (char*) path;
  54. if(!(pFile=fopen(config_path,"r"))) {
  55. return 1;
  56. }
  57. while (fgets(temp,LINE_LENGTH,pFile)) {
  58. config_lines_qty++;
  59. }
  60. config_content = (struct conf*)realloc(config_content, (config_lines_qty+50)*sizeof(struct conf));
  61. int i;
  62. for (i=0;i<config_lines_qty+50;i++) {
  63. strcpy(config_content[i].config_line, "");
  64. strcpy(config_content[i].param_name, "");
  65. strcpy(config_content[i].param_value, "");
  66. }
  67. rewind(pFile);
  68. while (fgets(temp,LINE_LENGTH,pFile)) {
  69. if (strlen(temp)>LINE_LENGTH) { printf("Config error. Too long line: %d\n", counter1); ret=1; break; }
  70. if (strlen(temp)<4) {
  71. counter1++;
  72. continue;
  73. }
  74. strcpy(config_content[counter].config_line,temp);
  75. result=strtok(temp,"=");
  76. result=str_trim_right(result);
  77. if (!result) { printf("Config error. Line: %d\n", counter1); ret=1; break; }
  78. strcpy(config_content[counter].param_name,result);
  79. result=strtok(NULL,"\n");
  80. result=str_trim_left(result);
  81. if (!result) { printf("Config error. Line: %d\n", counter1); ret=1; break; }
  82. strcpy(config_content[counter].param_value,result);
  83. counter++; counter1++;
  84. }
  85. fclose(pFile);
  86. return ret;
  87. }
  88. char *Config_get_str(const char *par_name) {
  89. int i;
  90. for (i=0;i<config_lines_qty;i++) {
  91. if (!strcmp(config_content[i].param_name,par_name)) {
  92. strcpy(param_value,config_content[i].param_value);
  93. return param_value;
  94. }
  95. }
  96. return NULL;
  97. }
  98. void Config_set_str(const char *par_name, const char *value) {
  99. int i;
  100. for (i=0;i<config_lines_qty;i++) {
  101. if (!strcmp(config_content[i].param_name,par_name)) {
  102. strcpy(config_content[i].param_value, value);
  103. sprintf(config_content[i].config_line,"%s = %s\n",par_name,value);
  104. return;
  105. }
  106. }
  107. sprintf(config_content[i].config_line,"%s = %s\n",par_name,value);
  108. strcpy(config_content[i].param_name, par_name);
  109. strcpy(config_content[i].param_value, value);
  110. config_lines_qty++;
  111. return;
  112. }
  113. int Config_get_int(const char *par_name) {
  114. unsigned int intval;
  115. char *strval = Config_get_str(par_name);
  116. if(!sscanf(strval, "0x%04x", &intval) && !sscanf(strval, "%d", &intval)) {
  117. return 0;
  118. }
  119. return(intval);
  120. }
  121. void Config_set_int(const char *par_name, unsigned int value) {
  122. char strval[16];
  123. sprintf(strval, "0x%04x", value);
  124. Config_set_str(par_name, strval);
  125. }
  126. int Config_close() {
  127. int i=0;
  128. pFile=fopen(config_path,"w");
  129. while (i<config_lines_qty) {
  130. fputs (config_content[i].config_line,pFile);
  131. i++;
  132. }
  133. fclose(pFile);
  134. free (config_content);
  135. return 0;
  136. }