Skip to content

Commit 73dd0be

Browse files
committed
Refactor
1 parent 6fd9923 commit 73dd0be

File tree

3 files changed

+16
-112
lines changed

3 files changed

+16
-112
lines changed

include/command_base.h

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ class CommandBase {
7171
return true;
7272
}
7373

74+
/**
75+
* Check if all readings in a vector are empty (for removeEmptyJson filtering)
76+
*/
77+
static bool areAllReadingsEmpty(const std::vector<std::map<std::string, std::string>>& readings) {
78+
for (const auto& r : readings) {
79+
if (!r.empty()) return false;
80+
}
81+
return true;
82+
}
83+
7484
/**
7585
* Check if a reading should be included based on all active filters.
7686
* Subclasses can override for additional filtering.
@@ -153,67 +163,6 @@ class CommandBase {
153163
return true;
154164
}
155165

156-
/**
157-
* Parse common filter options from command line.
158-
* Returns the next index after consuming the argument (if any).
159-
* Returns -1 if the argument was not recognized.
160-
*/
161-
int parseFilterOption(int argc, char* argv[], int i, const std::string& arg) {
162-
if (arg == "--remove-errors") {
163-
removeErrors = true;
164-
return i;
165-
} else if (arg == "--remove-empty-json") {
166-
removeEmptyJson = true;
167-
return i;
168-
} else if (arg == "--not-empty") {
169-
if (i + 1 < argc) {
170-
++i;
171-
notEmptyColumns.insert(argv[i]);
172-
return i;
173-
} else {
174-
std::cerr << "Error: " << arg << " requires an argument" << std::endl;
175-
return -1;
176-
}
177-
} else if (arg == "--only-value") {
178-
if (i + 1 < argc) {
179-
++i;
180-
std::string filter = argv[i];
181-
size_t colonPos = filter.find(':');
182-
if (colonPos == std::string::npos || colonPos == 0 || colonPos == filter.length() - 1) {
183-
std::cerr << "Error: --only-value requires format 'column:value'" << std::endl;
184-
return -1;
185-
}
186-
std::string column = filter.substr(0, colonPos);
187-
std::string value = filter.substr(colonPos + 1);
188-
onlyValueFilters[column].insert(value);
189-
return i;
190-
} else {
191-
std::cerr << "Error: " << arg << " requires an argument" << std::endl;
192-
return -1;
193-
}
194-
} else if (arg == "--exclude-value") {
195-
if (i + 1 < argc) {
196-
++i;
197-
std::string filter = argv[i];
198-
size_t colonPos = filter.find(':');
199-
if (colonPos == std::string::npos || colonPos == 0 || colonPos == filter.length() - 1) {
200-
std::cerr << "Error: --exclude-value requires format 'column:value'" << std::endl;
201-
return -1;
202-
}
203-
std::string column = filter.substr(0, colonPos);
204-
std::string value = filter.substr(colonPos + 1);
205-
excludeValueFilters[column].insert(value);
206-
return i;
207-
} else {
208-
std::cerr << "Error: " << arg << " requires an argument" << std::endl;
209-
return -1;
210-
}
211-
}
212-
213-
// Not a filter option
214-
return -2;
215-
}
216-
217166
/**
218167
* Copy common options from the CommonArgParser
219168
*/

src/data_counter.cpp

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,7 @@ long long DataCounter::countFromFile(const std::string& filename) {
5858
auto readings = JsonParser::parseJsonLine(line);
5959

6060
// Skip empty JSON arrays/objects if removeEmptyJson is set
61-
if (removeEmptyJson) {
62-
bool allEmpty = true;
63-
for (const auto& r : readings) {
64-
if (!r.empty()) { allEmpty = false; break; }
65-
}
66-
if (allEmpty) continue;
67-
}
61+
if (removeEmptyJson && areAllReadingsEmpty(readings)) continue;
6862

6963
for (const auto& reading : readings) {
7064
if (reading.empty()) continue;
@@ -116,13 +110,7 @@ long long DataCounter::countFromStdin() {
116110

117111
auto readings = JsonParser::parseJsonLine(line);
118112

119-
if (removeEmptyJson) {
120-
bool allEmpty = true;
121-
for (const auto& r : readings) {
122-
if (!r.empty()) { allEmpty = false; break; }
123-
}
124-
if (allEmpty) continue;
125-
}
113+
if (removeEmptyJson && areAllReadingsEmpty(readings)) continue;
126114

127115
for (const auto& reading : readings) {
128116
if (reading.empty()) continue;
@@ -176,13 +164,7 @@ void DataCounter::countFromStdinFollow() {
176164
// JSON format
177165
auto readings = JsonParser::parseJsonLine(line);
178166

179-
if (removeEmptyJson) {
180-
bool allEmpty = true;
181-
for (const auto& r : readings) {
182-
if (!r.empty()) { allEmpty = false; break; }
183-
}
184-
if (allEmpty) continue;
185-
}
167+
if (removeEmptyJson && areAllReadingsEmpty(readings)) continue;
186168

187169
for (const auto& reading : readings) {
188170
if (reading.empty()) continue;
@@ -243,13 +225,7 @@ void DataCounter::countFromFileFollow(const std::string& filename) {
243225
} else {
244226
auto readings = JsonParser::parseJsonLine(line);
245227

246-
if (removeEmptyJson) {
247-
bool allEmpty = true;
248-
for (const auto& r : readings) {
249-
if (!r.empty()) { allEmpty = false; break; }
250-
}
251-
if (allEmpty) continue;
252-
}
228+
if (removeEmptyJson && areAllReadingsEmpty(readings)) continue;
253229

254230
for (const auto& reading : readings) {
255231
if (reading.empty()) continue;
@@ -287,13 +263,7 @@ void DataCounter::countFromFileFollow(const std::string& filename) {
287263
} else {
288264
auto readings = JsonParser::parseJsonLine(line);
289265

290-
if (removeEmptyJson) {
291-
bool allEmpty = true;
292-
for (const auto& r : readings) {
293-
if (!r.empty()) { allEmpty = false; break; }
294-
}
295-
if (allEmpty) continue;
296-
}
266+
if (removeEmptyJson && areAllReadingsEmpty(readings)) continue;
297267

298268
for (const auto& reading : readings) {
299269
if (reading.empty()) continue;
@@ -323,15 +293,6 @@ DataCounter::DataCounter(int argc, char* argv[]) : followMode(false) {
323293
}
324294
}
325295

326-
// Parse filter options
327-
for (int i = 1; i < argc; ++i) {
328-
std::string arg = argv[i];
329-
int result = parseFilterOption(argc, argv, i, arg);
330-
if (result >= 0) {
331-
i = result;
332-
}
333-
}
334-
335296
// Parse --follow flag
336297
for (int i = 1; i < argc; ++i) {
337298
std::string arg = argv[i];

src/sensor_data_transformer.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,8 @@ SensorDataTransformer::SensorDataTransformer(int argc, char* argv[], bool reject
390390
std::cerr << "Error: " << arg << " requires an argument" << std::endl;
391391
exit(1);
392392
}
393-
} else {
394-
// Try to parse as filter option
395-
int result = parseFilterOption(argc, argv, i, arg);
396-
if (result >= 0) {
397-
i = result;
398-
}
399-
// CommonArgParser will handle remaining options
400393
}
394+
// CommonArgParser will handle remaining options
401395
}
402396

403397
// Second pass: parse common flags and collect files

0 commit comments

Comments
 (0)