diff options
Diffstat (limited to 'tools/preproc')
| -rw-r--r-- | tools/preproc/c_file.cpp | 42 | ||||
| -rw-r--r-- | tools/preproc/preproc.cpp | 25 | 
2 files changed, 44 insertions, 23 deletions
| diff --git a/tools/preproc/c_file.cpp b/tools/preproc/c_file.cpp index 229f568fa..b550a53fa 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,41 @@  CFile::CFile(std::string filename) : m_filename(filename)  { -    FILE *fp = std::fopen(filename.c_str(), "rb"); +    if (filename == "-") { +        std::string s, b; -    if (fp == NULL) -        FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename.c_str()); +        while (!std::cin.eof()) { +            std::getline(std::cin, b); +            s += b + "\n"; +        } +        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"); -    std::fseek(fp, 0, SEEK_END); +        if (fp == NULL) +            FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename.c_str()); -    m_size = std::ftell(fp); +        std::fseek(fp, 0, SEEK_END); -    if (m_size < 0) -        FATAL_ERROR("File size of \"%s\" is less than zero.\n", filename.c_str()); +        m_size = std::ftell(fp); -    m_buffer = new char[m_size + 1]; +        if (m_size < 0) +            FATAL_ERROR("File size of \"%s\" is less than zero.\n", filename.c_str()); -    std::rewind(fp); +        m_buffer = new char[m_size + 1]; -    if (std::fread(m_buffer, m_size, 1, fp) != 1) -        FATAL_ERROR("Failed to read \"%s\".\n", filename.c_str()); +        std::rewind(fp); -    m_buffer[m_size] = 0; +        if (std::fread(m_buffer, m_size, 1, fp) != 1) +            FATAL_ERROR("Failed to read \"%s\".\n", filename.c_str()); -    std::fclose(fp); +        m_buffer[m_size] = 0; + +        std::fclose(fp); +    }      m_pos = 0;      m_lineNum = 1; diff --git a/tools/preproc/preproc.cpp b/tools/preproc/preproc.cpp index c9c6042df..2d51ab175 100644 --- a/tools/preproc/preproc.cpp +++ b/tools/preproc/preproc.cpp @@ -140,17 +140,22 @@ int main(int argc, char **argv)      g_charmap = new Charmap(argv[2]); -    char* extension = GetFileExtension(argv[1]); - -    if (!extension) -        FATAL_ERROR("\"%s\" has no file extension.\n", argv[1]); +    if (argv[1][0] == '-' && argv[1][1] == 0) { +        PreprocCFile("-"); +    } else { +        char* extension = GetFileExtension(argv[1]); + +        if (!extension) +            FATAL_ERROR("\"%s\" has no file extension.\n", argv[1]); + +        if ((extension[0] == 's') && extension[1] == 0) +            PreprocAsmFile(argv[1]); +        else if ((extension[0] == 'c' || extension[0] == 'i') && extension[1] == 0) +            PreprocCFile(argv[1]); +        else +            FATAL_ERROR("\"%s\" has an unknown file extension of \"%s\".\n", argv[1], extension); +    } -    if ((extension[0] == 's') && extension[1] == 0) -        PreprocAsmFile(argv[1]); -    else if ((extension[0] == 'c' || extension[0] == 'i') && extension[1] == 0) -        PreprocCFile(argv[1]); -    else -        FATAL_ERROR("\"%s\" has an unknown file extension of \"%s\".\n", argv[1], extension);      return 0;  } | 
