summaryrefslogtreecommitdiff
path: root/cpp/src/slice2cpp/Gen.cpp
diff options
context:
space:
mode:
authorMichi Henning <michi@zeroc.com>2002-07-05 00:22:17 +0000
committerMichi Henning <michi@zeroc.com>2002-07-05 00:22:17 +0000
commit451058a0f311fea3784812ad8b5759170de35afd (patch)
treeeb1367904c39bea9e5b18290255e6491581da196 /cpp/src/slice2cpp/Gen.cpp
parentFixes from Marc review. (diff)
downloadice-451058a0f311fea3784812ad8b5759170de35afd.tar.bz2
ice-451058a0f311fea3784812ad8b5759170de35afd.tar.xz
ice-451058a0f311fea3784812ad8b5759170de35afd.zip
Constant definitions for Slice are complete now. (Code generation for C++
only.) I couldn't avoid adding a few #ifdefs because of the different 64-bit integer APIs across Linux and Windows. (#including config.h wasn't an option because that would have created a circular dependency between Ice and Slice.) Added sufficient test cases to convince myself that things actually work as intended. Will compile under Windows now, so bear with me if things are broken under Windows for a few minutes... Some things that aren't quite right yet: - No support for universal character names (\uxxxx) - gcc appears to have a preprocessor bug. For example, preprocessing a file containing a single '@' character just echos that '@' character to the output. According to the C++ spec, '@' should be translated to its universal character name (because it isn't part of the basic C++ source character set). - Why is the Slice string type mapped to std::string? Given that we are supposed to support unicode for everything, shouldn't that be mapped to std::wstring? Once Windows compiles OK, I'll start on the Java code generation.
Diffstat (limited to 'cpp/src/slice2cpp/Gen.cpp')
-rw-r--r--cpp/src/slice2cpp/Gen.cpp52
1 files changed, 51 insertions, 1 deletions
diff --git a/cpp/src/slice2cpp/Gen.cpp b/cpp/src/slice2cpp/Gen.cpp
index 8c133248975..db6643bf6ba 100644
--- a/cpp/src/slice2cpp/Gen.cpp
+++ b/cpp/src/slice2cpp/Gen.cpp
@@ -932,7 +932,57 @@ void
Slice::Gen::TypesVisitor::visitConstDef(const ConstDefPtr& p)
{
H << sp;
- H << nl << "const " << inputTypeToString(p->type()) << " " << p->name() << " = " << p->value() << ";";
+ H << nl << "const " << typeToString(p->type()) << " " << p->name() << " = ";
+
+ BuiltinPtr bp = BuiltinPtr::dynamicCast(p->type());
+ if(bp && bp->kind() != Builtin::KindString)
+ {
+ H << p->value();
+ }
+ else
+ {
+ //
+ // Expand strings into the basic source character set. We can't use isalpha() and the like
+ // here because they are sensitive to the current locale.
+ //
+ static const string basicSourceChars = "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "0123456789"
+ "_{}[]#()<>%:;,?*+=/^&|~!=,\\\"' \t";
+ static const set<char> charSet(basicSourceChars.begin(), basicSourceChars.end());
+
+ H << "\""; // Opening "
+
+ ios_base::fmtflags originalFlags = H.flags(); // Save stream state
+ streamsize originalWidth = H.width();
+ ostream::char_type originalFill = H.fill();
+
+ const string val = p->value();
+ for(string::const_iterator c = val.begin(); c != val.end(); ++c)
+ {
+ if(charSet.find(*c) == charSet.end())
+ {
+ unsigned char uc = *c; // char may be signed, so make it positive
+ H << "\\"; // Print as octal if not in basic source character set
+ H.flags(ios_base::oct);
+ H.width(3);
+ H.fill('0');
+ H << static_cast<unsigned>(uc);
+ }
+ else
+ {
+ H << *c; // Print normally if in basic source character set
+ }
+ }
+
+ H.fill(originalFill); // Restore stream state
+ H.width(originalWidth);
+ H.flags(originalFlags);
+
+ H << "\""; // Closing "
+ }
+
+ H << ";";
}
void