Skip to content

Instantly share code, notes, and snippets.

@KageKirin
Last active August 20, 2020 13:52
Show Gist options
  • Select an option

  • Save KageKirin/1c2ad5738ea84dbc3b247744fc36528e to your computer and use it in GitHub Desktop.

Select an option

Save KageKirin/1c2ad5738ea84dbc3b247744fc36528e to your computer and use it in GitHub Desktop.
Recomment C++ to C89
//#!`/usr/bin/env clang++` --std=c++03 -I/usr/local/Cellar/boost/1.73.0/include -L/usr/local/Cellar/boost/1.73.0/lib -lboost_wave -orecomment
#include <fstream>
#include <iostream>
#include <sstream>
#include <cassert>
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
typedef boost::wave::cpplexer::lex_token<> token_type;
typedef boost::wave::cpplexer::lex_iterator<token_type> token_iterator;
typedef token_type::position_type position_type;
int main(int argc, char** argv)
{
assert(argc >= 2);
const char *infile = argv[1];
std::string instr;
std::stringstream outstrm;
std::string cmt_str;
std::ifstream instream(infile);
if (!instream.is_open())
{
std::cerr << "Could not open file: " << infile << "\n";
}
instream.unsetf(std::ios::skipws);
instr = std::string(std::istreambuf_iterator<char>(instream.rdbuf()), std::istreambuf_iterator<char>());
position_type pos(infile);
token_iterator it =
token_iterator(instr.begin(), instr.end(), pos,
boost::wave::language_support(boost::wave::support_cpp | boost::wave::support_c99 | boost::wave::support_option_long_long));
token_iterator end = token_iterator();
boost::wave::token_id id = *it;
while (it != end)
{
// here you check the c++ style comments
if (id == boost::wave::T_CPPCOMMENT)
{
std::cout << "Found CPP COMMENT" << std::endl;
cmt_str = it->get_value().c_str();
cmt_str[0] = '/';
cmt_str[1] = '*';
// since the last token is the new_line token so replace the new line
cmt_str[cmt_str.size() - 1] = '*';
cmt_str.push_back('/');
// and then append the newline at the end of the string
cmt_str.push_back('\n');
outstrm << cmt_str;
}
else
{
outstrm << it->get_value();
}
++it;
id = *it;
}
instream.close();
const char *outfile = argc >= 3 ? argv[2] : argv[1];
std::ofstream outstream(outfile);
if (!outstream.is_open())
{
std::cerr << "Could not open file: " << outfile << "\n";
}
outstream << outstrm.str();
return 0;
}

Recomment C++ to C89

This program follows the spirit of recomment to change C++ comments //... into C89 comments /*...*/.

Disclaimer

I did not invent it, but just adapted it from this SO answer.

Build

Recomment requires Boost::Wave, thus Boost installed somewhere.

Build command line:

clang++ --std=c++03 -I/usr/local/Cellar/boost/1.73.0/include -L/usr/local/Cellar/boost/1.73.0/lib -lboost_wave -orecomment recomment.cpp
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment