use of fmt lib in asn1 utils

master
Francisco Paisana 4 years ago
parent 9133135e12
commit 1304746bce

@ -13,14 +13,13 @@
#ifndef SRSASN_COMMON_UTILS_H #ifndef SRSASN_COMMON_UTILS_H
#define SRSASN_COMMON_UTILS_H #define SRSASN_COMMON_UTILS_H
#include "srslte/srslog/bundled/fmt/format.h"
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <cmath> #include <cmath>
#include <cstring> #include <cstring>
#include <limits> #include <limits>
#include <map> #include <map>
#include <sstream>
#include <stdarg.h> /* va_list, va_start, va_arg, va_end */
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <vector> #include <vector>
@ -1301,6 +1300,8 @@ private:
JsonWriter JsonWriter
*******************/ *******************/
using json_buffer = fmt::basic_memory_buffer<char, 2048>;
class json_writer class json_writer
{ {
public: public:
@ -1321,9 +1322,9 @@ public:
std::string to_string() const; std::string to_string() const;
private: private:
std::stringstream ss; json_buffer buffer;
std::string ident; std::string ident;
enum separator_t { COMMA, NEWLINE, NONE }; enum separator_t { COMMA = 0, NEWLINE, NONE };
separator_t sep; separator_t sep;
}; };

@ -12,7 +12,9 @@
#include "srslte/asn1/asn1_utils.h" #include "srslte/asn1/asn1_utils.h"
#include "srslte/common/logmap.h" #include "srslte/common/logmap.h"
#include "srslte/srslog/bundled/fmt/core.h"
#include <cmath> #include <cmath>
#include <stdarg.h> /* va_list, va_start, va_arg, va_end */
#include <stdio.h> #include <stdio.h>
namespace asn1 { namespace asn1 {
@ -1513,13 +1515,11 @@ json_writer::json_writer() : ident(""), sep(NONE) {}
void json_writer::write_fieldname(const std::string& fieldname) void json_writer::write_fieldname(const std::string& fieldname)
{ {
if (sep == COMMA) { constexpr static const char* septable[] = {",\n", "\n", ""};
ss << ",\n" << ident;
} else if (sep == NEWLINE) { fmt::format_to(buffer, "{}{}", septable[sep], sep != NONE ? ident : "");
ss << "\n" << ident;
}
if (not fieldname.empty()) { if (not fieldname.empty()) {
ss << "\"" << fieldname << "\": "; fmt::format_to(buffer, "\"{}\": ", fieldname);
} }
sep = NONE; sep = NONE;
} }
@ -1527,7 +1527,7 @@ void json_writer::write_fieldname(const std::string& fieldname)
void json_writer::write_str(const std::string& fieldname, const std::string& value) void json_writer::write_str(const std::string& fieldname, const std::string& value)
{ {
write_fieldname(fieldname); write_fieldname(fieldname);
ss << "\"" << value << "\""; fmt::format_to(buffer, "\"{}\"", value);
sep = COMMA; sep = COMMA;
} }
void json_writer::write_str(const std::string& value) void json_writer::write_str(const std::string& value)
@ -1538,7 +1538,7 @@ void json_writer::write_str(const std::string& value)
void json_writer::write_int(const std::string& fieldname, int64_t value) void json_writer::write_int(const std::string& fieldname, int64_t value)
{ {
write_fieldname(fieldname); write_fieldname(fieldname);
ss << value; fmt::format_to(buffer, "{}", value);
sep = COMMA; sep = COMMA;
} }
void json_writer::write_int(int64_t value) void json_writer::write_int(int64_t value)
@ -1549,7 +1549,7 @@ void json_writer::write_int(int64_t value)
void json_writer::write_bool(const std::string& fieldname, bool value) void json_writer::write_bool(const std::string& fieldname, bool value)
{ {
write_fieldname(fieldname); write_fieldname(fieldname);
ss << (value ? "true" : "false"); fmt::format_to(buffer, "{}", value ? "true" : "false");
sep = COMMA; sep = COMMA;
} }
void json_writer::write_bool(bool value) void json_writer::write_bool(bool value)
@ -1560,7 +1560,7 @@ void json_writer::write_bool(bool value)
void json_writer::write_null(const std::string& fieldname) void json_writer::write_null(const std::string& fieldname)
{ {
write_fieldname(fieldname); write_fieldname(fieldname);
ss << "null"; fmt::format_to(buffer, "null");
sep = COMMA; sep = COMMA;
} }
void json_writer::write_null() void json_writer::write_null()
@ -1571,33 +1571,34 @@ void json_writer::write_null()
void json_writer::start_obj(const std::string& fieldname) void json_writer::start_obj(const std::string& fieldname)
{ {
write_fieldname(fieldname); write_fieldname(fieldname);
ss << "{"; fmt::format_to(buffer, "{{");
ident += " "; ident += " ";
sep = NEWLINE; sep = NEWLINE;
} }
void json_writer::end_obj() void json_writer::end_obj()
{ {
ident.erase(ident.size() - 2, 2); ident.erase(ident.size() - 2, 2);
ss << "\n" << ident << "}"; fmt::format_to(buffer, "\n{}}}", ident);
sep = COMMA; sep = COMMA;
} }
void json_writer::start_array(const std::string& fieldname) void json_writer::start_array(const std::string& fieldname)
{ {
write_fieldname(fieldname); write_fieldname(fieldname);
ss << "["; fmt::format_to(buffer, "[");
ident += " "; ident += " ";
sep = NEWLINE; sep = NEWLINE;
} }
void json_writer::end_array() void json_writer::end_array()
{ {
ident.erase(ident.size() - 2, 2); ident.erase(ident.size() - 2, 2);
ss << "\n" << ident << "]"; fmt::format_to(buffer, "\n{}]", ident);
sep = COMMA; sep = COMMA;
} }
std::string json_writer::to_string() const std::string json_writer::to_string() const
{ {
return ss.str(); return std::string(buffer.data(), buffer.size());
} }
} // namespace asn1 } // namespace asn1

@ -19,6 +19,7 @@
#include <iostream> #include <iostream>
#include <libconfig.h++> #include <libconfig.h++>
#include <list> #include <list>
#include <sstream>
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>

Loading…
Cancel
Save