Once I posted a library, a collection of batch functions and macros and some tools for convenient batch scripting. This allows me to write less error prone scripts much faster due to the high abstraction a library provides.
Unfortunately the more macro's included in a library the longer it takes to initialize. One way to reduce load and optimize performance is to only include those macro's that will be used. What if this process could be automated ? What if I could write a batch file using functions stored in other files and then having it processed in some way so it can run independently from its library ? Having a byteFile pumped out automatically which would have higher performance and smaller file-size than the original.
The script I am writing may use this particular library function;
Code: Select all
::--------------------------------------------------------------------------------------------------------------------------
set "$defines=toParams_"
::--------------------------------------------------------------------------------------------------------------------------
:: last updated : 2012^11^14
:: support : naDelayed, delayedChars, related chaining
::
:: Processes formatted arguments.
::
:: -Value is assigned to argName, if value is null then argName is set true.
::
:: -Similar to how microsoft programs parse commandline arguments,
:: arguments are only valid if they start with a slash and are combined if they end with a colon,
:: other cases are ignored eg; /argName: "value" /boolArg
::
:: -Unless enclosed in double quotes, the default delimiters apply.
::
:: -Double quotes inside values which are unevenly distributed need to be doubled, eg;
:: correct; /argName: "val""ue", wrong; /argName: "val"ue"
::
:: $par0: original parameters passed to the function.
:: $par : number of arguments passed
:: $parN: where n is a number from 1 to number of arguments passed, the actual argument.
:: $p : the value of the arguments passed ( booleans excluded ).
::
set ^"$usage.%$defines%=^
%= =% use: ( %%toParams_%% #etc.. )%$n1c%
%= =% ret: string $p, array $par, all variables as boolean or string%$n1c%
%= =% err: unaffected, panic otherwise"
::
2>%_Dcon% ( %macroStart_% enableDelayedExpansion )
:: (
%= =%set ^"!$defines!=!forQ_! (1,2) do if %%?==2 (!setLocal_!!$defines!%$n1c%
%= =%!necho_! !$defines!%e7%(^^^!_^^^!%e7%)%$n1c%
!==^^^!^
%= =%!forÇ_! (1,1,^^^!$par^^^!) do set $par%%ç=%$n1c%
%= =%!reDelayLH_!_%$n1c%
%= =%set $par=^&set $par0=^&!forQ_! (^
%= =% ^^^!_^^^!^
%= =%) do set/a$par+=1^&set $par0=^^^!$par0^^^!,%%?^&set $par^^^!$par^^^!=%%?%$n1c%
%= =%if defined $par0 set $par0=^^^!$par0:~1^^^!%$n1c%
!==^^^!^
%= =%!necho_! $par: '^^^!$par^^^!'%$n1c%
%= =%!necho_! $par0: '^^^!$par0^^^!'%$n1c%
%= =%set $r=^^^!$r^^^!,$par,$par0%$n1c%
!==^^^!^
%= =%set $p=^&set ?=^&!forÇ_! (1,1,^^^!$par^^^!) do (^
!==^^^!^
%= =%!necho_! $par%%ç: '^^^!$par%%ç^^^!'%$n1c%
%= =%set $r=^^^!$r^^^!,$par%%ç%$n1c%
%= =%if defined ? (^
!==^^^!^
%= =%set $p=^^^!$p^^^!,^^^!$par%%ç^^^!%$n1c%
%= =%!deQuoteLH_!$par%%ç%$n1c%
%= =%!necho_! $^^^!?^^^!: '^^^!$par%%ç^^^!'%$n1c%
%= =%set $^^^!?^^^!=^^^!$par%%ç^^^!%$n1c%
%= =%set $r=^^^!$r^^^!,$^^^!?^^^!%$n1c%
%= =%set ?=^
!==^^^!^
%= =%)else if ^^^!$par%%ç:~0%e7%,1^^^!==/ (^
!==^^^!^
%= =%if ^^^!$par%%ç:~-1^^^!==: (^
!==^^^!^
%= =%set ?=^^^!$par%%ç:~1,-1^^^!%$n1c%
%= =%!necho_! $^^^!?^^^!: '1'^
!==^^^!^
%= =%)else (^
!==^^^!^
%= =%!necho_! $^^^!$par%%ç:~1^^^!: '1'%$n1c%
%= =%set $^^^!$par%%ç:~1^^^!=1%$n1c%
%= =%set $r=^^^!$r^^^!,$^^^!$par%%ç:~1^^^!%$n1c%
%= =%set $p=^^^!$p^^^!,1^
%= =%)^
!==^^^!^
%= =%)else set $p=^^^!$p^^^!,^^^!$par%%ç^^^!^
!==^^^!^
%= =%)%$n1c%
%= =%if defined $p set $p=^^^!$p:~1^^^!%$n1c%
%= =%!necho_! $p: '^^^!$p^^^!'%$n1c%
%= =%set $r=^^^!$r^^^!,$p%$n1c%
!==^^^!^
%= =%!forQ_! (^^^!$err^^^!) do (^
%= =%!nErrL!^&!endlocalL_!^^^!$r^^^!%$n1c%
%= =%cmd/cexit %%?^
%= =%))else setlocal enableDelayedExpansion^&set _="
:: )
2>%_Dcon% %macroEnd%
%endlocalR_% (
%$%
)
::
%@bTryAssert%
:: (
setlocal enableDelayedExpansion
:: (
( %toParams_% /arg0: "this work^^ s^!" /arg1: this_^^^^works^^^! /arg2 not_recognized ) ||( %assertFail_% )
( %toParams_% ) ||( %assertFail_% )
:: )
endlocal
:: )
::--------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
Code: Select all
@echo off &title %~n0 &prompt $G &<nul set /p "= " &chcp 850 &goto :§main_ () %= prompt minimalistic, codepage 850 =%
::--------------------------------------------------------------------------------------------------------------------------
:: toIndependentByteCode ( byteCode[independent] )
::--------------------------------------------------------------------------------------------------------------------------
:: (
:: sourceFile : none; this is a unitTest
:: sourceFunction : §label0
:: programmer : tooComplex
::
:: sourceProject : doskitXP32x86 v20131205
:: supported platform/s: windows XP - 32bit versions - x86 architectures
:: supported languages : dutch, english ( experimental ), international ( experimental )
:: programmer : tooComplex
::
:: dump type : independent
:: /independent : external executables are still required to run the application
:: /linked : external executables and project files are still required to run the application
::
:: special thanks to : admin, jeb, dBenham, aGerman from www.dosTips.COM, the dosTips team and members
:: )
::--------------------------------------------------------------------------------------------------------------------------
::--------------------------------------------------------------------------------------------------------------------------
:§init_ ( support $cr, $lf, $esc, $sub, $tab, $bs ) [requires disableDelayedExpansion]
::--------------------------------------------------------------------------------------------------------------------------
:: (
title toIndependentByteCode
:: $bs, Ascii-0x08-08
for /f "delims=#" %%? in (
'"prompt #$H# &echo on &for %%? in (1) do rem"'
) do set "$bs=%%?"
::
set "$bs=%$bs:~0,1%"
:: $tab Ascii-0x09
for /f "skip=4tokens=2delims=pR " %%? in (
'reg.EXE query hkcu\environment /v temp'
) do set "$tab=%%?"
:: $lf Asci-0x0A
set $lf=^
:: $cr Ascii-0x0D
for /f %%? in ( 'copy /z "%~f0" nul' ) do set "$cr=%%?"
:: $sub Ascii-0x1A
>nul copy /z /a nul + nul "%temp%\$sub.TMP"
:: (
for /f "useback" %%? in (
"%temp%\$sub.TMP"
) do set "$sub=%%?"
:: )
>nul del /f /q "%temp%\$sub.TMP"
:: $esc Ascii-0x1B-27
for /f "delims=#" %%? in (
'"prompt #$E# &echo on &for %%? in (1) do rem"'
) do set "$esc=%%?"
:: $n1c newLine+continuation, 1 expansions, 0sets, 1echo
set ^"$n1c=^^^%$lf%%$lf%^%$lf%%$lf%^^"
:: common variables
set /a $bUninstallOnExit = 0
set /a $err = 0
:: )
exit /b %$err%
::--------------------------------------------------------------------------------------------------------------------------
::--------------------------------------------------------------------------------------------------------------------------
:§memRestore_ (toParams_) [requires enableDelayedExpansion]
::--------------------------------------------------------------------------------------------------------------------------
:: (
echo(&<nul set/p= restoring memory...
:: (toParams_) '4146' bytes on file, '2530' bytes in memory.
:: (
%= =%set ^"toParams_=for %%? in (1,2) do if %%?==2 (for %%? in (1,2) do if %%?==2 (for %%? in (^^^!~^^^!) do (if ^^^!§inline^^^!.==. echo(%$n1c%
%= =%set/a$debug+=1,$err=0,§inline=1%$n1c%
%= =%set $trace=%%~?%$n1c%
%= =%set $debug[^^^!$debug^^^!]=%%~?%$n1c%
%= =%set $%%~?.p=^^^!*^^^!^&if ^^^!*:~0^^^^,1^^^!==^^^^^^^^^^^^^^^! set $%%~?.p=^^^!*:~1^^^!%$n1c%
%= =%set $r=$err))else set ~=toParams_%$n1c%
%= =%echo(^&^<nul set/p= toParams_^^^^(^^^!_^^^!^^^^)%$n1c%
%= =%for /l %%ç in (1,1,^^^!$par^^^!) do set $par%%ç=%$n1c%
%= =%for %%? in (1,2) do if %%?==2 (for %%? in (^^^!*^^^!) do set ?=^&set ?=^^^!%%~?^^^!^&^&(set ?=^^^!?:^"=^"^"^^^!%$n1c%
%= =%call set ^"?=%%^^^^?:^^^^^^^!=#^"#^"^^^^^^^!%%^"%$n1c%
%= =%set ^"?=^^^!?:^^^^=^^^^^^^^^^^!^"^&set ^"?=^^^!?:#^"#^"=^^^^^^^!^"%$n1c%
%= =%set ?=^^^!?:^"^"=^"^^^!%$n1c%
%= =%set %%~?=^^^!?^^^!))else set *=_%$n1c%
%= =%set $par=^&set $par0=^&for %%? in ( ^^^!_^^^!) do set/a$par+=1^&set $par0=^^^!$par0^^^!,%%?^&set $par^^^!$par^^^!=%%?%$n1c%
%= =%if defined $par0 set $par0=^^^!$par0:~1^^^!%$n1c%
%= =%echo(^&^<nul set/p= $par: '^^^!$par^^^!'%$n1c%
%= =%echo(^&^<nul set/p= $par0: '^^^!$par0^^^!'%$n1c%
%= =%set $r=^^^!$r^^^!,$par,$par0%$n1c%
%= =%set $p=^&set ?=^&for /l %%ç in (1,1,^^^!$par^^^!) do (echo(^&^<nul set/p= $par%%ç: '^^^!$par%%ç^^^!'%$n1c%
%= =%set $r=^^^!$r^^^!,$par%%ç%$n1c%
%= =%if defined ? (set $p=^^^!$p^^^!,^^^!$par%%ç^^^!%$n1c%
%= =%for %%? in (1,2) do if %%?==2 for /f ^"tokens=1-26delims=,%$tab% ^" %%a in (^"^^^!*^^^!^") do (set _=^&set _=^^^!%%~a^^^!^&^&if ^^^!_:~0^^^^,1^^^!==^^^^^" if ^^^!_:~-1^^^!==^^^^^" set %%~a=^^^!_:~1,-1^^^!)else set *=$par%%ç%$n1c%
%= =%echo(^&^<nul set/p= $^^^!?^^^!: '^^^!$par%%ç^^^!'%$n1c%
%= =%set $^^^!?^^^!=^^^!$par%%ç^^^!%$n1c%
%= =%set $r=^^^!$r^^^!,$^^^!?^^^!%$n1c%
%= =%set ?=)else if ^^^!$par%%ç:~0^^^^,1^^^!==/ (if ^^^!$par%%ç:~-1^^^!==: (set ?=^^^!$par%%ç:~1,-1^^^!%$n1c%
%= =%echo(^&^<nul set/p= $^^^!?^^^!: '1')else (echo(^&^<nul set/p= $^^^!$par%%ç:~1^^^!: '1'%$n1c%
%= =%set $^^^!$par%%ç:~1^^^!=1%$n1c%
%= =%set $r=^^^!$r^^^!,$^^^!$par%%ç:~1^^^!%$n1c%
%= =%set $p=^^^!$p^^^!,1))else set $p=^^^!$p^^^!,^^^!$par%%ç^^^!)%$n1c%
%= =%if defined $p set $p=^^^!$p:~1^^^!%$n1c%
%= =%echo(^&^<nul set/p= $p: '^^^!$p^^^!'%$n1c%
%= =%set $r=^^^!$r^^^!,$p%$n1c%
%= =%for %%? in (^^^!$err^^^!) do (echo(^&(if ^^^!$err:~0^^^^,1^^^!==- (set ?=^^^!$err:~1^^^!)else set ?=^^^!$err^^^!%$n1c%
%= =%for %%? in (^"$^^^!?^^^!.error^") do if ^^^!?^^^! neq 0 (^<nul set/p= [error:^^^!$err^^^!] ^^^!%%~?^^^! ^^^^^^^^^^^^^^^!)else ^<nul set/p= [^^^!%%~?^^^!:^^^!$err^^^!])^&for %%? in (1,2) do if %%?==2 (set ¤=^&for %%? in (^^^!º^^^!) do if ^^^!¤^^^!.==. set/a¤=0%%~? 2^>nul^&^&call set º=%%º:*^^^!¤^^^!=%%^|^|set/a¤=1%$n1c%
%= =%set _=^&for %%? in (^^^!º^^^!) do (set ?=^^^!%%~?^^^!^&if defined ? (set ?=^^^!?:§=§0^^^!%$n1c%
%= =%set ?=^^^!?:^^^^!$lf!!$lf!=§0l^^^!%$n1c%
%= =%for /f %%1 in (^"^^^!$cr^^^! ^") do set ?=^^^!?:%%1=§0c^^^!)%$n1c%
%= =%set _=^^^!_^^^!§l%%~?=^^^!?^^^!)%$n1c%
%= =%set _=^^^!_:^"=^"^"^^^!%$n1c%
%= =%set ^"_=^^^!_:^^^^=§9^^^!^"^&call set ^"_=%%^^^^_:^^^^^^^!=§3%%^"%$n1c%
%= =%for /f ^"delims=^" %%r in (^"^^^!_:~2^^^!^") do (for /l %%^^^! in (1,1,^^^!¤^^^!) do endlocal%$n1c%
%= =%set ?=^^^!^&setlocal enableDelayedExpansion%$n1c%
%= =%set _=%%r%$n1c%
%= =%call set ^"_=%%^^^^_:§3=§3^^^^^^^!%%^"^&if ^^^!?^^^!.==. (set ^"_=^^^!_:§9=^^^^^^^^^^^!^"^&set ^"_=^^^!_:§3=^^^^^^^!^"%$n1c%
%= =%)else set ^"_=^^^!_:§9=^^^^^^^!^"^&set _=^^^!_:§3=^^^!%$n1c%
%= =%set _=^^^!_:^"^"=^"^^^!%$n1c%
%= =%for %%? in (^"^^^!$lf^^^!^") do set _=^^^!_:§l=%%~?^^^!%$n1c%
%= =%set _=^^^!_:§0=§^^^!%$n1c%
%= =%for /f delims^^^^=^^^^ eol^^^^= %%r in (^"^^^!_^^^!^") do (endlocal%$n1c%
%= =%if ^^^!.==. (set _=%%r^^^!%$n1c%
%= =%set _=^^^!_:§l=^^^^!$lf!!$lf!^^^!%$n1c%
%= =%for /f %%1 in (^"^^^!$cr^^^! ^") do set _=^^^!_:§c=%%1^^^!%$n1c%
%= =%set ^"^^^!_^^^!^"^^^!)else set ^"%%r^"%$n1c%
%= =%setlocal enableDelayedExpansion)%$n1c%
%= =%endlocal))else set º=^^^!$r^^^!%$n1c%
%= =%cmd/cexit %%?))else setlocal enableDelayedExpansion^&set _=" !
:: )
<nul set/p=%$bs%%$bs% [done]
:: )
exit /b %$err%
::--------------------------------------------------------------------------------------------------------------------------
::--------------------------------------------------------------------------------------------------------------------------
:§main_ ( %* )
::--------------------------------------------------------------------------------------------------------------------------
setlocal enableExtensions disableDelayedExpansion
:: (
:: set common variables &functions
:: (
call :§init_ ()
:: )
:: get commandLineArgs( %* )
:: (
(call set $par0=%%^*) %= beware that set arg * can break code between braces unless called =%
:: ) %= $lf prevents winXP exception; 'input corrupt or incorrectly escaped' =%
:: load memory dump, call function dump
setlocal enableDelayedExpansion &call :§memRestore_ ()
:: (
call :§label0 !$par0! ^!
:: )
endlocal &set /a $err = %$err%
:: )
endlocal &exit %$err%
::--------------------------------------------------------------------------------------------------------------------------
::--------------------------------------------------------------------------------------------------------------------------
:§label0 ( %* )
::--------------------------------------------------------------------------------------------------------------------------
:: (
:: see_(toParams_)
setlocal enableDelayedExpansion
:: (
( echo( ) &for %%? in ( toParams_ ) do echo(&<nul set/p= %%~?=!%%~?!
:: )
endlocal
:: unitTest_(toParams_)
:: (
( echo( ) &( %toParams_% /arg0: "this work^^ s^!" )
:: )
echo(&<nul set/p= &pause
:: )
exit /b %$err%
::--------------------------------------------------------------------------------------------------------------------------
Code: Select all
Actieve codetabel: 850
restoring memory. [done]
toParams_=for %? in (1,2) do if %?==2 (for %? in (1,2) do if %?==2 (for %? in (
!~!) do (if !ºinline!.==. echo(
set/a$debug+=1,$err=0,ºinline=1
set $trace=%~?
set $debug[!$debug!]=%~?
set $%~?.p=!*!&if !*:~0^,1!==^^^! set $%~?.p=!*:~1!
set $r=$err))else set ~=toParams_
echo(&<nul set/p= toParams_^(!_!^)
for /l %þ in (1,1,!$par!) do set $par%þ=
for %? in (1,2) do if %?==2 (for %? in (!*!) do set ?=&set ?=!%~?!&&(set ?=!?:"=
""!
call set "?=%^?:^!=#"#"^!%"
set "?=!?:^=^^!"&set "?=!?:#"#"=^!"
set ?=!?:""="!
set %~?=!?!))else set *=_
set $par=&set $par0=&for %? in ( !_!) do set/a$par+=1&set $par0=!$par0!,%?&set $
par!$par!=%?
if defined $par0 set $par0=!$par0:~1!
echo(&<nul set/p= $par: '!$par!'
echo(&<nul set/p= $par0: '!$par0!'
set $r=!$r!,$par,$par0
set $p=&set ?=&for /l %þ in (1,1,!$par!) do (echo(&<nul set/p= $par%þ: '!$par%þ
!'
set $r=!$r!,$par%þ
if defined ? (set $p=!$p!,!$par%þ!
for %? in (1,2) do if %?==2 for /f "tokens=1-26delims=, " %a in ("!*!") do (set
_=&set _=!%~a!&&if !_:~0^,1!==^" if !_:~-1!==^" set %~a=!_:~1,-1!)else set *=$p
ar%þ
echo(&<nul set/p= $!?!: '!$par%þ!'
set $!?!=!$par%þ!
set $r=!$r!,$!?!
set ?=)else if !$par%þ:~0^,1!==/ (if !$par%þ:~-1!==: (set ?=!$par%þ:~1,-1!
echo(&<nul set/p= $!?!: '1')else (echo(&<nul set/p= $!$par%þ:~1!: '1'
set $!$par%þ:~1!=1
set $r=!$r!,$!$par%þ:~1!
set $p=!$p!,1))else set $p=!$p!,!$par%þ!)
if defined $p set $p=!$p:~1!
echo(&<nul set/p= $p: '!$p!'
set $r=!$r!,$p
for %? in (!$err!) do (echo(&(if !$err:~0^,1!==- (set ?=!$err:~1!)else set ?=!$e
rr!
for %? in ("$!?!.error") do if !?! neq 0 (<nul set/p= [error:!$err!] !%~?! ^^^!)
else <nul set/p= [!%~?!:!$err!])&for %? in (1,2) do if %?==2 (set ñ=&for %? in (
!║!) do if !ñ!.==. set/añ=0%~? 2>nul&&call set ║=%║:*!ñ!=%||set/añ=1
set _=&for %? in (!║!) do (set ?=!%~?!&if defined ? (set ?=!?:º=º0!
set ?=!?:^
=º0l!
for /f %1 in ("!$cr! ") do set ?=!?:%1=º0c!)
set _=!_!ºl%~?=!?!)
set _=!_:"=""!
set "_=!_:^=º9!"&call set "_=%^_:^!=º3%"
for /f "delims=" %r in ("!_:~2!") do (for /l %! in (1,1,!ñ!) do endlocal
set ?=!&setlocal enableDelayedExpansion
set _=%r
call set "_=%^_:º3=º3^!%"&if !?!.==. (set "_=!_:º9=^^!"&set "_=!_:º3=^!"
)else set "_=!_:º9=^!"&set _=!_:º3=!
set _=!_:""="!
for %? in ("!$lf!") do set _=!_:ºl=%~?!
set _=!_:º0=º!
for /f delims^=^ eol^= %r in ("!_!") do (endlocal
if !.==. (set _=%r!
set _=!_:ºl=^
!
for /f %1 in ("!$cr! ") do set _=!_:ºc=%1!
set "!_!"!)else set "%r"
setlocal enableDelayedExpansion)
endlocal))else set ║=!$r!
cmd/cexit %?))else setlocal enableDelayedExpansion&set _=
toParams_( /arg0: "this work^ s!" )
$par: '2'
$par0: '/arg0:,"this work^ s!"'
$par1: '/arg0:'
$arg0: '1'
$par2: '"this work^ s!"'
$arg0: 'this work^ s!'
$p: '"this work^ s!"'
[ok:0]
Druk op een toets om door te gaan. . .
What I hate more than programming is reinventing the wheel. In an ideal world, code is nothing more than a technical artifact from a model.