Skip to content

Instantly share code, notes, and snippets.

@KageKirin
Last active August 20, 2020 15:22
Show Gist options
  • Save KageKirin/486bd009397fe4b4efd2defc7a286292 to your computer and use it in GitHub Desktop.
Save KageKirin/486bd009397fe4b4efd2defc7a286292 to your computer and use it in GitHub Desktop.
Recomment C89 to C99
//#!`/usr/bin/env clang++` --std=c++17 -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 <regex>
#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::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_CCOMMENT)
{
std::cout << "Found C COMMENT" << std::endl;
std::cout << it->get_value() << std::endl;
std::string cmt_str = it->get_value().c_str();
std::string result;
// replace '/*' with '///'
std::regex_replace (std::back_inserter(result), cmt_str.begin(), cmt_str.end(), std::regex("/\\*"), "///");
cmt_str = result;
// cull terminal '*/'
result.clear();
std::regex_replace (std::back_inserter(result), cmt_str.begin(), cmt_str.end(), std::regex("\\*/"), "");
cmt_str = result;
// replace '....*' with '....'
result.clear();
std::regex_replace (std::back_inserter(result), cmt_str.begin(), cmt_str.end(), std::regex("\n(\\s*)\\*"), "\n$1");
cmt_str = result;
// replace '\n' with '\n///'
result.clear();
std::regex_replace (std::back_inserter(result), cmt_str.begin(), cmt_str.end(), std::regex("\n"), "\n///");
cmt_str = result;
// trim trailing spaces
result.clear();
std::regex_replace (std::back_inserter(result), cmt_str.begin(), cmt_str.end(), std::regex("(\\s*)\n"), "\n");
cmt_str = result;
std::cout << result << std::endl;
outstrm << result;
}
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 C89 to C99

This is a fork of the Recomment C++ to C89 gist, which does the inverse: change comments from C89 /*...*/ to C99/C++ //.

In fact, it does even write /// which is my preferred comment style.

Build

Recomment requires Boost::Wave, thus Boost installed somewhere, and C++17 (for Regex).

Build command line:

clang++ --std=c++17 -I/usr/local/Cellar/boost/1.73.0/include -L/usr/local/Cellar/boost/1.73.0/lib -lboost_wave -orecomment recomment2.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