2 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
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
10 * @brief Read configuration file
21 // TODO Auto-generated constructor stub
27 // TODO Auto-generated destructor stub
30 void CConf::LoadConfig(const char*filePath)
32 //CConf::GetModulePath(m_strConfPath, sizeof(m_strConfPath));
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);
39 m_nWinkR = CConf::GetConfig(m_strConfPath, "WINKER_RIGHT", "NUMBER", 4);
40 m_nWinkL = CConf::GetConfig(m_strConfPath, "WINKER_LEFT", "NUMBER", 5);
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);
56 CConf::GetConfig(m_strConfPath, "HEAD_LIGHT", "NUMBER", 292);
59 CConf::GetConfig(m_strConfPath, "LASTPOSITION", "LAT", 35.717931);
61 CConf::GetConfig(m_strConfPath, "LASTPOSITION", "LNG", 139.736518);
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);
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);
71 printf("Configuration[%s]:\n",m_sDeviceName.c_str());
72 printf(" WINKER(R) button:%d\tWINKER(L) button:%d\n", m_nWinkR,
74 printf(" SHIFT(U) button:%d\tSHIFT(D) button:%d\n", m_nShiftU,
76 printf(" STEERING axis:%d\tACCEL axis:%d\n", m_nSteering, m_nAccel);
79 bool CConf::GetConfig(const char *strPath, const char *strSection,
80 const char *strKey, const char *strDefault, char *buf,
88 char retbuf[STR_BUF_SIZE];
89 memset(retbuf, 0, sizeof(retbuf));
91 if (strDefault == NULL) {
92 strncpy(buf, "" "", bufsize);
95 if ((strSection == NULL) || (strKey == NULL)) {
96 strncpy(buf, strDefault, bufsize);
100 FILE *fp = fopen(strPath, "r");
102 strncpy(buf, strDefault, bufsize);
107 if (key.rfind("=", key.length()) != key.length() - 1) {
112 int n = getline(&line, &len, fp);
114 strncpy(buf, strDefault, bufsize);
118 std::string s = line;
120 if (s.find("//", 0) == 0) {
125 if ((s.find("[", 0) == 0)
126 && (s.rfind("]", s.length()) == s.length() - 2)) {
128 sec = s.erase(s.rfind("]"), s.length() - s.rfind("]"));
132 if ((s.find(key, 0) == 0) && (sec == strSection)) {
133 s.erase(0, key.length());
134 s.erase(s.length() - 1, 2);
136 ::memcpy(retbuf, s.c_str(), s.length());
137 strncpy(buf, retbuf, bufsize);
148 int CConf::GetConfig(const char *strPath, const char *strSection,
149 const char *strKey, int nDefault)
155 if (strSection == NULL)
160 char buf[STR_BUF_SIZE];
162 memset(buf, 0, sizeof(buf));
164 CConf::GetConfig(strPath, strSection, strKey, "", buf, sizeof(buf));
169 return atoi((char *) buf);
173 double CConf::GetConfig(const char *strPath, const char *strSection,
174 const char *strKey, double fDefault)
176 double fRet = fDefault;
180 if (strSection == NULL)
185 char buf[STR_BUF_SIZE];
188 CConf::GetConfig(strPath, strSection, strKey, "", buf, sizeof(buf));
193 return atof((char *) buf);
196 void CConf::GetModulePath(char *buf, int bufsize)
198 size_t size = readlink("/proc/self/exe", buf, (size_t) bufsize);
201 char *p = strrchr(buf, '/');
205 if (buf[strlen(buf) - 1] == '/')
206 buf[strlen(buf) - 1] = '\0';
210 * End of File.(CConf.cpp)