OSDN Git Service

1sec and high smpling file record add.
[scilog/scilog.git] / conf.c
1 #include <pthread.h>
2 #include <semaphore.h> 
3 #include <string.h>
4 #include <syslog.h>
5 #include <stdio.h>
6
7 #include "conf.h"
8 #include "debug_print.h"
9
10 //
11 /**** Station ID ***************************************************
12 */
13 #define SID_MAX 16
14 static char     gsid[SID_MAX + 1];
15
16 // ミューテックス
17 static pthread_mutex_t  mutex_sid = PTHREAD_MUTEX_INITIALIZER;
18
19 void sid_set(char *s)
20 {
21 pthread_mutex_lock(&mutex_sid);
22         strncpy(gsid, s, SID_MAX);
23         gsid[SID_MAX] = 0;
24 pthread_mutex_unlock(&mutex_sid);
25 }
26 void sid_get(char *s)
27 {
28 pthread_mutex_lock(&mutex_sid);
29         strncpy(s, gsid, SID_MAX);
30         s[SID_MAX] = 0;
31 pthread_mutex_unlock(&mutex_sid);
32 }
33 char *sid_getp(void)
34 {
35         return gsid;
36 }
37
38 //
39 /**** 設定ファイル *******************************************
40 */
41 /*
42         文字列の前から空白を取り除く
43 */
44 static void trims_space(char *name)
45 {
46         char    c;
47         char    *name0;
48         int     len;
49         
50         name0 = name;
51         len = strlen(name);
52         
53         // 最初の非空白文字までポインタ移動
54         while (*name) {
55                 c = *name;
56                 if (c != ' ') break;
57                 name++;
58                 len--;
59         }
60         if (len < 0) return;
61         // 移動
62         if (name0 != name) memmove(name0, name, len);
63         // 末尾に0入れる
64         name0[len] = 0;
65 }
66 /*
67         文字列の後ろから空白を取り除く
68 */
69 static void trime_space(char *name)
70 {
71         char    c;
72         int     len, i;
73
74         // 文字列末尾
75         len = strlen(name);
76         name += (len - 1);
77         // 最初の非空白文字までポインタ移動
78         for(i = len - 1; i >= 0; i--) {
79                 c = *name;
80                 if (c != ' ') break;
81                 name--;
82         }
83         // 末尾に0入れる
84         *(name + 1) = 0;
85 }
86 /*
87         文字列の前後から空白を取り除く
88 */
89 static void trim_space(char *name)
90 {
91         if (name == NULL) return;
92         trims_space(name);
93         trime_space(name);
94 }
95 /*
96         文字列の末尾からCR LFを除く
97 */
98 static void trim_crlf(char *name)
99 {
100         int     i;
101         int     len;
102         char    *src, *dst;
103         
104         if (name == NULL) return;
105         len = strlen(name);
106         src = name;
107         dst = name;
108         for(i = 0; i < len; i++) {
109                 if (*src != 0x0d && *src != 0x0a) {
110                         *dst = *src;
111                         dst++;
112                 }
113                 src++;
114         }
115         *dst = 0;
116 }
117
118 /*
119         読み込み
120
121 return
122         -1=ERR
123         0=OK
124 */
125 int conf_read(void)
126 {
127         FILE    *fp;
128         char    buf[256];
129         char    buf2[256];
130
131         fp = fopen(CONF_FILE, "rt");
132         if (fp == NULL) {
133                 syslog(LOG_ERR, "conf_read(): conf file not found. %s", CONF_FILE);
134                 return -1;
135         }
136
137         syslog(LOG_INFO, "conf_read():");
138         while(fgets(buf, sizeof(buf), fp)) {
139                 // CR LF除く
140                 trim_crlf(buf);
141                 trim_space(buf);
142                 PDEBUG(buf);
143                 PDEBUG("\n");
144                 // Station ID
145                 if (sscanf(buf, "sid = %s", buf2) == 1) {
146                         sid_set(buf2);
147                         syslog(LOG_INFO, "sid=%s", sid_getp());
148                 } 
149         }
150         fclose(fp);
151         return 0;
152 }
153
154