Thread: Batch: Trying to replace c++ includes

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    277

    Batch: Trying to replace c++ includes

    I'm converting a project from delphi into c++ and there's a lot of files, I managed to get close to the script I want but still having problems, here's my current script:
    Code:
    set head=Classes.h
    set chk="#include ""%head%"""
    set out="#include <delphi/%head%>"
    for %%i in (*.h) do (
    call :loop_lines %%i
    )
    :loop_lines
    set file=%1
    for /f "tokens=* delims=\r\n" %%j in (%1) do (
    call :chk_line "%%j"
    )
    rem del %1
    rem rn %1.tmp %1
    goto :eof
    :chk_line
    set line=%~1
    if /i %1 == %chk% (
    echo %out%
    ) else (
    if /i "%line:~0,-1" == %chk% (
    echo %out%
    } else {
    rem "%2"
    )
    )
    :eof
    It still outputs the occasional error of unrecognised command (project is large so I turned of echo at the line loop and turned back after), is there anyone who can fix this and explain to me what I was doing wrong? (for now leave as just echo/rem at the line part because I wanna confirm no corruption will occur before I switch to outputing to replacement files)
    Last edited by awsdert; 1 Week Ago at 04:35 AM. Reason: typo

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,836
    > if /i %1 == %chk%
    Is there any chance that there are spaces, blanks, shell metacharacters (or any other weirdness) which might trip you up?

    DOS/CMD batch files always seem to need ad-hoc hackery to work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    277
    well some of them have resource strings but I would expect that to be handled just the same as the header strings, the headers also contain classes and some I expect would use templates given they were auto converted from pas files via a program called delphi2cpp and batch making use of that program, my current target is to declare all the classes needed by the app before gradually replacing the delphi inherited stuff with IUP for the graphical stuff and standard classes like std::string. The goal of the conversion is to make porting to another system easier by implementing the original API with behind the scenes stuff, this will also encourage many more developers to get involved the project because it would finally be using a programing language that is semi sane (I think C is more sane but asking the original developers to transition from class based delphi to pointer based C right off the bat is a bit much, C++ is a suitable middle ground).

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    277
    After getting back from work I did some further digging and found I was going about it the wrong way, I threw away those if statements in favour of something simpler:
    Code:
    set head=Classes.h
    for %%i in (*.h) do (
    call :loop_lines %%i
    )
    :loop_lines
    set file=%1
    rem 
    for /f "tokens=* delims=\r\n" %%j in (%1) do (
    call :chk_line "!j:^"%head%^"=^<delphi/%head%^>" <-- This line is not doing as expected
    )
    rem del %1
    rem rn %1.tmp %1
    goto :eof
    :chk_line
    set "line=%~1"
    set "line=%line:^=^^%"
    set "line=%line:<=^<%"
    set "line=%line:>=^>%"
    set "line=%line:(=^(%"
    set "line=%line:)=^)%"
    set "line=%line:{=^{%"
    set "line=%line:}=^}%"
    set "line=%line:[=^[%"
    set "line=%line:]=^]%"
    set "line=%line:&=^&%"
    rem %line%
    :eof
    You may have noticed I marked a line as not working there, I need help fixing that because right now it's just appending or replacing (Edit: entire line I mean) whatever I try, I also need a variant of that that replaces the lowercase with Propercase (well the header string I have anyways)
    Last edited by awsdert; 1 Week Ago at 07:07 PM.

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    277
    Seem to have fixed it, will try it tomorrow after getting some sleep, here's what I have for now, still lacking case change though:
    Code:
    set head=Classes.h
    for %%i in (*.h) do call :loop_lines %%i
    :loop_lines
    set file=%1
    rem "" > %1
    for /f "tokens=* delims=\r\n" %%j in (%1) do call :chk_line "%%j"
    rem del %1
    rem rn %1.tmp %1
    goto :eof
    :chk_line
    call :chk_line_sub %1:%head%=%head%
    goto :eof
    :chk_line_sub
    call :chk_line_sub_sub %1:"%head%"=^<delphi/%head%^>
    goto :eof
    :chk_line_sub_sub
    set "1=%1:~1,-1"
    set "l=%l:^=^^"
    set "l=%l:<=^<"
    set "l=%l:>=^>"
    set "l=%l:(=^("
    set "l=%l:)=^)"
    set "l=%l:{=^{"
    set "l=%l:}=^}"
    set "l=%l:[=^["
    set "l=%l:]=^]"
    set "l=%l:&=^&"
    set "l=%l::=^:"
    rem %l >> %file%
    :eof

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    277
    I think I finally cracked it:
    Code:
    set head=Classes.h
    set ichk="%head%"
    set "feed=#include <delphi/%head%>"
    for %%i in (*.h) do call :loop_lines %%i
    :loop_lines
    set file=%1.tmp
    echo  >nul
    for /f "tokens=* delims=\r\n" %%j in (%1) do call :chk_line "%%j" %%j
    if exists(%file%) == true del %file%
    rem rn %file% %1
    goto :eof
    :chk_line
    set "line=%~1"
    if "%2" == "#include" (
    if /i "%3" == "%ichk%" (
    set "line=%feed%"
    )
    )
    if not "echo %line%>nul" == 0 exit /B
    :eof

  7. #7
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    277
    It just spat an error at me:
    Code:
    O:\Data\C_Playground\cheat-engine-master\Cheat Engine>set "line=// uses 85 ascii characters which are better to use in XML ( no &'<"\"
    O:\Data\C_Playground\cheat-engine-master\Cheat Engine>if "and" == "#include" (if /I "no" == ""Classes.h"" (set "line=#include <delphi/Classes.h>" ) )
    > was unexpected at this time.
    O:\Data\C_Playground\cheat-engine-master\Cheat Engine>if not "echo // uses 85 ascii characters which are better to use in XML ( no &'<"\>nul" == 0 exit /B
    Is what it spat out at me, any ideas on getting passed this?

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    277
    After some fiddling I got it running now, waiting to see if it will spit another error, for now here is my script:
    Code:
    set head=Classes.h
    set ichk="%head%"
    set "feed=#include <delphi/%head%>"
    for %%i in (*.h) do call :loop_lines %%i
    :loop_lines
    set file=%1.tmp
    echo  >nul
    for /f "tokens=* delims=\r\n" %%j in (%1) do call :chk_line "%%j" %%j
    if exists(%file%) == true del %file%
    rem rn %file% %1
    goto :eof
    :literalize
    set l=%l:^^=^^^^%
    set l=%l:^%=^^^%%
    set l=%l:^&=^^&%
    set l=%l:^|=^^|%
    set l=%l:^<=^^<%
    set l=%l:^>=^^>%
    goto :eof
    :chk_line
    if "%1" == "" goto :eof
    echo off
    set l=%~1
    if "%2" == "#include" (
    if /i "%3" == "%ichk%" (
    set "line=%feed%"
    )
    )
    call :literalize
    call :try_line %2
    goto :eof
    :try_line
    echo on
    echo %l%>nul
    :eof

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with includes
    By vfpereira in forum C Programming
    Replies: 6
    Last Post: 02-26-2015, 11:10 AM
  2. who includes who?
    By sept in forum C++ Programming
    Replies: 5
    Last Post: 02-23-2008, 06:53 AM
  3. Includes
    By golfinguy4 in forum Windows Programming
    Replies: 7
    Last Post: 01-29-2002, 05:29 PM
  4. #includes
    By RedLenses in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2002, 03:59 PM

Tags for this Thread