/Scripts/Marshall/StringMarshaller.py
Python | 127 lines | 120 code | 0 blank | 7 comment | 5 complexity | 8a32564dc764c315ab91f359a39b4703 MD5 | raw file
Possible License(s): Apache-2.0
- """
- This source file is part of HGamer3D
- (A project to enable 3D game development in Haskell)
- For the latest info, see http://www.althainz.de/HGamer3D.html
-
- (c) 2011 Peter Althainz
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- """
-
- # StringMarshaller.py
-
- import os, re, sys
- import Config, BasicMarshaller
-
- class StringMarshaller(BasicMarshaller.BasicMarshaller):
- """
- marshalling of Simple c/Haskell types
-
- Transformation Table for "string" Types:
- ----------------------------------------
-
- ----------------+--------+-----------+------------------------------------------------------+-------+------------------------------------------+
- cpp-type + In/Out + x_ctype + CtoCpp + vcpm + CpptoC +
- ----------------+--------+-----------+------------------------------------------------------+-------+------------------------------------------+
- base * | In | ctype* | base *name_cpp = (base *) in_name_c; | - | - |
- const base * | In | ctype* | const base *name_cpp = (const base *) in_name_c; | - | - |
- ----------------+--------+-----------+------------------------------------------------------+-------+------------------------------------------+
-
- x_type is in/out c-type
- """
-
- def getPossibleModifications(self):
- """string types currently support only in and const out for overwriteOut"""
- return {
- # const, ref, pointer, doublepointer
- "in" : (False, False, True, False), # input, simple pointer
- "const" : (True, False, True, False), # input/output const pointer
- }
-
- #
- # here are coming the marshalling routines
- #
-
-
- def vcpm(self):
- """
- modifier before the variable name, for the invocation of the function,
- depends on the difference between the parameter type and the variable
- type.
- """
- return ""
-
-
- def ctype(self, overwriteOut):
- """
- type for C-function declaration,
- overwriteIsOut: the parameter is an out parameter, independent
- of the mod function (its a function return value)
- """
- return self.typeConfig.getProp("ctype") + "*"
-
-
- def fromCtoCpp(self, cname, cppname, overwriteOut):
- """
- returns lines of code, to create a cpp variable, transform the
- input C value to this type. If it is a out variable, transformation
- of input variable not needed, but creation of cpp variable still needed.
- overwriteOut: if True, this is a out variable, independent of self.mod
- """
- if overwriteOut and self.mod == "in":
- print "overwriteOut for string type:", self.cpptype()
- sys.exit(-1)
-
- if overwriteOut and self.mod == "const":
- return "const " + self.baseType + " *" + cppname + ";\n"
- else:
- return self.baseType + " *" + cppname + " = (" + self.baseType + "*) " + cname + ";\n"
-
-
- def fromCpptoC(self, cname, cppname, overwriteOut):
- """
- returns lines of code, to transform the cpp value to the output
- c value, only needed in case of out variable.
- overwriteOut: if True, this is a out variable, independent of self.mod
- """
- if overwriteOut and self.mod == "in":
- print "overwriteOut for string type", self.cpptype()
- sys.exit(-1)
-
- if overwriteOut and self.mod == "const":
- if (self.baseType == "wchar_t"):
- outtext = "if (wcslen( (" + self.baseType + "*) " + cppname + ") < (512 * 64 - 2)) { \n"
- outtext = outtext + "wcscpy(" + cname + ", (" + self.baseType + "*) " + cppname + "); } else {\n"
- outtext = outtext + "wcscpy(" + cname + ", L\"error: outstring larger then 64k\");};\n"
- return outtext
- else:
- outtext = "if (strlen( (" + self.baseType + "*) " + cppname + ") < (1024 * 64 - 1)) { \n"
- outtext = outtext + "strcpy(" + cname + ", (" + self.baseType + "*) " + cppname + "); } else {\n"
- outtext = outtext + "strcpy(" + cname + ", \"error: outstring larger then 64k\");};\n"
- return outtext
- else:
- return ""
-
- def chsFuncDefinition(self, overwriteOut):
- """provides the marshalling string within the C2HS func definition"""
- if overwriteOut and self.mod == "in":
- print "overwriteOut for string type", self.cpptype()
- sys.exit(-1)
-
- conf = self.typeConfig
- if overwriteOut and self.mod == "const":
- return "alloc64k- `" + conf.getProp("htype") + "' " + conf.getProp("outmarsh") + "*"
- else:
- return conf.getProp("inmarsh") + "* `" + conf.getProp("htype") + "' "
-
-