PageRenderTime 67ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/Scripts/Marshall/StringMarshaller.py

https://code.google.com/p/hgamer3d/
Python | 127 lines | 120 code | 0 blank | 7 comment | 5 complexity | 8a32564dc764c315ab91f359a39b4703 MD5 | raw file
Possible License(s): Apache-2.0
  1. """
  2. This source file is part of HGamer3D
  3. (A project to enable 3D game development in Haskell)
  4. For the latest info, see http://www.althainz.de/HGamer3D.html
  5. (c) 2011 Peter Althainz
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. """
  16. # StringMarshaller.py
  17. import os, re, sys
  18. import Config, BasicMarshaller
  19. class StringMarshaller(BasicMarshaller.BasicMarshaller):
  20. """
  21. marshalling of Simple c/Haskell types
  22. Transformation Table for "string" Types:
  23. ----------------------------------------
  24. ----------------+--------+-----------+------------------------------------------------------+-------+------------------------------------------+
  25. cpp-type + In/Out + x_ctype + CtoCpp + vcpm + CpptoC +
  26. ----------------+--------+-----------+------------------------------------------------------+-------+------------------------------------------+
  27. base * | In | ctype* | base *name_cpp = (base *) in_name_c; | - | - |
  28. const base * | In | ctype* | const base *name_cpp = (const base *) in_name_c; | - | - |
  29. ----------------+--------+-----------+------------------------------------------------------+-------+------------------------------------------+
  30. x_type is in/out c-type
  31. """
  32. def getPossibleModifications(self):
  33. """string types currently support only in and const out for overwriteOut"""
  34. return {
  35. # const, ref, pointer, doublepointer
  36. "in" : (False, False, True, False), # input, simple pointer
  37. "const" : (True, False, True, False), # input/output const pointer
  38. }
  39. #
  40. # here are coming the marshalling routines
  41. #
  42. def vcpm(self):
  43. """
  44. modifier before the variable name, for the invocation of the function,
  45. depends on the difference between the parameter type and the variable
  46. type.
  47. """
  48. return ""
  49. def ctype(self, overwriteOut):
  50. """
  51. type for C-function declaration,
  52. overwriteIsOut: the parameter is an out parameter, independent
  53. of the mod function (its a function return value)
  54. """
  55. return self.typeConfig.getProp("ctype") + "*"
  56. def fromCtoCpp(self, cname, cppname, overwriteOut):
  57. """
  58. returns lines of code, to create a cpp variable, transform the
  59. input C value to this type. If it is a out variable, transformation
  60. of input variable not needed, but creation of cpp variable still needed.
  61. overwriteOut: if True, this is a out variable, independent of self.mod
  62. """
  63. if overwriteOut and self.mod == "in":
  64. print "overwriteOut for string type:", self.cpptype()
  65. sys.exit(-1)
  66. if overwriteOut and self.mod == "const":
  67. return "const " + self.baseType + " *" + cppname + ";\n"
  68. else:
  69. return self.baseType + " *" + cppname + " = (" + self.baseType + "*) " + cname + ";\n"
  70. def fromCpptoC(self, cname, cppname, overwriteOut):
  71. """
  72. returns lines of code, to transform the cpp value to the output
  73. c value, only needed in case of out variable.
  74. overwriteOut: if True, this is a out variable, independent of self.mod
  75. """
  76. if overwriteOut and self.mod == "in":
  77. print "overwriteOut for string type", self.cpptype()
  78. sys.exit(-1)
  79. if overwriteOut and self.mod == "const":
  80. if (self.baseType == "wchar_t"):
  81. outtext = "if (wcslen( (" + self.baseType + "*) " + cppname + ") < (512 * 64 - 2)) { \n"
  82. outtext = outtext + "wcscpy(" + cname + ", (" + self.baseType + "*) " + cppname + "); } else {\n"
  83. outtext = outtext + "wcscpy(" + cname + ", L\"error: outstring larger then 64k\");};\n"
  84. return outtext
  85. else:
  86. outtext = "if (strlen( (" + self.baseType + "*) " + cppname + ") < (1024 * 64 - 1)) { \n"
  87. outtext = outtext + "strcpy(" + cname + ", (" + self.baseType + "*) " + cppname + "); } else {\n"
  88. outtext = outtext + "strcpy(" + cname + ", \"error: outstring larger then 64k\");};\n"
  89. return outtext
  90. else:
  91. return ""
  92. def chsFuncDefinition(self, overwriteOut):
  93. """provides the marshalling string within the C2HS func definition"""
  94. if overwriteOut and self.mod == "in":
  95. print "overwriteOut for string type", self.cpptype()
  96. sys.exit(-1)
  97. conf = self.typeConfig
  98. if overwriteOut and self.mod == "const":
  99. return "alloc64k- `" + conf.getProp("htype") + "' " + conf.getProp("outmarsh") + "*"
  100. else:
  101. return conf.getProp("inmarsh") + "* `" + conf.getProp("htype") + "' "