Update build system, documentation and delete temp files
[wave300.git] / tools / rtlogger / logcnv / LogFmtDB.cpp
blob037474921cca68b322c3a2995cc2f3c0e39cffd7
1 /******************************************************************************
3 Copyright (c) 2012
4 Lantiq Deutschland GmbH
6 For licensing information, see the file 'LICENSE' in the root folder of
7 this software module.
9 ******************************************************************************/
10 #include "mtlkinc.h"
11 #include "LogFmtDB.h"
13 #include <fstream>
15 using namespace std;
17 #define MAKE_ORGS_KEY(oid) ((uint8)oid)
18 #define MAKE_GRPS_KEY(oid, gid) ((((uint16)(uint8)(oid)) << 8) | ((uint16)(uint8)(gid)))
19 #define MAKE_FMTS_KEY(oid, gid, fid, lid) ((((uint64)(uint8)(oid)) << 40) | \
20 (((uint64)(uint8)(gid)) << 32) | \
21 (((uint64)(uint16)(fid)) << 16) | \
22 ((uint64)(uint16)(lid)))
24 string
25 replace_all(const string &source, const string &victim, const string &replacement)
27 string answer = source;
28 string::size_type j = 0;
30 while ((j = answer.find(victim, j)) != std::string::npos )
31 answer.replace(j, victim.length(), replacement);
32 return answer;
35 void
36 CLogFmtDB::Read (string &fname)
38 ifstream ifs;
40 ifs.exceptions(ifstream::badbit); /* An error happened. */
42 ifs.open(fname.c_str());
44 while (!ifs.eof()) {
45 char record_type;
46 uint8 oid;
47 uint8 gid;
48 uint16 fid;
49 uint16 lid;
50 char buf[512];
51 uint32 val;
52 bool fmt_read;
53 string fmt_str;
55 ifs >> record_type;
57 switch (record_type) {
58 case 'O':
59 ifs >> val;
60 oid = (uint8)val;
61 ifs.getline(buf, sizeof(buf));
62 if (!ifs.fail()) { /* if all the readings were OK */
63 if (!orgs.insert(pair<uint8, string> (MAKE_ORGS_KEY(oid), buf)).second) {
64 throw bad_scd_file(oid, buf);
67 break;
68 case 'G':
69 ifs >> val;
70 oid = (uint8)val;
71 ifs >> val;
72 gid = (uint8)val;
73 ifs.getline(buf, sizeof(buf));
75 if (!ifs.fail()) { /* if all the readings were OK */
76 if (!grps.insert(pair<uint16, string> (MAKE_GRPS_KEY(oid, gid), buf)).second) {
77 throw bad_scd_file(oid, gid, buf);
80 break;
81 case 'S':
82 ifs >> val;
83 oid = (uint8)val;
84 ifs >> val;
85 gid = (uint8)val;
86 ifs >> val;
87 fid = (uint16)val;
88 ifs >> val;
89 lid = (uint16)val;
91 /* read all strings before next record - we may have multiline format strings */
92 fmt_read = false;
93 fmt_str = "";
94 while (!fmt_read) {
95 ifs.getline(buf, sizeof(buf));
96 if (ifs.fail())
97 break;
98 fmt_str.append(buf);
99 /* check if we are done: logprep inserts spaces for all multiline formats to align them */
100 ifs >> record_type;
101 ifs.unget();
102 if (record_type != '\"') {
103 fmt_read = true;
104 break;
107 if (fmt_read) {
108 fmt_str = replace_all(fmt_str, "\"\"", "");
109 if (!fmts.insert(pair<uint64, string> (MAKE_FMTS_KEY(oid, gid, fid, lid), fmt_str.c_str())).second) {
110 throw bad_scd_file(oid, gid, fid, lid, buf);
113 break;
114 default:
115 throw bad_scd_file(record_type);
116 break;
121 void
122 CLogFmtDB::GetFormat (uint8 oid, uint8 gid, uint16 fid, uint16 lid, string &res) const
124 map<uint64, string>::const_iterator it = fmts.find(MAKE_FMTS_KEY(oid, gid, fid, lid));
126 if (it != fmts.end()) {
127 res = it->second;
129 else {
130 res.clear();
134 void
135 CLogFmtDB::GetOrgName (uint8 oid, string &res) const
137 map<uint8, string>::const_iterator it = orgs.find(MAKE_ORGS_KEY(oid));
139 if (it != orgs.end()) {
140 res = it->second;
142 else {
143 res.clear();
147 void
148 CLogFmtDB::GetGrpName (uint8 oid, uint8 gid, string &res) const
150 map<uint16, string>::const_iterator it = grps.find(MAKE_GRPS_KEY(oid, gid));
152 if (it != grps.end()) {
153 res = it->second;
155 else {
156 res.clear();