/*******************************************/ /* */ /* File: tlfroms.h */ /* Purpose: Format convering routines */ /* */ /* Author: Sfiligoi Igor */ /* */ /* Last modified: 03.04.1996 */ /* */ /*******************************************/ /***************************** Natural formats supported: normal 234 compact 0c23cC 0C23_^As hex 0x12Ea 0X23da octal 0o235 0O234 binary 0b10011 0B1101 ******************************/ /***************************** Real formats supported: scientific .23e12 12.34E0xa fixed point 13.456 binary real 0f1001e23 0F001E0b11 all natural numbers ******************************/ /******************************************************** Compact number definition: 6-bit case sensitive 0 0 16 g 32 w 48 M 1 1 17 h 33 x 49 N 2 2 18 i 34 y 50 O 3 3 19 j 35 z 51 P 4 4 20 k 36 A 52 Q 5 5 21 l 37 B 53 R 6 6 22 m 38 C 54 S 7 7 23 n 39 D 55 T 8 8 24 o 40 E 56 U 9 9 25 p 41 F 57 V 10 a 26 q 42 G 58 W 11 b 27 r 43 H 59 X 12 c 28 s 44 I 60 Y 13 d 29 t 45 J 61 Z 14 e 30 u 46 K 62 _ 15 f 31 v 47 L 63 ^ *********************************************************/ /*********************************************** Binary real format definition(by example): binary format normal 0 0.5 = 2^(-1) 1 0.75 = 2^(-1) + 1*2^(-2) 0e-1 0.5 * 2^(-1)=0.25 Attention: 0.0 cannot be rapresented by binary real format ************************************************/ #ifndef TL_FORMS_H #define TL_FORMS_H #define TLF_OK 0 #define TLF_ERROR 1 /*************************************************************************/ /* String to number */ /*************************************************************************/ #define TLF_EMPTY 2 /*************************** Natural numbers *****************************/ /* convert nat string in natural number */ /* must be in binary format 0b format*/ /* returns TLF_OK iff no error */ int str2binary(char *strnat, /* IN */ unsigned int *nrnat); /* OUT */ /* convert nat string in natural number */ /* must be in octal format 0o format*/ /* returns TLF_OK iff no error */ int str2octal(char *strnat, /* IN */ unsigned int *nrnat); /* OUT */ /* convert nat string in natural number */ /* must be in hex format no 0x format*/ /* returns TLF_OK iff no error */ int str2hex(char *strnat, /* IN */ unsigned int *nrnat); /* OUT */ /* convert nat string in natural number */ /* must be in compact format, no 0c prefix */ /* returns TLF_OK iff no error */ int str2compact(char *strnat, /* IN */ unsigned int *nrnat); /* OUT */ /* convert nat string in natural number */ /* returns TLF_OK iff no error */ int str2nat(char *strnat, /* IN */ unsigned int *nrnat); /* OUT */ /*************************** Integer numbers *****************************/ /* convert integer string in integer number */ /* returns TLF_OK iff no error */ int str2integer(char *strint, /* IN */ int *nrinteger); /* OUT */ /***************************** Real numbers ******************************/ /* convert unsigned real string in unsigned real number */ /* in binary real format, no 0f... */ /* attention, 0 cannot be rapresented by binary real format */ /* returns TLF_OK iff no error */ int str2bureal(char *strureal, /* IN */ double *nrureal); /* OUT */ /* convert unsigned real string in unsigned real number */ /* returns TLF_OK iff no error */ int str2ureal(char *strureal, /* IN */ double *nrureal); /* OUT */ /* convert real string in real number */ /* returns TLF_OK iff no error */ int str2real(char *strreal, /* IN */ double *nrreal); /* OUT */ /*************************** Intreal numbers *****************************/ /* convert unsigned intreal string in unsigned intreal number */ /* round to the nearest integer, if necessary */ /* returns TLF_OK iff no error */ int str2uintreal(char *struintreal, /* IN */ double *nruintreal); /* OUT */ /* convert intreal string in intreal number */ /* returns TLF_OK iff no error */ int str2intreal(char *strintreal, /* IN */ double *nrintreal); /* OUT */ /*************************************************************************/ /* Number to String */ /*************************************************************************/ /*************************** Natural numbers *****************************/ /* convert natural number in normal format */ /* strnat must be allocated with enough space. */ /* i.e.: 11 bytes are enough for 32 bit */ /* returns number of digits */ int nat2str(unsigned int nrnat, /* IN */ char *strnat); /* OUT */ /* convert natural number in binary format */ /* strnat must be allocated with enough space. */ /* i.e.: 33 bytes are enough for 32 bit */ /* returns number of digits */ int binary2str(unsigned int nrnat, /* IN */ char *strnat); /* OUT */ /* convert natural number in octal format */ /* strnat must be allocated with enough space. */ /* i.e.: 12 bytes are enough for 32 bit */ /* returns number of digits */ int octal2str(unsigned int nrnat, /* IN */ char *strnat); /* OUT */ /* convert natural number in hex format */ /* strnat must be allocated with enough space. */ /* i.e.: 9 bytes are enough for 32 bit */ /* returns number of digits */ int hex2str(unsigned int nrnat, /* IN */ char *strnat); /* OUT */ /* convert natural number in compact format */ /* strnat must be allocated with enough space. */ /* i.e.: 7 bytes are enough for 32 bit */ /* returns number of digits */ int compact2str(unsigned int nrnat, /* IN */ char *strnat); /* OUT */ /*************************** Integer numbers *****************************/ /* convert integer number in normal format */ /* strint must be allocated with enough space. */ /* i.e.: 12 bytes are enough for 32 bit */ /* returns number of digits */ int int2str(int nrint, /* IN */ char *strint); /* OUT */ /***************************** Real numbers ******************************/ #define TLF_MAXFRACT 16 /* convert real number in scientific format */ /* if maxfract<=0, maxfract = TLF_MAXFRACT */ /* strreal must be allocated with enough space.(1+maxfract+7) */ /* returns number of digits */ int real2str(double nrreal, /* IN */ int maxfract, /* IN */ char *strreal); /* OUT */ /* convert real number in fixed format */ /* if maxint<=0, do not limit int length */ /* if maxfract<0, maxfract = TLF_MAXFRACT, else if ==0, set to 1 */ /* strreal must be allocated with enough space.(1+maxint+1+maxfract+5+1) */ /* returns number of digits */ int freal2str(double nrreal, /* IN */ int maxint, /* IN */ int maxfract, /* IN */ char *strreal); /* OUT */ /* convert real number in binary real format prefixed by 0f*/ /* strreal must be allocated with enough space. */ /* i.e.: 72 bytes are enough for 64-bit double */ /* returns number of digits */ int breal2str(double nrreal, /* IN */ char *strreal); /* OUT */ /*************************** Intreal numbers *****************************/ /* convert real number in integer format */ /* if maxlength<=0, return full integer, else if <6, set to 6 */ /* strreal must be allocated with enough space.(maxlength+1) */ /* returns number of digits */ int intreal2str(double nrintreal, /* IN */ int maxlength, /* IN */ char *strintreal); /* OUT */ /*************************************************************************/ /* String to Boolean */ /*************************************************************************/ /* convert Boolean string in boolean(char) */ /* i.e.: False ==0, true >0 */ /* returns TLF_OK iff no error */ int str2bool(char *strbool, /* IN */ char *bool); /* OUT */ /*************************************************************************/ /* Boolean to String */ /*************************************************************************/ /* convert Boolean nr. to True/False */ /* use C style boolean (False==0, true>0 */ /* strbool must be allocated with enough space.(6 chars) */ void bool2str(char bool, /* IN */ char *strbool); /* OUT */ /* convert Boolean nr. to T/F */ /* use C style boolean (False==0, true>0 */ /* strbool must be allocated with enough space.(2 chars) */ void cbool2str(char bool, /* IN */ char *strbool); /* OUT */ /* convert Boolean nr. to 1/0 */ /* use C style boolean (False==0, true>0 */ /* strbool must be allocated with enough space.(2 chars) */ void nbool2str(char bool, /* IN */ char *strbool); /* OUT */ /*************************************************************************/ /* String to Pointer */ /*************************************************************************/ /* convert pointer string in void * */ /* must be a string returned by pointer2str */ /* returns TLF_OK iff no error */ int str2pointer(char *strpoint, /* IN */ void * *data); /* OUT */ /*************************************************************************/ /* Pointer to String */ /*************************************************************************/ /* convert void * in pointer string */ /* strbool must be allocated with enough space.(10 chars on 32bit systems) */ /* returns number of digits */ int pointer2str(void *data, /* IN */ char *strpoint); /* OUT */ /*************************************************************************/ /* String to C String */ /*************************************************************************/ /* convert a string to a c string */ /* allocates new space, must be freed by the user*/ char *str2cstr(char *oldstr); #endif /* TL_FORMS_H */