summaryrefslogtreecommitdiff
path: root/tools/preproc/c_file.cpp
diff options
context:
space:
mode:
authorPikalaxALT <pikalaxalt@gmail.com>2019-03-18 16:39:47 -0400
committerPikalaxALT <pikalaxalt@gmail.com>2019-03-18 16:39:47 -0400
commitf29b9d30ea74275a4c9955393059af6bdc2d07a7 (patch)
tree679ae4892a37d063203800e95e76d1e78a3f31d6 /tools/preproc/c_file.cpp
parent309d2b1246dee904dbde826f64b9fd4970f8e585 (diff)
preproc supports GNU-processed C pipe from stdin
Diffstat (limited to 'tools/preproc/c_file.cpp')
-rw-r--r--tools/preproc/c_file.cpp44
1 files changed, 31 insertions, 13 deletions
diff --git a/tools/preproc/c_file.cpp b/tools/preproc/c_file.cpp
index 229f568fa..3a026d0d2 100644
--- a/tools/preproc/c_file.cpp
+++ b/tools/preproc/c_file.cpp
@@ -22,6 +22,9 @@
#include <cstdarg>
#include <string>
#include <memory>
+#include <iostream>
+#include <iterator>
+#include <cstring>
#include "preproc.h"
#include "c_file.h"
#include "char_util.h"
@@ -30,28 +33,43 @@
CFile::CFile(std::string filename) : m_filename(filename)
{
- FILE *fp = std::fopen(filename.c_str(), "rb");
+ if (filename == "-") {
+ std::string s;
+
+ while (!std::cin.eof()) {
+ std::string b;
+ std::getline(std::cin, b);
+ b += "\n";
+ s += b;
+ }
+ m_size = s.size();
+ m_buffer = new char[m_size + 1];
+ memcpy(m_buffer, s.c_str(), m_size);
+ m_buffer[m_size] = 0;
+ } else {
+ FILE *fp = std::fopen(filename.c_str(), "rb");
- if (fp == NULL)
- FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename.c_str());
+ if (fp == NULL)
+ FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename.c_str());
- std::fseek(fp, 0, SEEK_END);
+ std::fseek(fp, 0, SEEK_END);
- m_size = std::ftell(fp);
+ m_size = std::ftell(fp);
- if (m_size < 0)
- FATAL_ERROR("File size of \"%s\" is less than zero.\n", filename.c_str());
+ if (m_size < 0)
+ FATAL_ERROR("File size of \"%s\" is less than zero.\n", filename.c_str());
- m_buffer = new char[m_size + 1];
+ m_buffer = new char[m_size + 1];
- std::rewind(fp);
+ std::rewind(fp);
- if (std::fread(m_buffer, m_size, 1, fp) != 1)
- FATAL_ERROR("Failed to read \"%s\".\n", filename.c_str());
+ if (std::fread(m_buffer, m_size, 1, fp) != 1)
+ FATAL_ERROR("Failed to read \"%s\".\n", filename.c_str());
- m_buffer[m_size] = 0;
+ m_buffer[m_size] = 0;
- std::fclose(fp);
+ std::fclose(fp);
+ }
m_pos = 0;
m_lineNum = 1;