Advertisement
GlobalAccessSoftware

utility3.bat

Nov 16th, 2023
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 12.81 KB | Software | 0 0
  1.  
  2.  
  3. ::: RC9 :::
  4.  
  5.  
  6. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  7. ::: Utility3.bat splitout from Errorhandling.bat v8.6.8.94  :::
  8. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  9. ::: :revchars <string revstrReturned, string str2rev>                 :::
  10. ::: :makeline <string line2Fill, string padChars, string strToPadTo>  :::
  11. ::: :padder <string toRtn, int lengthRequired, string padchars>       :::
  12. ::: :getspcs <string spcs2Return, int gap>                            :::
  13. ::: :strlen <int sizeOfstr2rtn, string str2measure>   WORKING!        :::
  14. ::: :overload <no parms, no args, no returns, cash only please>       :::
  15. ::: :getgap <string gap, string line, string chars, string msg>       :::
  16. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  17.  
  18.   if not defined vers set vers=v8.7.0.10 20231108073214 by -JpE-
  19.  
  20. :alt <string 2Return, string calling, string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7>
  21.   setlocal & set "rtn0=%~1" & set "calling=%~2" & set "arg1=%~3" & set "arg2=%~4" & set "arg3=%~5" & set "arg4=%~6" & set "arg5=%~7" & set "arg6=%~8" & set "arg7=%~9"
  22.   if not defined calling goto altx
  23.   if %testwell% gtr 4 goto altx
  24.  ::: Jump to :::
  25.   if "%calling:~0,1%" neq ":" goto alt0
  26.  ::: altN where N is the number of ^ args being passed.
  27.   if "%calling%" equ ":revchars" call %calling% rtn0 "%arg1%"
  28.   if "%calling%" equ ":makeline" call %calling% rtn0 "%arg1%" "%arg2%"
  29.   if "%calling%" equ ":padder" call %calling% rtn0 %arg1% %arg2%
  30.   if "%calling%" equ ":getspcs" call %calling% rtn0 %arg1%
  31.   if "%calling%" equ ":strlen" call %calling% rtn0 "%arg1%"
  32.   if "%calling%" equ ":overload" call %calling%
  33.   if "%calling%" equ ":overload" set "rtn0= "
  34.   if "%calling%" equ ":middleman" call %calling% "%arg1%" "%arg2%"
  35.   if "%calling%" equ ":middleman" set "rtn0= "
  36.   endlocal & set "%~1=%rtn0%"
  37.   exit /b
  38.  
  39.  
  40.   ::: All vars are global from above or below same namespace for all alts,
  41.  ::: But not the functions below the last alt. They are sealed.
  42.  
  43. ::: for calls without a calling, or arg1,2,3,4,5
  44. :altx
  45. if %testwell% gtr 2 set txt=Hello New World-x!
  46. if %testwell% gtr 2 echo . & echo alt-x says: & echo %txt%
  47. if %testwell% gtr 2 echo . & pause & echo .
  48. if not defined calling set calling=Hello New World-0
  49.   if defined calling goto alt0
  50. ::: note: unindented testcode vs indented code.
  51.  ::: Then finish the :altx function below.
  52.  
  53.   ::: AND... end like... below from the "goto" of all alt.
  54.   endlocal & set "%~1=%txt%" & set "%~2=%calling%"
  55.   exit /b %errorlevel%
  56.  
  57.  
  58. ::: with a calling not begun with a colon and no other args.
  59. :alt0
  60. if %testwell% gtr 2 echo . & echo calling says %calling% & echo .
  61. REM if %testwell% gtr 2 pause & echo .
  62. if %testwell% gtr 2 if not defined arg1 set arg1=Hello New World-1
  63.   if defined arg1 goto alt1
  64. ::: note: unindented testcode vs indented code.
  65.  ::: Then finish the :alt0 function below.
  66.  
  67.   ::: AND... end like... below from the "goto" of all alt.
  68.   endlocal & set "%~1=%calling%" & set "%~2=%arg1%"
  69.   exit /b %errorlevel%
  70.  
  71.  
  72. ::: calling w/o colon; with 1 arg
  73. :alt1
  74. if %testwell% gtr 2 echo . & echo arg1 is %arg1% & echo .
  75. REM if %testwell% gtr 2 pause & echo .
  76. if %testwell% gtr 2 if not defined arg2 set arg2=Hello New World-2!
  77.   if defined arg2 goto alt2
  78. ::: note: unindented testcode vs indented code.
  79.  ::: Then finish the :alt1 function below.
  80.  
  81.   ::: AND... end like... below from the "goto" of all alt.
  82.   endlocal & set "%~1=%arg1%" & set "%~2=%arg2%"
  83.   exit /b %errorlevel%
  84.  
  85.  
  86. ::: calling w/o colon; with 2 args
  87. :alt2 <string calling, string arg1, string arg2>
  88. if %testwell% gtr 2 echo . & echo arg2 is %arg2% & echo .
  89. REM if %testwell% gtr 2 pause & echo .
  90. if %testwell% gtr 2 if not defined arg3 set arg3=Hello New World-3!
  91.   if defined arg3 goto alt3  
  92. ::: note: unindented testcode vs indented code.
  93.  REM if defined arg4 goto alt4
  94.  ::: Then finish the :alt3 function below instead of returning back 1 level.
  95.  
  96.   ::: AND... end like... below from the "goto" of all alt.
  97.   endlocal & set "%~1=%arg1%" & set "%~2=%arg2%"
  98.   exit /b %errorlevel%
  99.  
  100.  
  101. ::: calling w/o colon; with 3 args
  102. :alt3
  103. if %testwell% gtr 3 echo . & echo arg2 is %arg2% & echo .
  104. if %testwell% gtr 4 if not defined arg4 set "arg4=Hello New World-4!"
  105.   if defined arg4 goto alt4
  106.   call :getgaps spc1 spc2 "%arg1%" "%arg2%" "%arg3%"
  107.   endlocal & set "%~1=%spc1%" & set "%~2=%spc2%"
  108.   exit /b %errorlevel%
  109.  
  110.  
  111. ::: calling w/o colon; with 4 args
  112. :alt4
  113. if %testwell% gtr 2 echo . & echo arg4 is %arg4% & echo .
  114. if %testwell% gtr 2 if not defined arg5 set "arg5=Hello New World-5!"
  115.   if defined arg5 call :alt5
  116.  ::: note: unindented testcode vs indented code.
  117.  REM if defined arg5 goto alt5
  118.  ::: Then finish the :alt4 function below instead of returning back 1 level.
  119. if %testwell% gtr 1 echo . & echo arg1==%arg1%, arg2==%arg2%, arg3==%arg3%, arg4==%arg4%
  120. if %testwell% gtr 1 pause & echo .
  121.   call :exception1 "%arg1%" "%arg2%" "%arg3%" "%arg4%"
  122. if %testwell% gtr 0 echo Back from exception1 & echo . & pause & echo .  
  123.  ::: AND... end like... below from the "goto"
  124.   endlocal & set "%~1=0" & set "%~2=1"  
  125.   exit /b %errorlevel%
  126.  
  127.  
  128. ::: calling w/o colon, with 5 args.
  129. :alt5
  130. if %testwell% gtr 2 echo . & echo arg5 is %arg5% & echo .
  131. if %testwell% gtr 2 if not defined arg6 set "arg6=You have reached alt6, there is one more levels available currently."
  132.   if defined arg6 call :alt6
  133. if %testwell% gtr 2 echo . & echo %arg6% & echo .
  134.  ::: Then finish the :alt5 function below instead of returning back 1 level.
  135.  
  136. if %testwell% gtr 2 pause & echo .
  137.  ::: AND... end like... below from the "goto"
  138.  rem endlocal & set "%~1=%arg5%" & set "%~2=%arg6%"
  139.   exit /b %errorlevel%
  140.  
  141.  
  142. ::: calling w/o colon, with 6 args.
  143. :alt6
  144. if %testwell% gtr 2 echo . & echo arg6 is %arg6% & echo .
  145. if %testwell% gtr 2 if not defined arg7 set "arg7=This is alt7, there is no more above this."
  146.   if defined arg7 call :alt7
  147. if %testwell% gtr 2 echo . & echo %arg7% & echo .
  148.  ::: Then finish the :alt6 function below instead of returning back 1 level.
  149.  
  150. if %testwell% gtr 2 pause & echo .
  151.  ::: AND... end like... below from the "goto"
  152.  rem endlocal & set "%~1=%arg5%" & set "%~2=%arg6%"
  153.   exit /b %errorlevel%
  154.  
  155.  
  156. ::: calling w/o colon, with 7 args.
  157. :alt7
  158. if %testwell% gtr 2 echo . & echo arg7 is %arg7% & echo .
  159. if %testwell% gtr 2 if not defined arg8 set "arg8=You've reached alt7, there are no provisions for more than 7 args."
  160. if %testwell% gtr 2 echo . & echo %arg8% & echo .
  161.  ::: Then finish the :alt7 function below instead of returning back 1 level.
  162.  
  163.   ::: AND... end like... below from the "goto"
  164.  rem endlocal & set "%~1=%arg5%" & set "%~2=%arg6%"
  165.   exit /b %errorlevel%
  166.  
  167. ::: End of alt namespace, the below are all isolated functions.
  168.  
  169.  
  170. ::: Reverse a string arg and return reversed string.
  171. :revchars <string revstrReturned, string str2rev>
  172.   setlocal & set str=%~2
  173.   if not defined sizer set sizer="%bats%\string_length.bat"
  174.   if not exist %sizer% echo . & echo %sizer% not found. & echo Error 47 & echo . & pause & echo . & exit /b
  175.   call %sizer% char5 str
  176.   set rev=
  177.   for /l %%n in (1,1,%char5%) do call :forloop1
  178.   endlocal & set "%~1=%rev%"
  179.   exit /b %errorlevel%
  180. :forloop1
  181.     set ltr=%str:~-1%
  182.     set str=%str:~0,-1%
  183.     set "rev=%rev%%ltr%"
  184.     exit /b %errorlevel%
  185.  
  186.  
  187. ::: To remain generic, we'll be using it a lot.
  188. :makeline <string line2Fill, string padchars, string strToPadTo>
  189.   setlocal & set "txt3=%~3" & set "padchars=%~2"
  190.   call :strlen pad "%padchars%"
  191.   call :strlen len "%txt3%"
  192.   if %pad% equ 0 if %len% equ 0 set line=WHO CALLED YOU?
  193.   if %pad% equ 0 if %len% equ 0 (
  194.     echo . & echo %line% & echo .
  195.     pause & echo . & pause & echo . & goto sorry )
  196.   set /a size8=((2 + %len% + %pad% * 2)/%pad%)
  197.   :while
  198.     set /a s8=(%size8% * %pad%)
  199.     set /a g1=(%len% + 2 + (2 * %pad%))
  200.     if %g1% gtr %s8% set /a size8+=1
  201.     if %g1% gtr %s8% goto while
  202.  REM if %code2% equ 7 goto except
  203.  REM if %code2% neq 7
  204.   call :padder line %size8% "%padchars%"
  205.   :sorry
  206.     endlocal & set "%~1=%line%"
  207.     exit /b %errorlevel%
  208.  
  209.  
  210. ::: Pads string var to passed length with sent chars.
  211. :padder <string toRtn, int lengthOfStringReqd, string padchars>
  212.   setlocal & set "padchars=%~3" & set line1=
  213.   for /l %%k in (1, 1, %~2) do ( call :loop3
  214.     if %errorlevel% gtr 0 goto except
  215.   )
  216.   endlocal & set "%~1=%line1%"
  217.   exit /b %errorlevel%
  218. ::: loop body for for-loop above.
  219. :loop3
  220.   set line1=%line1%%padchars%
  221.   exit /b %errorlevel%
  222.  
  223.  
  224. :except
  225.   set /a code2=7 & set t1=The Exception was caught in :padder
  226.   set s1=utility3.bat
  227.   call :exception1 "%t1%" "%s1%" "%errorlevel%" "%code2%"
  228.   exit
  229.  
  230.  
  231. :exception1 <str text, str sender, int error, int code2>
  232.   setlocal & set "t1=%~1" & set "s1=%~2" & set "e1=%~3" & set "c2=%~4"
  233.   if not defined errr set errr="%bats%\ErrorHandling.bat"
  234.   set ch=-/\-\/
  235.   set t2=The ErrorLevel was %e1% and code2 == %c2%
  236.  REM call :middleman "%ch%" "%t2%"
  237.   echo . & if exist %errr% call %errr% "%t1%" "%s1%"
  238.   echo . & pause & echo .
  239.   endlocal & set /a code2=0
  240.   exit /b %errorlevel%
  241.  
  242.  
  243. :getspcs <string spcs2Return, int gap>
  244.   setlocal & set /a "gap=%~2" & set sp=
  245.   for /l %%i in (1, 1, %gap%) do call :addspace
  246.   endlocal & set "%~1=%sp%"
  247.   exit /b %errorlevel%
  248. :addspace
  249.   set nl=
  250.   set "sp=%sp%%nl%"
  251.   exit /b %errorlevel%
  252.  
  253.  
  254. :strlen <int sizeOfstr2rtn, string str2measure>
  255.   setlocal & set str=%~2
  256.   if not defined sizer set sizer="%bats%\string_length.bat"
  257.   if exist %sizer% call %sizer% size str
  258.   if not exist %sizer% echo . & echo Can't find %sizer% & echo .
  259.   endlocal & set "%~1=%size%"
  260.   exit /b
  261.  
  262.  
  263. :overload
  264.   echo .
  265.   echo Years beyond 2147483647 are not calculable by this program,
  266.   echo ask for an update. I'd just have to use another 32 bit register
  267.   echo beyond that, to raise it by another order of this magnitude.
  268.   exit /b
  269.  
  270.  
  271. ::: An All-In-One Function. Though these calls
  272. ::: can still be used separately when desired.
  273. :middleman <str chars, str msg>
  274.   setlocal & set "ch=%~1" & set "ms=%~2"
  275.   call :revchars rv "%ch%"
  276.   call :makeline ln "%ch%" "%ms%"
  277.   call :getgaps g1 g2 "%ln%" "%ch%" "%ms%"
  278.   set "nl=%rv%%g1% %ms% %g2%%rv%" & echo .
  279.   echo %ln% & echo %nl% & echo %ln% & echo .
  280.   endlocal
  281.   exit /b
  282.  
  283.  
  284. ::: default is 2, 1 on ea side of txt. which ^ is in the program already at display of these. So if anything more then we are adding that here.
  285. :getgaps <str gap1, str gap2, str line, str chars, str msg>
  286.   setlocal & set "line=%~3" & set "chars=%~4" & set "msg=%~5"
  287.   call :strlen l3n "%line%"
  288.   call :strlen k4d "%chars%"
  289.   call :strlen m5g "%msg%"
  290.   set /a gap0=(%l3n% - (%m5g% + (2 * %k4d%)) - 2)
  291.   call :equalizer
  292.   if %gap1% gtr 0 call :getspcs pad1 %gap1%
  293.   if %gap2% gtr 0 call :getspcs pad2 %gap2%
  294.   if %gap1% lss 1 set pad1=
  295.   if %gap2% lss 1 set pad2=  
  296.   endlocal & set "%~1=%pad1%" & set "%~2=%pad2%"
  297.   exit /b %errorlevel%
  298.  
  299.  
  300.   :::::::::::::::::::::::::::::::::::::::::::::::::
  301.  ::: NOTE: Inside same domain as :getgaps <==< :::
  302.  ::: if the gap is greater than 2 it can be    :::
  303.  ::: within 1 of being equally divided between :::
  304.  ::: the two sides of the text. by -JpE- 2310  :::
  305.  ::: Therefore, if div by 2 isn't even add 1   :::
  306.  ::: to one side, the 2nd here, & Rock-n-Roll! :::
  307.  :::::::::::::::::::::::::::::::::::::::::::::::::
  308.  
  309. :: given: gap total as gap2 & w/ gap1 init; [above]
  310. :: make as equal as possible. Odd on gap2 if over.
  311. :: Remember: 1 spc on each side is already programatic.
  312. :equalizer
  313.   set /a gap1=0 & set /a gap2=0 & set /a divi=0 & set /a rmdr=0
  314.   if %gap0% gtr 0 set /a divi=(%gap0%/2) & set /a rmdr=(%gap0% %% 2%)
  315.   if %gap0% gtr 0 set /a gap1=%divi% & set /a gap2=%divi%
  316.  ::  if it went evenly we're done. Else... if odd merely... add one to only one side. Herein, the 2nd side or far end of line b4 revchar.
  317.   if %rmdr% gtr 0 set /a gap2+=1
  318.   exit /b %errorlevel%
  319.  
  320.     :: More a conceptual Design challenge than coding.
  321.    :: But if Num went into 2 oddly, AKA unevenly...
  322.    :: we can therefore safely say that it's the
  323.    :: even product above (from the even which is
  324.    :: 1 less than it) plus 1 in each instance,
  325.    :: with the extra 1 on the right, in gap2. (here)
  326.  
  327.       :: design NOTES: 1 is the only possible remainder when dividing odds by 2 as 2 would make another pair. So... 5/2 == 2 w/ rmdr of 1 over 2 or 1/2. Or, with this, unevenly split into 2 + 2+1 == 5 so we don't have to split the character in 2. lol. Now, with divisions by odd numbers > 1, the remainder is greater than that, up to denominator-1. 2/3rds, or 4/5ths, etc. Example, take the year (after Gregorian Calendar in 1753) and divide by 4, if there is no remainder it is a leap year. Magic? No, Math! 1 in 4 years are leap years. Every 4 years. Divide by 4, same same... no remainder, that's the 1. i.e. 3:1 odds it's not leap year.
  328.  
  329.  
  330.  
  331.   ::: 100% -JpE-
  332.  
  333.  
  334.  
  335.  
  336.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement