Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Win 32: Use REGEXPR to ".h" and .".rc" files into PB ".bi" files with equates

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Win 32: Use REGEXPR to ".h" and .".rc" files into PB ".bi" files with equates

    One of the hassles of Writing resource files is creating the PB "%Equate_Name" entities to use in your program. This may help.

    Code:
    $IF 0
      FILE: REGEXPR-CREATE-INC.BAS FOR PB/CC 2.0
      Parse "C" OR resource file, looking FOR #DEFINE  SQL_xxxxxxxx    nnnnnnn
      REPLACE WITH   %SQL_xxxxxxxx   =    nnnnnnn
      That is, convert "#Define"s into PB equates
      Author: Michael Mattias, Racine WI
      March 5, 2000
      This code is placed IN the public domain by the author, who expressly waives
      ANY claim OF copyright OR patent otherwise available under United States OF
      America OR other applicable international laws, treaties OR conventions.
      
      WHILE NOT required, credit would be way cool IF you find this useful.
      
    $ENDIF
    
    ' don't allow PB to assign REGISTER variables.
    #REGISTER NONE
    #DIM ALL
    '#DEBUG ERROR ON
    
    '#COMPILE EXE "REGEXPR-CREATE-INC.EXE"
    ' cuz the compiler is a 16-bit app
    $INCLUDE "WIN32API.INC"
    
    
    FUNCTION WinMain (BYVAL hCurInstance  AS LONG, _
                      BYVAL hPrevInstance AS LONG, _
                      lpszCmdLine         AS ASCIIZ PTR, _
                      BYVAL nCmdShow      AS LONG) EXPORT AS LONG
    
       DIM ThisFile AS ASCIIZ * %Max_Path
       DIM Stat AS LONG
       DIM J$
       Stat = GetModuleFileName (hCurInstance, ThisFile, SIZEOF(ThisFile) )
       PRINT "Hello World from Program " & ThisFile
       J$ = WAITKEY$
       CALL DoIt
       J$ = WAITKEY$
    
    END FUNCTION
    
    
    SUB Doit
       DIM ThePath AS STRING
       ThePath = "C:\Software_Development\Testdata\"
       DIM Infile AS STRING, Outfile AS STRING
       Infile = "SQL.H"
       Outfile = EXTRACT$(Infile, ".") + ".BI"
       InFile = ThePath & Infile
       Outfile = ThePath & Outfile
       PRINT "Processing file " & Infile
     
       DIM Stat AS LONG
       DIM FileData AS STRING, IH AS LONG, OH AS LONG
       
    $IF 0
    
      We want REGEXPR to look FOR:
      #define        Literal
      [\x20]+        One OR more spaces
      SQL_           Literal
      [a-z0-9_]      ANY combination OF letters, numbers AND underscores
                     above two items TO be remembered AS tag1
      [\x20]+        One OR more spaces
      [0-9xL]+       One OR more numeric digits, X AND L (TO be remembered AS tag2)
      This does NOT HANDLE "#define IDM_DO_THIS = WM_USER + 1" but that is
      an exercise left for the ambitious.
    $ENDIF
    
       DIM SearchForReplaceMask AS STRING
       SearchForReplaceMask = "#define[\x20]+(SQL_[a-z0-9_]+)[\x20]+([0-9xl]+)"
       DIM ReplaceMask AS STRING
       ReplaceMask = "%\01 = \02"    '  %(SQL_xxxxx) = nnnnnn
       DIM NewData AS STRING
       DIM PosVar AS LONG, StartVar AS LONG, LenVar AS LONG
       DIM SelectCount AS LONG
       IH = FREEFILE
       OPEN InFile FOR INPUT AS IH
       OH = FREEFILE
       OPEN OutFile FOR OUTPUT AS OH
       PRINT #OH, "$IF 0"
       PRINT #OH, "  Generated PowerBASIC Equate File"
       PRINT #OH, "  Source file:" & Infile
       PRINT #OH, "  Search mask Used:" & SearchForReplaceMask
       PRINT #OH, "  Date Created:" & DATE$
       PRINT #OH, "  Courtesy: Michael Mattias Racine WI"
       PRINT #OH, "$ENDIF
       PRINT #OH,
       
       WHILE NOT EOF(IH)
        LINE INPUT #IH, FileData
        ' see if this is a valid line per our searchmask..
        PosVar = 0
        REGEXPR SearchForReplaceMask IN FileData TO PosVar, LenVar
        IF PosVar THEN
           INCR SelectCount
           StartVar=1: NewData = ""
           FileData = LEFT$(FileData, LenVar)
          REGREPL SearchForReplaceMask IN FileData WITH ReplaceMask AT StartVar TO Posvar, NewData
          ' make replacement of c-style "0x0" with PB-style  "&h0"
          REPLACE "0x0" WITH "&h0" IN NewData
          ' remove trailing "L" sometimes used by C programmers to "cast" numeric values..
          NewData = RTRIM$(NewData, ANY "lL")
          PRINT ".";
          PRINT #OH, NewData
        END IF
       WEND
       PRINT
       CLOSE IH, OH
       
       PRINT "Selected "; FORMAT$(SelectCount);" lines"
       PRINT "Wrote Output to file "; OutFile
        
    END SUB



    -------------
    Michael Mattias
    Racine WI USA
    [email protected]
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

  • #2
    Nice work Michael! I wrote a similar (and less sophisticated) program for my own use a while back, and then I wrote a batch file to process my resource files. The batch file calls RC.EXE, then PBRES.EXE, then my equate-file-maker program, all based on a file name that is passed to the batch on the command line, so with a program like yours, creating PBR files can be virtually a one-step process.

    Hmmm... Maybe your app could SHELL to RC and PBRES after it converts the file?

    -- Eric

    P.S. I found that adding an ampersand to the end of hex equates was a necessary step for the conversion of 0x values over 32k, like &hFFFF&.

    ------------------
    Perfect Sync: Perfect Sync Development Tools
    Email: mailto:[email protected][email protected]</A>



    [This message has been edited by Eric Pearson (edited March 05, 2000).]
    "Not my circus, not my monkeys."

    Comment

    Working...
    X