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

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

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

Loading…
Cancel
Save