UREVERSE C Implementation

Note:

/*                                                                                 */
/* Sample User Written Routine in C ...                                            */
/* UREVERSE: Unicode UTF-8 capable string reversing routine                        */
/*                                                                                 */
/* Typical usage:                                                                  */
/* -SET &STRING = 'abcd' ;                                                         */
/* -SET &RSTRING = UREVERSE(&STRING,&STRING.LENGTH,&FOCCODEPAGE,A&STRING.LENGTH) ; */
/* -TYPE Reverse of &STRING is &RSTRING                                            */
/* Note: &FOCCODEPAGE is standard amper variable for server code page              */
/*                                                                                 */
/* Servers using the Unicode 65002 page are effectively UTF-EBCDIC and beyond      */
/* the scope of this simple sample. Customer implementations should follow the     */
/* information at http://www.unicode.org/reports/tr16 when using the 65002         */
/* UTF-EBCDIC code page.                                                           */
#include <stdio.h>
#include <stdlib.h>
void ureverse( char *instr, double *charsize, double *codepage, char *outstr )
{
  unsigned short codepg = (unsigned short)*codepage;
  int            csize = (int)*charsize;
  int            bsize, offset, clen, ccnt;
  unsigned char *cptr;
  char          *foccodepage;
  /* External var override, normally var is not set. If trying to make an       */
  /* existing routine Unicode compliant without passing an extra var, this      */
  /* method can be used to get a code page value if following is added to       */
  /* the server profile (edasprof) or other application code:                   */
  /* -SET &RC = FPUTENV(11,'FOCCODEPAGE',&FOCCODEPAGE.LENGTH,&FOCCODEPAGE,D8) ; */
  foccodepage = getenv("FOCCODEPAGE");
  if( foccodepage != NULL )
  {
    codepg = atoi( foccodepage );
  }
  if( codepg == 65001 ) /* Unicode reference number used by server for UTF-8 */
  {
    /* Unicode UTF-8 */
    /* Pass 1. Calculate the byte length of 'instr' in character length 'charsize' */
    /* Pass 2. Copy each character from 'instr' to 'outstr' in reverse             */
    bsize = csize * 3; /* maximum byte size */
    for( ccnt = offset = 0; ccnt < csize && offset < bsize; ccnt++, offset += clen )
    {
      cptr = (unsigned char *)&instr[offset];
      if(      *cptr < 0x80 )  clen = 1;
      else if( *cptr < 0xE0 )  clen = 2;
      else                     clen = 3;
    }
    bsize = offset; /* actual byte size in utf-8 for charsize */
    for( offset = 0; offset < bsize; offset += clen )
    {
      cptr = (unsigned char *)&instr[offset];
      if(      *cptr < 0x80 )  clen = 1;
      else if( *cptr < 0xE0 )  clen = 2;
      else                     clen = 3;
      memcpy( &outstr[bsize - offset - clen ], cptr, clen );
    }
  }
  else
  {
    /* Non-Unicode */
    /* Copy each character from 'instr' to 'outstr' in reverse */
    for( offset = 0; offset < csize; offset++ )
    {
      outstr[csize - offset - 1] = instr[offset];
    }
  }
}

iWay Software