blob: 648edf7ecd6fb45e49a1ff2592a2d3290829ad60 [file] [log] [blame]
Yuchen He6ceb09d2021-08-12 21:40:26 +00001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ParseUtils.h>
18#include <sstream>
19#include <stdexcept>
20
21namespace android {
22namespace hardware {
23namespace gnss {
24namespace common {
25
26int ParseUtils::tryParseInt(const std::string& s, int defaultVal) {
27 if (s.empty()) {
28 return defaultVal;
29 } else {
30 return std::stoi(s);
31 }
32}
33
34float ParseUtils::tryParsefloat(const std::string& s, float defaultVal) {
35 if (s.empty()) {
36 return defaultVal;
37 } else {
38 return std::stof(s);
39 }
40}
41
42double ParseUtils::tryParseDouble(const std::string& s, double defaultVal) {
43 if (s.empty()) {
44 return defaultVal;
45 } else {
46 return std::stod(s);
47 }
48}
49
50long ParseUtils::tryParseLong(const std::string& s, long defaultVal) {
51 if (s.empty()) {
52 return defaultVal;
53 } else {
54 return std::stol(s);
55 }
56}
57
58long long ParseUtils::tryParseLongLong(const std::string& s, long long defaultVal) {
59 if (s.empty()) {
60 return defaultVal;
61 } else {
62 return std::stoll(s);
63 }
64}
65
66void ParseUtils::splitStr(const std::string& line, const char& delimiter,
67 std::vector<std::string>& out) {
68 std::istringstream iss(line);
69 std::string item;
70 while (std::getline(iss, item, delimiter)) {
71 out.push_back(item);
72 }
73}
74
75bool ParseUtils::isValidHeader(const std::unordered_map<std::string, int>& columnNameIdMapping) {
76 std::vector<std::string> requiredHeaderColumns = {"Raw",
77 "utcTimeMillis",
78 "TimeNanos",
79 "LeapSecond",
80 "TimeUncertaintyNanos",
81 "FullBiasNanos",
82 "BiasNanos",
83 "BiasUncertaintyNanos",
84 "DriftNanosPerSecond",
85 "DriftUncertaintyNanosPerSecond",
86 "HardwareClockDiscontinuityCount",
87 "Svid",
88 "TimeOffsetNanos",
89 "State",
90 "ReceivedSvTimeNanos",
91 "ReceivedSvTimeUncertaintyNanos",
92 "Cn0DbHz",
93 "PseudorangeRateMetersPerSecond",
94 "PseudorangeRateUncertaintyMetersPerSecond",
95 "AccumulatedDeltaRangeState",
96 "AccumulatedDeltaRangeMeters",
97 "AccumulatedDeltaRangeUncertaintyMeters",
98 "CarrierFrequencyHz",
99 "CarrierCycles",
100 "CarrierPhase",
101 "CarrierPhaseUncertainty",
102 "MultipathIndicator",
103 "SnrInDb",
104 "ConstellationType",
105 "AgcDb",
106 "BasebandCn0DbHz",
107 "FullInterSignalBiasNanos",
108 "FullInterSignalBiasUncertaintyNanos",
109 "SatelliteInterSignalBiasNanos",
110 "SatelliteInterSignalBiasUncertaintyNanos",
111 "CodeType",
112 "ChipsetElapsedRealtimeNanos"};
113
114 for (const auto& columnName : requiredHeaderColumns) {
115 if (columnNameIdMapping.find(columnName) == columnNameIdMapping.end()) {
116 ALOGE("Missing column %s in header.", columnName.c_str());
117 return false;
118 }
119 }
120
121 return true;
122}
123
124} // namespace common
125} // namespace gnss
126} // namespace hardware
127} // namespace android