You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by th...@apache.org on 2012/05/16 10:27:45 UTC

svn commit: r1339055 - in /avro/trunk: CHANGES.txt lang/c++/impl/avrogencpp.cc

Author: thiru
Date: Wed May 16 08:27:45 2012
New Revision: 1339055

URL: http://svn.apache.org/viewvc?rev=1339055&view=rev
Log:
AVRO-1079. C++ Generator, improve include guard generation

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/c++/impl/avrogencpp.cc

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1339055&r1=1339054&r2=1339055&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed May 16 08:27:45 2012
@@ -29,6 +29,8 @@ Avro 1.7.0 (unreleased)
 
     AVRO-1085. Fingerprinting for C#. (Eric Hauser via thiru)
 
+    AVRO-1079. C++ Generator, improve include guard generation. (thiru)
+
   BUG FIXES
 
     AVRO-1045. Java: Fix a bug in GenericData#deepCopy() of ByteBuffer values.

Modified: avro/trunk/lang/c++/impl/avrogencpp.cc
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/impl/avrogencpp.cc?rev=1339055&r1=1339054&r2=1339055&view=diff
==============================================================================
--- avro/trunk/lang/c++/impl/avrogencpp.cc (original)
+++ avro/trunk/lang/c++/impl/avrogencpp.cc Wed May 16 08:27:45 2012
@@ -25,6 +25,7 @@
 #include <map>
 #include <set>
 
+#include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/program_options.hpp>
 
@@ -79,6 +80,7 @@ class CodeGen {
     const std::string headerFile_;
     const std::string includePrefix_;
     const bool noUnion_;
+    const std::string guardString_;
     boost::mt19937 random_;
 
     vector<PendingSetterGetter> pendingGettersAndSetters;
@@ -105,10 +107,12 @@ class CodeGen {
 public:
     CodeGen(std::ostream& os, const std::string& ns,
         const std::string& schemaFile, const std::string& headerFile,
+        const std::string& guardString,
         const std::string& includePrefix, bool noUnion) :
         unionNumber_(0), os_(os), inNamespace_(false), ns_(ns),
         schemaFile_(schemaFile), headerFile_(headerFile),
         includePrefix_(includePrefix), noUnion_(noUnion),
+        guardString_(guardString),
         random_(static_cast<uint32_t>(::time(0))) { }
     void generate(const ValidSchema& schema);
 };
@@ -602,7 +606,7 @@ void CodeGen::generate(const ValidSchema
 {
     emitCopyright();
 
-    string h = guard();
+    string h = guardString_.empty() ? guard() : guardString_;
 
     os_ << "#ifndef " << h << "\n";
     os_ << "#define " << h << "\n\n\n";
@@ -661,6 +665,28 @@ static const string IN("input");
 static const string INCLUDE_PREFIX("include-prefix");
 static const string NO_UNION_TYPEDEF("no-union-typedef");
 
+static string readGuard(const string& filename)
+{
+    std::ifstream ifs(filename.c_str());
+    string buf;
+    string candidate;
+    while (std::getline(ifs, buf)) {
+        boost::algorithm::trim(buf);
+        if (candidate.empty()) {
+            if (boost::algorithm::starts_with(buf, "#ifndef ")) {
+                candidate = buf.substr(8);
+            }
+        } else if (boost::algorithm::starts_with(buf, "#define ")) {
+            if (candidate == buf.substr(8)) {
+                break;
+            }
+        } else {
+            candidate.erase();
+        }
+    }
+    return candidate;
+}
+
 int main(int argc, char** argv)
 {
     po::options_description desc("Allowed options");
@@ -705,10 +731,11 @@ int main(int argc, char** argv)
         }
 
         if (! outf.empty()) {
+            string g = readGuard(outf);
             ofstream out(outf.c_str());
-            CodeGen(out, ns, inf, outf, incPrefix, noUnion).generate(schema);
+            CodeGen(out, ns, inf, outf, g, incPrefix, noUnion).generate(schema);
         } else {
-            CodeGen(std::cout, ns, inf, outf, incPrefix, noUnion).
+            CodeGen(std::cout, ns, inf, outf, "", incPrefix, noUnion).
                 generate(schema);
         }
         return 0;