Search code examples
ibm-midrangerpgle

RPGLE - How to Convert String to ProperCase / TitleCase


In the RPGLE programming Langause on the IBMi/iseries/AS400 how does one go about converting a character string to ProperCase / TitleCase?

As per usual, I have google my $%# off and ended up Frankensteining my own solution.

This is documented below, as I will in inevitably need to use this proc again. At least I know that google will find a solution for me.

@FutureSelf => Whoomp THERE IT IS!!!


Solution

  •  ?*--------------------------------------------------
     ?* Procedure name: ConverToTitleCase
     ?*--------------------------------------------------
       dcl-proc ConvertToTitleCase export;
         dcl-pi *n  varchar();
           i_String varchar() const;
         end-pi;
    
         dcl-s o_String varchar();
         dcl-s w_Text char();
         dcl-s w_Word char();
         dcl-s p_Word pointer;
         dcl-s p_Last pointer;
    
         dcl-c UPPER const(0);
         dcl-c lower const(1);
    
         dcl-pr GetToken pointer ExtProc('strtok_r');
           String pointer value options(*string);
           Seps   pointer value options(*string);
           Lasts  pointer ;
         end-pr;
         dcl-c BLANK const(' ');
         //-------------------------------------------------
         if %trim(i_String) = *blanks;
           return i_String; //Nothing to Convert;
         endif;
         //-------------------------------------------------
         p_Word = GetToken(ConvertCase(i_String:lower):BLANK:p_Last);
         dow p_Word <> *null;
    
           w_Word = %str(p_Word);
           %subst(w_Word:1:1) = ConvertCase(%subst(w_Word:1:1):UPPER);
           o_String += BLANK + w_Word;
           p_Word = GetToken(*NULL:BLANK:p_Last);
         enddo;
         //-------------------------------------------------
         return o_String;
       end-proc;
       //***************************************************
       dcl-proc ConvertCase;
         dcl-pi *n  varchar();
           i_String varchar() const;
           i_CaseTo int(5)    const;
         end-pi;
    
         dcl-s o_String varchar();
    
         //Control Block for the call to QglConvertCase.
         dcl-ds FormatOfRequestControlBlock;
           ReqType  int(10)  inz(1); //Convert String
           CCSID    int(10)  inz(0); //CCSID of JOB
           CaseTo   int(10)  inz(0); //Default Lower
           Reserved char(10) inz(*ALLX'00');
         end-ds;
    
         dcl-s APIError char(16) inz(*ALLX'00');
    
         dcl-pr QlgCvtCase ExtProc('QlgConvertCase');
           ctrlBlock  LikeDS(FormatOfRequestControlBlock) Const;
           inString   char(65535)   Options(*VARSIZE)     Const;
           OutString  char(65535)   Options(*VARSIZE);
           Length     int(10) Const;
           APIError   like(APIError);
         end-pr;
    
         if %len(%trim(i_String)) <= 0;
           return i_String; //Noting to convert;
         endif;
    
         FormatOfRequestControlBlock.CaseTo = i_CaseTo;
    
         QlgCvtCase(FormatOfRequestControlBlock
                   :i_String
                   :o_String
                   :%len(i_String)
                   :APIError);
    
         return %Subst(o_String:1:%Len(i_String));
       end-proc;
       //***************************************************