home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD112271122000.psc / basTranslatePreprocessor.bas < prev    next >
Encoding:
BASIC Source File  |  2000-10-26  |  4.0 KB  |  120 lines

  1. Attribute VB_Name = "basTranslatePreprocessor"
  2. '    C2VB  Converts C style definitions to VB
  3. '    Copyright (C) 2000  Kimon Andreou (kimon@mindless.com)
  4. '
  5. '    This program is free software; you can redistribute it and/or modify
  6. '    it under the terms of the GNU General Public License as published by
  7. '    the Free Software Foundation; either version 2 of the License, or
  8. '    (at your option) any later version.
  9. '
  10. '    This program is distributed in the hope that it will be useful,
  11. '    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. '    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. '    GNU General Public License for more details.
  14. '
  15. '    You should have received a copy of the GNU General Public License
  16. '    along with this program; if not, write to the Free Software
  17. '    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.  
  20. Option Explicit
  21. Option Base 0
  22.  
  23. 'Translate a preprocessor instruction
  24. Public Function ProcessPreProcessor(Cstring As String) As String
  25. Dim parts() As String
  26. Dim dummy As String
  27.  
  28. 'Split into tokens
  29. parts = GetToken(Trim(Cstring), 0, dummy)
  30.  
  31. 'Find out what kind of preprocessor instruction we're dealing with
  32. Select Case parts(0)
  33.     Case "#ifdef":
  34.         parts(0) = "#If"
  35.         ProcessPreProcessor = Join(parts, " ") & " Then"
  36.         
  37.     Case "#endif":
  38.         parts(0) = "#End If"
  39.         ProcessPreProcessor = Join(parts, " ") & vbCrLf
  40.         
  41.     Case "#else":
  42.         parts(0) = "#Else"
  43.         ProcessPreProcessor = Join(parts, " ")
  44.         
  45.     Case "#elseif":
  46.         parts(0) = "#ElseIf"
  47.         ProcessPreProcessor = Join(parts, " ") & " Then"
  48.         
  49.     Case "#ifndef":
  50.         parts(0) = "#If Not"
  51.         ProcessPreProcessor = Join(parts, " ") & " Then"
  52.         
  53.     Case "#define":
  54.         ProcessPreProcessor = ProcessDefine(Trim(Cstring)) & vbCrLf
  55.         
  56.     Case Else:
  57.         ProcessPreProcessor = vbCrLf & vbCrLf & "#If False Then" & vbCrLf & _
  58.             "' Cannot handle the following" & vbCrLf
  59.         ProcessPreProcessor = ProcessPreProcessor & Trim(Cstring) & vbCrLf & _
  60.             "#End If" & vbCrLf
  61. End Select
  62.  
  63. End Function
  64.  
  65. 'Process a "#define" instruction
  66. 'These are tricky, since they can be constant definitions or flags or macros
  67. Private Function ProcessDefine(strDefinition As String) As String
  68. Dim parts() As String
  69. Dim dummy As String
  70.  
  71. 'Find out if we're dealing with a macro or not
  72. If InStr(strDefinition, "(") <> 0 Then
  73.     ProcessDefine = ProcessDefineAsFunction(strDefinition)
  74. Else
  75.     parts = GetToken(strDefinition, 0, dummy)
  76.     parts(0) = IIf(IsPublic, "Public ", "Private ") & "Const"
  77.     parts(1) = parts(1) & " ="
  78.     
  79.     'If the value is a hex number, convert it to VB style notation
  80.     If InStr(parts(UBound(parts)), "0x") <> 0 Then
  81.         parts(UBound(parts)) = Chex2VBhex(Trim(parts(UBound(parts))))
  82.     End If
  83.     
  84.     'If the value is an octal number, convert it to VB style notation
  85.     If InStr(parts(UBound(parts)), "0o") <> 0 Then
  86.         parts(UBound(parts)) = Coct2VBoct(Trim(parts(UBound(parts))))
  87.     End If
  88.     
  89.     dummy = Trim(Join(parts, " "))
  90.     
  91.     'See if we're dealing with a flag
  92.     parts = Split(dummy, "=")
  93.     
  94.     If parts(UBound(parts)) = "" Then   'Is this part of an #ifndef?
  95.         parts(UBound(parts)) = "True"
  96.         dummy = Join(parts, " = ")
  97.         parts = Split(dummy, "Const")
  98.         parts(0) = "#"
  99.         dummy = Join(parts, "Const")
  100.     Else
  101.         'Put it all together
  102.         dummy = Join(parts, " = ")
  103.     End If
  104.     
  105.     ProcessDefine = dummy
  106. End If
  107. End Function
  108.  
  109. 'This function will someday be able to process macro definitions
  110. 'Not yet though....
  111. Private Function ProcessDefineAsFunction(strDefine As String) As String
  112. Dim dummy As String
  113.  
  114. dummy = "#If False Then" & vbCrLf
  115. dummy = dummy & "'The following is a macro defined in C/C++." & vbCrLf
  116. dummy = dummy & strDefine & vbCrLf & "#End If" & vbCrLf
  117. ProcessDefineAsFunction = dummy
  118.  
  119. End Function
  120.