diff options
author | Benoit Foucher <benoit@zeroc.com> | 2002-10-29 17:36:01 +0000 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2002-10-29 17:36:01 +0000 |
commit | 6c8b1db065f4cb87d490e047a89e2cbad8a6d152 (patch) | |
tree | df2bebe0fbadb2eb3b03cc6fe61242f2da30f111 /cpp/src/Slice/Preprocessor.cpp | |
parent | removed AbortException from IcePatch. Fixed exception cleanup code in (diff) | |
download | ice-6c8b1db065f4cb87d490e047a89e2cbad8a6d152.tar.bz2 ice-6c8b1db065f4cb87d490e047a89e2cbad8a6d152.tar.xz ice-6c8b1db065f4cb87d490e047a89e2cbad8a6d152.zip |
Fixed the translators to use the icecpp C preprocessor.
Diffstat (limited to 'cpp/src/Slice/Preprocessor.cpp')
-rw-r--r-- | cpp/src/Slice/Preprocessor.cpp | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/cpp/src/Slice/Preprocessor.cpp b/cpp/src/Slice/Preprocessor.cpp new file mode 100644 index 00000000000..532ed8be9d6 --- /dev/null +++ b/cpp/src/Slice/Preprocessor.cpp @@ -0,0 +1,193 @@ +// ********************************************************************** +// +// Copyright (c) 2001 +// ZeroC, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <Slice/Preprocessor.h> + +#include <algorithm> +#include <fstream> + +#include <sys/types.h> +#include <sys/stat.h> + +#ifndef _WIN32 +# include <sys/wait.h> +#endif + +using namespace std; +using namespace Slice; + +Slice::Preprocessor::Preprocessor(const string& path, const string& fileName, const string& args) : + _path(path), + _fileName(fileName), + _args(args), + _cppHandle(0) +{ +} + +Slice::Preprocessor::~Preprocessor() +{ + if(_cppHandle) + { + close(); + } +} + +string +Slice::Preprocessor::getBaseName() +{ + string base(_fileName); + string suffix; + string::size_type pos = base.rfind('.'); + if(pos != string::npos) + { + base.erase(pos); + } + return base; +} + +FILE* +Slice::Preprocessor::preprocess(bool keepComments) +{ + if(!checkInputFile()) + { + return 0; + } + + string cmd = searchIceCpp(); + + if(cmd.empty()) + { + return 0; + } + + if(keepComments) + { + cmd += " -C"; + } + + cmd += " " + _args + " " + _fileName; + + // + // Open a pipe for reading to redirect icecpp output to _cppHandle. + // +#ifdef _WIN32 + _cppHandle = _popen(cmd.c_str(), "r"); +#else + _cppHandle = popen(cmd.c_str(), "r"); +#endif + return _cppHandle; +} + +void +Slice::Preprocessor::printMakefileDependencies() +{ + if(!checkInputFile()) + { + return; + } + + string cmd = searchIceCpp(); + + if(cmd.empty()) + { + return; + } + + cmd += " -M " + _args + " " +_fileName; + + // + // Open a pipe for writing to redirect icecpp output to the standard output. + // +#ifndef _WIN32 + FILE* cppHandle = popen(cmd.c_str(), "w"); + pclose(cppHandle); +#else + FILE* cppHandle = _popen(cmd.c_str(), "w"); + _pclose(cppHandle); +#endif +} + +bool +Slice::Preprocessor::close() +{ + assert(_cppHandle); + +#ifndef WIN32 + int status = pclose(_cppHandle); + _cppHandle = 0; + + if(WIFEXITED(status) && WEXITSTATUS(status) != 0) + { + return false; + } +#else + int status = _pclose(_cppHandle); + _cppHandle = 0; + + if(status != 0) + { + return false; + } +#endif + + return true; +} + +bool +Slice::Preprocessor::checkInputFile() +{ + string base(_fileName); + string suffix; + string::size_type pos = base.rfind('.'); + if(pos != string::npos) + { + suffix = base.substr(pos); + transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower); + } + if(suffix != ".ice") + { + cerr << _path << ": input files must end with `.ice'" << endl; + return false; + } + + ifstream test(_fileName.c_str()); + if(!test) + { + cerr << _path << ": can't open `" << _fileName << "' for reading: " << strerror(errno) << endl; + return false; + } + test.close(); + + return true; +} + +string +Slice::Preprocessor::searchIceCpp() +{ + const char* icecpp = "icecpp"; + + string::size_type pos = _path.find_last_of("/\\"); + if(pos != string::npos) + { + string path = _path.substr(0, pos + 1); + path += icecpp; + + struct stat st; + if(stat(path.c_str(), &st) == 0) + { + if(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) + { + return path; + } + } + } + + return icecpp; +} |