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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
/************************************************************************/
/* */
/* Unit types and conversions between units. */
/* */
/************************************************************************/
# include "appUtilConfig.h"
# include <string.h>
# include "utilBase26.h"
# include <appDebugon.h>
/************************************************************************/
/* */
/* Convert a number to base 26. Like in Excel column headers. */
/* 1->A; 26->Z; 27->AA; 28->AB &c. */
/* */
/* 1) Produce letters in the opposite order. */
/* 2) Terminate. */
/* 3) Swap. */
/* */
/************************************************************************/
static const char UTIL_Base26LowerDigits[]= "abcdefghijklnmopqrstuvwxyz";
static const char UTIL_Base26UpperDigits[]= "ABCDEFGHIJKLNMOPQRSTUVWXYZ";
int utilBase26String( char * target,
int maxlen,
int n,
int upper )
{
const char * d;
int done= 0;
int i;
if ( n <= 0 )
{ LDEB(n); return -1; }
if ( upper )
{ d= UTIL_Base26UpperDigits; }
else{ d= UTIL_Base26LowerDigits; }
n--;
/* 1 */
while( n >= 0 )
{
if ( maxlen == 0 )
{ LLDEB(n,maxlen); return -1; }
target[done++]= d[n % 26];
maxlen--;
n /= 26; n--;
}
/* 2 */
target[done]= '\0';
/* 3 */
for ( i= 0; i < done/ 2; i++ )
{
char s;
s= target[i]; target[i]= target[done-i-1]; target[done-i-1]= s;
}
return 0;
}
int utilBase26Int( const char * from )
{
int rval= 0;
const char * dig;
while( isspace( *from ) )
{ from++; }
if ( ! *from )
{ SDEB(from); return -1; }
while( *from )
{
if ( isupper( *from ) )
{
dig= strchr( UTIL_Base26UpperDigits, *from );
if ( ! dig )
{ SDEB(from); return -1; }
rval= 26* rval+ ( dig- UTIL_Base26UpperDigits );
}
else{
dig= strchr( UTIL_Base26LowerDigits, *from );
if ( ! dig )
{ SDEB(from); return -1; }
rval= 26* rval+ ( dig- UTIL_Base26LowerDigits );
}
rval++;
from++;
}
return rval;
}
|