1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
/************************************************************************/
/* */
/* Unit types and conversions between units. */
/* */
/************************************************************************/
# include "appUtilConfig.h"
# include <string.h>
# include "utilRoman.h"
# include <appDebugon.h>
/************************************************************************/
/* */
/* Convert a number to roman notation. */
/* */
/************************************************************************/
typedef struct RomanPiece
{
int rpValue;
const char * rpString;
int rpStrlen;
} RomanPiece;
static const RomanPiece APP_RomanPieces[]=
{
{ 1000, "m", 1 },
{ 900, "cm", 2 },
{ 500, "d", 1 },
{ 400, "cd", 2 },
{ 100, "c", 1 },
{ 90, "xc", 2 },
{ 50, "l", 1 },
{ 40, "xl", 2 },
{ 10, "x", 1 },
{ 9, "ix", 2 },
{ 5, "v", 1 },
{ 4, "iv", 2 },
{ 1, "i", 1 }
};
int utilRomanString( char * to,
int maxlen,
int n,
int upper )
{
int i;
const RomanPiece * rp;
if ( n <= 0 || n > 3500 )
{ LDEB(n); return -1; }
rp= APP_RomanPieces;
for ( i= 0; i < sizeof(APP_RomanPieces)/sizeof(RomanPiece); rp++, i++ )
{
while( n >= rp->rpValue )
{
if ( rp->rpStrlen > maxlen )
{ LLDEB(rp->rpStrlen,maxlen); return -1; }
strcpy( to, rp->rpString );
if ( upper )
{
while( *to )
{ *to= toupper( *to ); to++; }
}
else{ to += rp->rpStrlen; }
maxlen -= rp->rpStrlen; n -= rp->rpValue;
}
}
return 0;
}
|