Commit 5a12fecd authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add read_file helper method

parent 222f4458
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -237,7 +237,11 @@ ReturnCode load_csv(
    const std::string& csv_path,
    bool csv_headers,
    SeriesMap* data) {
  auto csv_data_str = FileUtil::read(csv_path).toString();
  std::string csv_data_str;
  if (auto rc = read_file(csv_path, &csv_data_str); !rc) {
    return rc;
  }

  auto csv_data = CSVData{};
  CSVParserConfig csv_opts;
  if (auto rc = parseCSV(csv_data_str, csv_opts, &csv_data); !rc) {
+8 −13
Original line number Diff line number Diff line
@@ -39,8 +39,6 @@
#include "elements/box.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>

using namespace plotfx;
@@ -99,18 +97,15 @@ int plotfx_configure(
int plotfx_configure_file(
    plotfx_t* ctx,
    const char* path) {
  std::stringstream config_buf;
  {
    std::ifstream config_file(path);
    if (!config_file.is_open()) {
      plotfx_seterrf(ctx, StringUtil::format("file not found: $0", path));
      return ERROR;
    }
  std::string config_path(path);
  std::string config_buf;

    config_buf << config_file.rdbuf();
  if (auto rc = read_file(config_path, &config_buf); !rc) {
    plotfx_seterr(ctx, rc);
    return rc;
  }

  return plotfx_configure(ctx, config_buf.str().c_str());
  return plotfx_configure(ctx, config_buf.c_str());
}

int plotfx_render_to(plotfx_t* ctx, void* backend) {
@@ -131,7 +126,7 @@ int plotfx_render_to(plotfx_t* ctx, void* backend) {
  });

  plotfx_seterr(ctx, rc);
  return OK;
  return rc;
}

int plotfx_render_file(plotfx_t* ctx, const char* path, const char* fmt) {
+20 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sstream>
#include <fstream>
#include "buffer.h"
#include "exception.h"
#include "stringutil.h"
@@ -223,5 +225,23 @@ size_t FileUtil::du_c(const std::string& path) {
  return size;
}

ReturnCode read_file(const std::string& path, std::string* data) {
  std::stringstream buffer;
  {
    std::ifstream file(path);
    if (!file.is_open()) {
      return ReturnCode::errorf(
          "EIO",
          "error while reading file: '$0': $1",
          path,
          strerror(errno));
    }

    buffer << file.rdbuf();
  }

  *data = buffer.str();
  return OK;
}

}
+3 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@
#define _plotfx_UTIL_FILEUTIL_H_
#include "buffer.h"
#include "stdtypes.h"
#include "return_code.h"

namespace plotfx {

@@ -111,5 +112,7 @@ public:

};

ReturnCode read_file(const std::string& path, std::string* data);

}
#endif