November 30, 2020, 01:34:57 AM

Author Topic: [bb] .decls Converter by N [ 1+ years ago ]  (Read 762 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] .decls Converter by N [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : .decls Converter
Author : N
Posted : 1+ years ago

Description : Basically, this is an application to convert Blitz3D/Plus .decls files to useable BlitzMax source code.

There is an optional flag you can pass to it -- '/u'.  This sets it up so that it calls LoadLibraryW instead of LoadLibraryA.  By default it is not enabled since Pub.Win32 doesn't have it declared.  This is by and large dependent on your code, which is why it isn't enabled by default.

To use it you just compile it and drag decls files onto it.  Simple as that, nothing hard.


Code :
Code: BlitzBasic
  1. SuperStrict
  2.  
  3. Framework Brl.Blitz
  4. Import Brl.Filesystem
  5. Import Brl.Stream
  6. Import Brl.LinkedList
  7.  
  8. Global libs:TList = New TList
  9. Global mode$="A"
  10.  
  11. For Local i:Int = 1 To AppArgs.Length-1
  12.     If AppArgs[i].ToLower( ) = "/u" Then
  13.         AppArgs[i] = ""
  14.         mode = "W"
  15.     EndIf
  16. Next
  17.  
  18. For Local i:Int = 1 To AppArgs.Length-1
  19.     If AppArgs[i].Length = 0 Then Continue
  20.     ProcessDecl( AppArgs[i] )
  21. Next
  22.  
  23. End
  24.  
  25. Type TFunc
  26.     Field sym$
  27.     Field decl$
  28.    
  29.     Method Format$( s$ )
  30.         Return s.Replace( "$(SYM)", sym )..
  31.                 .Replace( "$(DECL)", decl )
  32.     End Method
  33.    
  34.     Function Create:TFunc( decl$, symbol$ )
  35.         Local f:TFunc = New TFunc
  36.         f.sym = symbol
  37.         f.decl = decl
  38.         Return f
  39.     End Function
  40. End Type
  41.  
  42. Type TLib
  43.     Method New( )
  44.         link = libs.AddLast( Self )
  45.         funcs = New TList
  46.     End Method
  47.    
  48.     Method Dispose( )
  49.         funcs.Clear( )
  50.         funcs = Null
  51.         link.Remove( )
  52.         link = Null
  53.         lib = Null
  54.     End Method
  55.    
  56.     Field lib$
  57.     Field funcs:TList
  58.     Field link:TLink
  59. End Type
  60.  
  61. Function ProcessDecl( declPath$ )
  62.     Local s:TStream = ReadStream( declPath )
  63.     Local lib:TLib
  64.     While Not s.Eof( )
  65.         Local l$ = s.ReadLine( ).Trim( )
  66.        
  67.         Local cf% = l.Find( ";" )
  68.         If cf <> -1 Then l = l[..cf].Trim( )
  69.        
  70.         If l.Length = 0 Then Continue
  71.        
  72.         If l[0] = 46 Then ' Set lib
  73.             If lib Then
  74.                 If lib.funcs.Count( ) = 0 Or lib.lib = "." Then
  75.                     lib.Dispose( )
  76.                     lib = Null
  77.                 EndIf
  78.             EndIf
  79.            
  80.             Local qb% = l.Find( "~q" )+1 ' Quote begin
  81.             If qb = 0 then Continue
  82.             Local qe% = l.Find( "~q", qb ) ' Quote end
  83.             If qe = -1 Then Continue
  84.            
  85.             lib = New TLib
  86.             lib.lib = l[qb..qe]
  87.         ElseIf lib Then ' Parse function
  88.             Local s$[] = SplitString( l, ":~q" )
  89.             lib.funcs.AddLast( TFunc.Create( s[0].Replace("*","@ Ptr").Replace("$","$z"), s[1] ) )
  90.         EndIf
  91.     Wend
  92.     s.Close( )
  93.     lib = Null
  94.    
  95.     s = WriteStream( StripExt(declPath)+".bmx" )
  96.    
  97.     s.WriteLine( "Strict" )
  98.     s.WriteLine( "Import Pub.Win32" )
  99.     s.WriteLine( "" )
  100.     s.WriteLine( "Private" )
  101.     s.WriteLine( "Local lib%" )
  102.     s.WriteLine( "" )
  103.     s.WriteLine( "Public" )
  104.    
  105.     For lib = EachIn libs
  106.         s.WriteLine( "" )
  107.         s.WriteLine( "lib = LoadLibrary"+mode+"(~q"+lib.lib+"~q)" )
  108.         s.WriteLine( "" )
  109.         For Local f:TFunc = EachIn lib.funcs
  110.             s.WriteLine( f.Format( "Global $(DECL) = GetProcAddress(lib, ~q$(SYM)~q)" ) )
  111.         Next
  112.         s.WriteLine( "" )
  113.         s.WriteLine( "lib = 0" )
  114.     Next
  115.    
  116.     s.Close( )
  117.     s = Null
  118.    
  119.     ClearLibs( )
  120. End Function
  121.  
  122. Function ClearLibs( )
  123.     For Local i:TLib = EachIn libs
  124.         i.Dispose( )
  125.     Next
  126.     libs.Clear( )
  127. End Function
  128.  
  129. Function SplitString$[]( s$, sp$ )
  130.     Local p:Int, l:Int, o$[32], x%
  131.     Local n:Int
  132.     For n = 0 To s.Length - 1
  133.         Local lx% = x
  134.         For p = 0 To sp.Length - 1
  135.             If s[n] = sp[p] Then
  136.                 If x = o.Length Then o = o[..o.Length*2]
  137.                 o[x] = s[l..n+(n = s.Length-1 And (Not (s[n]=sp[p])))].Trim( )
  138.                 l = n+1
  139.                 If o[x].Length=0 Then Exit
  140.                 x :+ 1
  141.                 Exit
  142.             EndIf
  143.         Next
  144.         If x <> lx Then Continue
  145.         If n = s.Length-1 Then
  146.             If x = o.Length Then o = o[..o.Length*2]
  147.             o[x] = s[l..n+(n = s.Length-1 And (Not (s[n]=sp[p])))].Trim( )
  148.             If o[x].Length = 0 Then Exit
  149.             x :+ 1
  150.         EndIf
  151.     Next
  152.     If x = 0 Then Return Null
  153.     Return o[0..x]
  154. End Function


Comments :


Picklesworth(Posted 1+ years ago)

 Excellent.Thanks, Noel.I was hoping for some way to get back all those old Decls files!


Blueapples(Posted 1+ years ago)

 Banned or not, Noel, thank you so much - this is exactly what I've been trying to find forever.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal