62aeb5adb7ce7f9468407a8128908de6657fef06
[profile/ivi/ico-vic-carsimulator.git] / src / CConf.cpp
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the 
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   Read configuration file
11  * @file    CConf.cpp
12  *
13  */
14
15 #include <unistd.h>
16
17 #include "CConf.h"
18
19 CConf::CConf()
20 {
21     // TODO Auto-generated constructor stub
22
23 }
24
25 CConf::~CConf()
26 {
27     // TODO Auto-generated destructor stub
28 }
29
30 void CConf::LoadConfig(const char*filePath)
31 {
32     //CConf::GetModulePath(m_strConfPath, sizeof(m_strConfPath));
33
34     //strncat(m_strConfPath, filePath, sizeof(m_strConfPath));
35     memset(m_strConfPath, 0, sizeof(m_strConfPath));
36     strcpy(m_strConfPath, filePath);
37     printf("ConfPath:%s\n", m_strConfPath);
38
39     m_nWinkR = CConf::GetConfig(m_strConfPath, "WINKER_RIGHT", "NUMBER", 4);
40     m_nWinkL = CConf::GetConfig(m_strConfPath, "WINKER_LEFT", "NUMBER", 5);
41
42     m_nShiftU = CConf::GetConfig(m_strConfPath, "SHIFT_UP", "NUMBER", 11);
43     m_nShiftD = CConf::GetConfig(m_strConfPath, "SHIFT_DOWN", "NUMBER", 10);
44     m_nShift1 = CConf::GetConfig(m_strConfPath, "SHIFT_1", "NUMBER", 300);
45     m_nShift2 = CConf::GetConfig(m_strConfPath, "SHIFT_2", "NUMBER", 301);
46     m_nShift3 = CConf::GetConfig(m_strConfPath, "SHIFT_3", "NUMBER", 302);
47     m_nShift4 = CConf::GetConfig(m_strConfPath, "SHIFT_4", "NUMBER", 303);
48     m_nShift5 = CConf::GetConfig(m_strConfPath, "SHIFT_5", "NUMBER", 704);
49     m_nShift6 = CConf::GetConfig(m_strConfPath, "SHIFT_6", "NUMBER", 705);
50     m_nShiftR = CConf::GetConfig(m_strConfPath, "SHIFT_R", "NUMBER", 710);
51     m_nSteering = CConf::GetConfig(m_strConfPath, "STEERING", "NUMBER", 0);
52     m_nAccel = CConf::GetConfig(m_strConfPath, "ACCEL", "NUMBER", 1);
53     m_nBrake = CConf::GetConfig(m_strConfPath, "BRAKE", "NUMBER", 2);
54
55     m_nHeadLight =
56         CConf::GetConfig(m_strConfPath, "HEAD_LIGHT", "NUMBER", 292);
57
58     m_fLat =
59         CConf::GetConfig(m_strConfPath, "LASTPOSITION", "LAT", 35.717931);
60     m_fLng =
61         CConf::GetConfig(m_strConfPath, "LASTPOSITION", "LNG", 139.736518);
62     char devname[64];
63     memset(devname, 0, sizeof(devname));
64     CConf::GetConfig(m_strConfPath, "DEVICE", "NAME", "Driving Force GT", devname, sizeof(devname));
65     m_sDeviceName = std::string(devname);
66     char ambconfig[64];
67     memset(ambconfig, 0, sizeof(ambconfig));
68     CConf::GetConfig(m_strConfPath, "AMBCONFIG", "NAME", "/etc/ambd/config", ambconfig, sizeof(ambconfig));
69     m_sAmbConfigName = std::string(ambconfig);
70
71     printf("Configuration[%s]:\n",m_sDeviceName.c_str());
72     printf("  WINKER(R) button:%d\tWINKER(L) button:%d\n", m_nWinkR,
73            m_nWinkL);
74     printf("  SHIFT(U) button:%d\tSHIFT(D) button:%d\n", m_nShiftU,
75            m_nShiftD);
76     printf("  STEERING axis:%d\tACCEL axis:%d\n", m_nSteering, m_nAccel);
77 }
78
79 bool CConf::GetConfig(const char *strPath, const char *strSection,
80                       const char *strKey, const char *strDefault, char *buf,
81                       int bufsize)
82 {
83     char *line = NULL;
84     size_t len = 0;
85     std::string key = "";
86     std::string sec = "";
87
88     char retbuf[STR_BUF_SIZE];
89     memset(retbuf, 0, sizeof(retbuf));
90
91     if (strDefault == NULL) {
92         strncpy(buf, "" "", bufsize);
93         return false;
94     }
95     if ((strSection == NULL) || (strKey == NULL)) {
96         strncpy(buf, strDefault, bufsize);
97         return false;
98     }
99
100     FILE *fp = fopen(strPath, "r");
101     if (fp == NULL) {
102         strncpy(buf, strDefault, bufsize);
103         return false;
104     }
105
106     key = strKey;
107     if (key.rfind("=", key.length()) != key.length() - 1) {
108         key += "=";
109     }
110
111     while (true) {
112         int n = getline(&line, &len, fp);
113         if (n < 0) {
114             strncpy(buf, strDefault, bufsize);
115             return false;
116         }
117
118         std::string s = line;
119
120         if (s.find("//", 0) == 0) {
121             // skip comment
122             continue;
123         }
124
125         if ((s.find("[", 0) == 0)
126             && (s.rfind("]", s.length()) == s.length() - 2)) {
127             s = s.erase(0, 1);
128             sec = s.erase(s.rfind("]"), s.length() - s.rfind("]"));
129             continue;
130         }
131
132         if ((s.find(key, 0) == 0) && (sec == strSection)) {
133             s.erase(0, key.length());
134             s.erase(s.length() - 1, 2);
135
136             ::memcpy(retbuf, s.c_str(), s.length());
137             strncpy(buf, retbuf, bufsize);
138             break;
139         }
140     }
141
142
143     if (fp)
144         fclose(fp);
145     return true;
146 }
147
148 int CConf::GetConfig(const char *strPath, const char *strSection,
149                      const char *strKey, int nDefault)
150 {
151     int nRet = nDefault;
152
153     if (strPath == NULL)
154         return nRet;
155     if (strSection == NULL)
156         return nRet;
157     if (strKey == NULL)
158         return nRet;
159
160     char buf[STR_BUF_SIZE];
161
162     memset(buf, 0, sizeof(buf));
163     bool b =
164         CConf::GetConfig(strPath, strSection, strKey, "", buf, sizeof(buf));
165
166     if (!b)
167         return nRet;
168
169     return atoi((char *) buf);
170
171 }
172
173 double CConf::GetConfig(const char *strPath, const char *strSection,
174                         const char *strKey, double fDefault)
175 {
176     double fRet = fDefault;
177
178     if (strPath == NULL)
179         return fRet;
180     if (strSection == NULL)
181         return fRet;
182     if (strKey == NULL)
183         return fRet;
184
185     char buf[STR_BUF_SIZE];
186
187     bool b =
188         CConf::GetConfig(strPath, strSection, strKey, "", buf, sizeof(buf));
189
190     if (!b)
191         return fRet;
192
193     return atof((char *) buf);
194 }
195
196 void CConf::GetModulePath(char *buf, int bufsize)
197 {
198     size_t size = readlink("/proc/self/exe", buf, (size_t) bufsize);
199     buf[bufsize] = '\0';
200
201     char *p = strrchr(buf, '/');
202     if (p != 0L)
203         *p = '\0';
204
205     if (buf[strlen(buf) - 1] == '/')
206         buf[strlen(buf) - 1] = '\0';
207 }
208
209 /**
210  * End of File.(CConf.cpp)
211  */