From e0ab85c1c4fc1ffe285869ae160fd3a36d0f27b2 Mon Sep 17 00:00:00 2001 From: Wolfgang Thaller Date: Wed, 8 Oct 2014 01:17:39 +0200 Subject: [PATCH] Rez: strings --- Rez/Expression.cc | 10 ++++++ Rez/Expression.h | 2 ++ Rez/ResourceDefinitions.cc | 74 ++++++++++++++++++++++++++++++++------ Rez/ResourceDefinitions.h | 4 +++ 4 files changed, 80 insertions(+), 10 deletions(-) diff --git a/Rez/Expression.cc b/Rez/Expression.cc index 4c98dd4384..e93bec695a 100644 --- a/Rez/Expression.cc +++ b/Rez/Expression.cc @@ -8,6 +8,11 @@ int Expression::evaluateInt(ResourceCompiler *ctx) throw TypeError(); } +std::string Expression::evaluateString(ResourceCompiler *ctx) +{ + throw TypeError(); +} + Expression::~Expression() { } @@ -17,6 +22,11 @@ StringExpr::~StringExpr() { } +std::string StringExpr::evaluateString(ResourceCompiler *ctx) +{ + return str; +} + IntExpr::~IntExpr() { diff --git a/Rez/Expression.h b/Rez/Expression.h index 140f2ea0d9..9b4ec6b444 100644 --- a/Rez/Expression.h +++ b/Rez/Expression.h @@ -34,6 +34,7 @@ class Expression { public: virtual int evaluateInt(ResourceCompiler *ctx); + virtual std::string evaluateString(ResourceCompiler *ctx); virtual ~Expression(); }; @@ -43,6 +44,7 @@ class StringExpr : public Expression public: StringExpr(const std::string& str) : str(str) {} ~StringExpr(); + virtual std::string evaluateString(ResourceCompiler *ctx); }; class IntExpr : public Expression diff --git a/Rez/ResourceDefinitions.cc b/Rez/ResourceDefinitions.cc index fea09f43c2..46c4233cc0 100644 --- a/Rez/ResourceDefinitions.cc +++ b/Rez/ResourceDefinitions.cc @@ -88,7 +88,69 @@ bool SimpleField::needsValue() void SimpleField::compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass) { - int bitSize; + switch(type) + { + case Type::bitstring: + case Type::boolean: + case Type::byte: + case Type::integer: + case Type::longint: + compileInt(expr, compiler, prePass); + break; + case Type::string: + case Type::wstring: + case Type::pstring: + case Type::char_: + compileString(expr, compiler, prePass); + break; + } +} + +void SimpleField::compileString(ExprPtr expr, ResourceCompiler *compiler, bool prePass) +{ + std::string str; + { + ResourceCompiler::FieldScope scope(compiler, this); + str = (value ? value : expr)->evaluateString(compiler); + } + + if(arrayCount || type == Type::char_) + { + int requestedSize = type == Type::char_ ? 1 : arrayCount->evaluateInt(compiler); + if(requestedSize < str.size()) + str.erase(str.begin() + requestedSize, str.end()); + else if(requestedSize > str.size()) + str.insert(str.end(),requestedSize - str.size(), '\0'); + } + + int count = str.size(); + + if(type == Type::pstring) + { + if(count > 255) + { + str.erase(str.begin() + 255, str.end()); + count = 255; + } + compiler->write(8, count); + } + else if(type == Type::wstring) + { + if(count > 65535) + { + str.erase(str.begin() + 65535, str.end()); + count = 65535; + } + compiler->write(16, count); + } + + for(char c : str) + compiler->write(8, c); +} + +void SimpleField::compileInt(ExprPtr expr, ResourceCompiler *compiler, bool prePass) +{ + int bitSize = 0; switch(type) { @@ -113,15 +175,7 @@ void SimpleField::compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass if(!prePass) { ResourceCompiler::FieldScope scope(compiler, this); - if(value) - { - actualValue = value->evaluateInt(compiler); - } - else - { - // TODO: add alternatives to context - actualValue = expr->evaluateInt(compiler); - } + actualValue = (value ? value : expr)->evaluateInt(compiler); } compiler->write(bitSize, actualValue); diff --git a/Rez/ResourceDefinitions.h b/Rez/ResourceDefinitions.h index b7ab67c450..fc52cd11b4 100644 --- a/Rez/ResourceDefinitions.h +++ b/Rez/ResourceDefinitions.h @@ -90,6 +90,10 @@ public: virtual bool needsValue(); virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass); + +private: + void compileString(ExprPtr expr, ResourceCompiler *compiler, bool prePass); + void compileInt(ExprPtr expr, ResourceCompiler *compiler, bool prePass); }; typedef std::shared_ptr SimpleFieldPtr;