param.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  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 version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "param.h"
  12. char param[10][10][MENU_ITEM_MAX_LEN+1];
  13. static void GetSubStr(char *des, char *src, char ch, int n)
  14. {
  15. int i, len;
  16. char *p1, *p, tmp[300];
  17. strcpy(tmp, src);
  18. *des = 0;
  19. p1 = tmp;
  20. for (int i = 0; i < n; i++) {
  21. p = (char *)strchr(p1, ch);
  22. if (p != NULL) {
  23. *p++ = 0;
  24. p1 = p;
  25. }
  26. }
  27. p = (char *)strchr(p1, ch);
  28. if (p != NULL) {
  29. *p = 0;
  30. strcpy(des, p1);
  31. }
  32. }
  33. int get_param(char *name)
  34. {
  35. FILE *fp;
  36. char ss[201], xm[3], gs[3];
  37. int i, j;
  38. sprintf(ss, "%s.conf", name);
  39. if ((fp = fopen(ss, "r")) == NULL) return (-1);
  40. for (j = 0; j < 10; j++)
  41. for (i = 0; i < 10; i++)
  42. memset(param[j][i], 0, 13);
  43. while (1) {
  44. memset(ss, 0, 201);
  45. fgets(ss, 200, fp);
  46. if (feof(fp)) break;
  47. if (ss[0] == '#') continue;
  48. GetSubStr(xm, ss, '|', 0);
  49. GetSubStr(gs, ss, '|', 1);
  50. j = atoi(xm);
  51. for (i = 1; i <= atoi(gs); i++) {
  52. sprintf(param[j][0], "%s", gs);
  53. GetSubStr(param[j][i], ss, '|', i + 1);
  54. }
  55. }
  56. fclose(fp);
  57. return (0);
  58. }