English Amiga Board


Go Back   English Amiga Board > Coders > Coders. System > Coders. Scripting

 
 
Thread Tools
Old 04 November 2021, 21:50   #1
malko
Ex nihilo nihil
 
malko's Avatar
 
Join Date: Oct 2017
Location: CH
Posts: 4,856
Lightbulb AmigaOS 1.3 'ccase' command

Have you ever been in the need to change the case of a string ?

You know, you display things on the screen and you want that the input of the user to be correctly formatted... (users may be distracted ...)

This is exactly what the 'ccase' (Change Case) command does.

Three case choices are available :
- " L " switch forces lowercase
- " U " switch forces uppercase
- no switch simply invert the letters case
A QUIET switch is also available for the command to be silent

Of course, strings with space have to be surrounded by quotation marks.

Please mind that only a-z letters are processed. Others characters, like characters with accents, are left untouched.

As usual the result is available in a variable (cCASE<shell_number>) and, cherry on the cake, the string length is also available (cLNGR<shell_number> - not that it is useful here, but the value was available and it costed nothing to make it available through a variable).

NEW VERSION OF THE SCRIPT AVAILABLE BELOW. SEE POST #2

Code:
.key STRING/A,U/S,L/S,QUIET/S
.bra {
.ket }
;---------------------
;initialisation
;-
setenv cccptA{$$} 1
setenv cccptB{$$} 0
setenv ccstr0{$$} "{STRING}"
echo >env:ccstr9{$$}t "" NOLINE
echo >t:rmcr{$$} "1CL//;w"
echo >t:rmet{$$} "pa l//;<;sa//;p;d;w"
echo >t:rmhx{$$} "pa/0A/;2<;sa//;d;p;pa l//;2<;sa//;p;d;A//0x/;w"
;---------------------
;début de boucle : preparation de l'extraction
;-
lab mlk01
setenv <env:cccptA{$$} >NIL: echop{$$}t ?
echo >>env:echop{$$}t " LEN 1 *"{STRING}*"" NOLINE
echo >env:echop{$$} "FIRST " NOLINE
type >>env:echop{$$} env:echop{$$}t
;---------------------
;extraction par caractère & nettoyage
;-
echo <env:echop{$$} >env:exstr1{$$}t ?
edit >NIL: FROM env:exstr1{$$}t TO env:exstr1{$$} WITH t:rmet{$$}
;---------------------
;valeur hexadécimale du caractère extrait & nettoyage
;  miniscules: a=0x61=97 ; z=0x7A=122
;  majuscules: A=0x41=65 ; Z=0x5A=90
;-
type >env:vhstr1{$$}t env:exstr1{$$} OPT h
edit >NIL: FROM env:vhstr1{$$}t TO env:vhstr1{$$} WITH t:rmhx{$$}
;---------------------
;valeur décimale du caractère extrait 
;-
eval <env:vhstr1{$$} >NIL: TO env:vdstr1{$$} lformat %N ?
;---------------------
;test de case 
;-
if VAL $vdstr1{$$} GE 97
    if VAL $vdstr1{$$} NOT GT 122
        if "{L}" NOT EQ "L"
            skip min2maj
        endif
    endif
endif
if VAL $vdstr1{$$} GE 65
    if VAL $vdstr1{$$} NOT GT 90
        if "{U}" NOT EQ "U"
            skip maj2min
        endif
    endif
endif
;---------------------
;on garde tel quel
;-
if VAL $vdstr1{$$} EQ 0
    setenv vhstr1{$$} "0x00"
    setenv <env:vhstr1{$$} >NIL: evalp{$$} ?
    echo >>env:evalp{$$} " + 0x20" NOLINE
else
    setenv <env:vhstr1{$$} >NIL: evalp{$$} ?
    echo >>env:evalp{$$} " + 0x00" NOLINE
endif
skip mlk02
quit
lab min2maj
;---------------------
;préparation du changement de case (minuscule à majuscule) 
;-
setenv <env:vhstr1{$$} >NIL: evalp{$$} ?
echo >>env:evalp{$$} " - 0x20" NOLINE
skip mlk02
lab maj2min
;---------------------
;préparation du changement de case (majuscule à minuscule) 
;-
setenv <env:vhstr1{$$} >NIL: evalp{$$} ?
echo >>env:evalp{$$} " + 0x20" NOLINE
skip mlk02
lab mlk02
;---------------------
;conversion de case (minuscule <-> majuscule) 
;-
eval <env:evalp{$$} >NIL: TO env:cvstr1{$$} lformat "%C" ?
;---------------------
;concaténation de la chaîne convertie
;-
join env:ccstr9{$$}t "env:cvstr1{$$}" AS env:ccstr9{$$}
copy env:ccstr9{$$} env:ccstr9{$$}t CLONE
lab mlk03
;---------------------
;la chaîne originale et celle recréée sont-elles identiques ?
;-
if $ccstr9{$$} NOT EQ $ccstr0{$$}
    ;---------------------
    ;incrémentation du compteur
    ;-
    eval <env:cccptA{$$} >NIL: TO VALUE2 1 OP + TO env:cccptB{$$} ?
    setenv <env:cccptB{$$} >NIL: cccptA{$$} ?
        skip mlk01 BACK
endif
;---------------------
;fin
;-
copy env:ccstr9{$$} TO env:cCASE{$$} CLONE QUIET
setenv <env:cccptA{$$} >NIL: cLNGR{$$} ?
if "{QUIET}" NOT EQ "QUIET"
    echo "  *e[32m$cCASE{$$} : " NOLINE
    getenv cCASE{$$}
    echo "*e[0m" NOLINE
    echo "  *e[32m$cLNGR{$$} : " NOLINE
    getenv cLNGR{$$}
    echo "*e[0m"
endif
run >NIL: delete t:rm(cr|et|hx){$$} env:cccpt(A|B){$$} env:(cc|cv|ex|vd|vh)str(0|1|9){$$}#? env:echop{$$}#? env:evalp{$$}#? QUIET
quit 0
And how it looks :


Last edited by malko; 10 November 2021 at 23:25. Reason: typo
malko is offline  
Old 10 November 2021, 23:24   #2
malko
Ex nihilo nihil
 
malko's Avatar
 
Join Date: Oct 2017
Location: CH
Posts: 4,856
Taking advantage of some edit commands, the upper (U switch) and lower (L switch) case change is now much faster (4 seconds).
The length is now available as a switch (LNG).

If you have any tip to improve it, you are welcome

Code:
ccase_v2 :
.key CHAINE/A,L/S,U/S,LNG/S,QUIET/S
.bra {
.ket }
;---------------------
;initialisation et contrôle des interrupteurs
;-
setenv ccchn0{$$} "{CHAINE}"
run >NIL: delete env:ccLNG{$$} QUIET
if "{LNG}" EQ "LNG"
   echo >t:ccrmlt{$$} "27#;5>;sa//;d;w"
endif
if "{L}" EQ "L"
   ;tout en minuscules
   echo >t:ccmamip{$$} "254$;w"
	skip mlk05
endif
if "{U}" EQ "U"
   ;tout en majuscules
   echo >t:ccmamip{$$} "254%;w"
	skip mlk05
endif
;---------------------
;pas d'interrupteur de case spécifié
;-
setenv cccptA{$$} 1
setenv cccptB{$$} 0
echo >env:ccchn7{$$} "" NOLINE
echo >t:ccrmcr{$$} "1CL//;w"
echo >t:ccrmet{$$} "pa l//;<;sa//;p;d;w"
echo >t:ccrmhx{$$} "pa/0A/;2<;sa//;d;p;pa l//;2<;sa//;p;d;A//0x/;w"
lab mlk07
;---------------------
;début de boucle : preparation de l'extraction
;-
setenv <env:cccptA{$$} >NIL: ccechot{$$} ?
echo >>env:ccechot{$$} " LEN 1 *"{CHAINE}*"" NOLINE
echo >env:ccechop{$$} "FIRST " NOLINE
type >>env:ccechop{$$} env:ccechot{$$}
;---------------------
;extraction par caractère & nettoyage
;-
echo <env:ccechop{$$} >env:ccext0{$$} ?
edit >NIL: FROM env:ccext0{$$} TO env:ccext1{$$} WITH t:ccrmet{$$}
;---------------------
;valeur hexadécimale du caractère extrait & nettoyage
;  miniscules: a=0x61=97 ; z=0x7A=122
;  majuscules: A=0x41=65 ; Z=0x5A=90
;-
type >env:ccvhe0{$$} env:ccext1{$$} OPT h
edit >NIL: FROM env:ccvhe0{$$} TO env:ccvhe1{$$} WITH t:ccrmhx{$$}
;---------------------
;valeur décimale du caractère extrait 
;-
eval <env:ccvhe1{$$} >NIL: TO env:ccvde1{$$} lformat %N ?
;---------------------
;test de case 
;-
if VAL $ccvde1{$$} GE 97
	if VAL $ccvde1{$$} NOT GT 122
		if "{L}" NOT EQ "L"
			skip min2maj
		endif
	endif
endif
if VAL $ccvde1{$$} GE 65
	if VAL $ccvde1{$$} NOT GT 90
		if "{U}" NOT EQ "U"
			skip maj2min
		endif
	endif
endif
;---------------------
;on garde tel quel car autre caractère que (a-z|A-Z)
;-
if VAL $ccvde1{$$} EQ 0
	setenv ccvhe1{$$} "0x00"
	setenv <env:ccvhe1{$$} >NIL: ccevlp{$$} ?
	echo >>env:ccevlp{$$} " + 0x20" NOLINE
else
	setenv <env:ccvhe1{$$} >NIL: ccevlp{$$} ?
	echo >>env:ccevlp{$$} " + 0x00" NOLINE
endif
skip mlk08
lab min2maj
;---------------------
;préparation du changement de case (minuscule à majuscule) 
;-
setenv <env:ccvhe1{$$} >NIL: ccevlp{$$} ?
echo >>env:ccevlp{$$} " - 0x20" NOLINE
skip mlk08
lab maj2min
;---------------------
;préparation du changement de case (majuscule à minuscule) 
;-
setenv <env:ccvhe1{$$} >NIL: ccevlp{$$} ?
echo >>env:ccevlp{$$} " + 0x20" NOLINE
lab mlk08
;---------------------
;conversion de case (minuscule <-> majuscule) 
;-
eval <env:ccevlp{$$} >NIL: TO env:cccvt1{$$} lformat "%C" ?
;---------------------
;concaténation de la chaîne convertie
;-
join env:ccchn7{$$} env:cccvt1{$$} AS env:ccchn9{$$}
copy env:ccchn9{$$} env:ccchn7{$$} CLONE QUIET
lab mlk09
;---------------------
;la chaîne originale et recréée sont-elles identiques ?
;-
if $ccchn7{$$} NOT EQ $ccchn0{$$}
	;---------------------
	;incrémentation du compteur
	;-
	eval <env:cccptA{$$} >NIL: TO VALUE2 1 OP + TO env:cccptB{$$} ?
	setenv <env:cccptB{$$} >NIL: cccptA{$$} ?
	echo "°" NOLINE
	skip mlk07 BACK
endif
echo "*e[1A*N*e[K" NOLINE
skip mlk06
lab mlk05
;---------------------
;transformation en bloc : tout en majuscules ou tout en minuscules
;-
edit >NIL: FROM env:ccchn0{$$} TO env:ccchn7{$$} with t:ccmamip{$$}
if "{LNG}" EQ "LNG"
   list >NIL: env:ccchn0{$$} TO env:cccptA{$$}
   edit >NIL: FROM env:cccptA{$$} TO env:cccptB{$$} WITH t:ccrmlt{$$}
   if $cccptB{$$} EQ empty
      setenv cccptA{$$} 0
   else
      eval <env:cccptB{$$} >NIL: TO env:cccptA{$$} ?
   endif
endif
lab mlk06
;---------------------
;fin
;-
copy env:ccchn7{$$} TO env:cCASE{$$} CLONE QUIET
if "{QUIET}" NOT EQ "QUIET"
	echo "  *e[32m$cCASE{$$} : " NOLINE
	getenv cCASE{$$}
	echo "*e[0m" NOLINE
if "{LNG}" EQ "LNG"
		setenv <env:cccptA{$$} >NIL: ccLNG{$$} ?
		echo "  *e[32m$ccLNG{$$} : " NOLINE
		getenv ccLNG{$$}
		echo "*e[0m" NOLINE
endif
echo ""
endif
run >NIL: delete t:ccrm(lt|cr|et|hx){$$} t:cc(mamip|echot|echop){$$} env:cc(chn|ext|vhe|vde|cvt)(0|1|7|9){$$} env:cccpt(A|B){$$} env:cc(echot|echop|evlp){$$} QUIET
quit 0
malko is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
AmigaOS 1.3 ' ls ' command malko Coders. Scripting 10 19 November 2021 21:09
AmigaOS 1.3 ' strlng ' command malko Coders. Scripting 12 08 November 2021 01:25
AmigaOS 1.3 'wmn' command malko Coders. Scripting 0 01 November 2021 21:25
Is there a WB command at all that can... MethodGit request.Other 43 10 October 2010 09:06
What is the WB command that..... MethodGit request.Other 4 08 October 2010 04:21

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:55.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.26159 seconds with 16 queries