Input/Output and String Functions
v3InputSyntax#include "cosmio.h" u64 v3Input( u8 * buffer, u64 length, u32 echo ); DescriptionRead length bytes from stdin into the buffer. If echo is V3_IO_NOECHO then the typed characters will not be echoed in any way (for password entry etc), otherwise echo should be V3_IO_ECHO. The buffer will not be terminated with a 0, becasue it is not a string. Usually used with a length of 1. Return ValuesNumber of bytes read, 0xFFFFFFFFFFFFFFFF (-1) indicates EOF. ErrorsNone. Exampleu8 * buffer; u64 size; u64 len; u64 eof; /* ... */ _V3_SET64( size, 00000000, 00000001 ); _V3_SET64( eof, FFFFFFFF, FFFFFFFF ); v3PrintA( "Hit a key: " ); len = v3Input( buffer, size, V3_IO_ECHO ); if ( ( v3u64Eq( len, eof ) ) || ( !v3u64Eq( len, size ) ) ) { /* EOF reached */ } v3InputASyntax#include "cosmio.h" u64 v3InputA( ascii * buffer, u64 max_chars, u32 echo ); DescriptionReads up to max_chars-1 ascii characters from standard input device until EOF or '\n' is reached. buffer will always be a terminated string if input is read. If echo is V3_IO_NOECHO then the typed characters will not be echoed in any way (for password entry etc), otherwise echo should be V3_IO_ECHO. Any input beyond max_chars-1 will be discarded, so use a large enough buffer. Return ValuesNumber of characters input, 0xFFFFFFFFFFFFFFFF (-1) indicates EOF. ErrorsNone. Exampleascii * buffer; u64 size; u64 len; u64 eof_length; /* ... */ _V3_SET64( size, 00000000, 000000FF ); len = v3InputA( buffer, size, V3_IO_ECHO ); _V3_SET64( eof_length, FFFFFFFF, FFFFFFFF ); if ( v3u64Eq( len, eof_length ) ) { /* EOF */ } This will read up to size-1 characters into buffer from standard input. v3InputUSyntax#include "cosmio.h" u64 v3InputU( unicode * buffer, u64 max_chars, u32 echo ); DescriptionReads up to max_chars-1 unicode characters from standard input device until EOF or '\n' is reached. buffer will always be a terminated string if input is read. If echo is V3_IO_NOECHO then the typed characters will not be echoed in any way (for password entry etc), otherwise echo should be V3_IO_ECHO. Return ValuesNumber of characters input, 0xFFFFFFFFFFFFFFFF (-1) indicates EOF. ErrorsNone. Exampleunicode * buffer; u64 size; u64 len; u64 eof_length; /* ... */ _V3_SET64( size, 00000000, 000000FF ); len = v3InputU( buffer, size, V3_IO_ECHO ); _V3_SET64( eof_length, FFFFFFFF, FFFFFFFF ); if ( v3u64Eq( len, eof_length ) ) { /* EOF */ } This will read up to size-1 characters into buffer from standard input. v3StrLengthASyntax#include "cosmio.h" u64 v3StrLengthA( const ascii * string ); DescriptionGet the length of the ascii string pointed to by string. Return ValuesThe length of the ascii string in characters. ErrorsNone. Exampleascii * string; u64 size; u64 length; _V3_SET64( size, 00000000, 00000400 ); string = v3MemAlloc( size, (u32) 0 ); string = (ascii *) "A 27 character test string."; length = v3StrLengthA( string ); v3StrLengthUSyntax#include "cosmio.h" u64 v3StrLengthU( const unicode * string ); DescriptionGet the length of the unicode string pointed to by string. Return ValuesThe length of the unicode string in characters. ErrorsNone. Exampleunicode string[256]; u64 size; u64 length; _V3_SET64( size, 00000000, 00000100 ); if ( v3StrCopyUA( string, (ascii *) "A 27 character test string.", size ) != V3_PASS ) { /* Error */ } length = v3StrLengthU( string ); v3StrCopyASyntax#include "cosmio.h" s32 v3StrCopyA( ascii * dest, const ascii * src, u64 max_chars ); DescriptionCopy a string of up to max_chars-1 ascii characters from src to dest, including the terminating '\0'. If the src string is longer then max_chars-1 the function will fail and do nothing. Return ValuesV3_PASS on success, or V3_FAIL on failure. ErrorsPossible causes of an failure:
Exampleascii string[256]; ascii copy[256]; u64 size; /* ... */ if ( v3StrCopyA( copy, string, size ) != V3_PASS ) { /* Error */ } v3StrCopyUSyntax#include "cosmio.h" s32 v3StrCopyU( unicode * dest, const unicode * src, u64 max_chars ); DescriptionCopy a string of up to max_cahrs-1 unicode characters from src to dest, including the terminating '\0'. If the src string is longer then max_chars-1 the function will fail and do nothing. Return ValuesV3_PASS on success, or V3_FAIL on failure. ErrorsPossible causes of an failure:
Exampleunicode string[256]; unicode copy[256]; u64 size; /* ... */ if ( v3StrCopyU( copy, string, size ) != V3_PASS ) { /* Error */ } v3StrCopyUASyntax#include "cosmio.h" s32 v3StrCopyUA( unicode * dest, const ascii * src, u64 max_chars ); DescriptionConverts the ascii string to unicode using the trivial mapping, up to and including the terminating '\0'. Only strings up to max_chars-1 characters long will be converted. Return ValuesV3_PASS on success, or V3_FAIL on failure. ErrorsPossible causes of an failure:
Exampleunicode buffer[256]; u64 size; _V3_SET64( size, 00000000, 00000100 ); /* ... */ if ( v3StrCopyUA( buffer, (ascii *) "Test string", size ) != V3_PASS ) { /* Error */ } v3StrAppendASyntax#include "cosmio.h" s32 v3StrAppendA( ascii * stringA, const ascii * stringB, u64 max_chars ); DescriptionAppends the ascii stringB to the end of stringA, only if the sum of the string lengths is less than max_chars-1. Return ValuesV3_PASS on success, or V3_FAIL on failure. ErrorsPossible causes of an failure:
Exampleascii buffer[256]; u64 size; _V3_SET64( size, 00000000, 00000100 ); /* ... */ if ( v3StrAppendA( buffer, (ascii *) "Test string", size ) != V3_PASS ) { /* Error */ } v3StrAppendUSyntax#include "cosmio.h" s32 v3StrAppendU( unicode * stringA, const unicode * stringB, u64 max_chars ); DescriptionAppends the unicode stringB to the end of stringA, only if the sum of the string lengths is less than max_chars-1 Return ValuesV3_PASS on success, or V3_FAIL on failure. ErrorsPossible causes of an failure:
Exampleunicode buffer[256]; unicode buffer1[256]; u64 size; _V3_SET64( size, 00000000, 00000100 ); /* ... */ if ( v3StrCopyUA( buffer1, (ascii *) "Test string", size ) != V3_PASS ) { /* Error */ } if ( v3StrAppendU( buffer, buffer1, size ) != V3_PASS ) { /* Error */ } v3StrCmpASyntax#include "cosmio.h" s32 v3StrCmpA( const ascii * stringA, const ascii * stringB, u64 max_chars ); DescriptionCompare the two ascii strings, up to max_chars or the end of one string. Return Values0 if the strings are identical, a postive number if the ascii code of the first different character in stringA is greater than the ascii code of that character in stringB, or a negative number if the ascii code of the first different character in stringA is less than the ascii code of that character in stringB. If any parameter is NULL/0 then it returns -1 unless stringA == stringB. ErrorsNone. Exampleascii stringa[128]; ascii stringb[128]; u64 max; /* set stringa and stringb to something */ if ( v3StrCmpA( stringa, stringb, max ) != 0 ) { /* Strings do not match */ } v3StrCmpUSyntax#include "cosmio.h" s32 v3StrCmpU( const unicode * stringA, const unicode * stringB, u64 max_chars ); DescriptionCompare the two unicode strings, up to max_chars or the end of one string. Return Values0 if the strings are identical, a postive number if the ascii code of the first different character in stringA is greater than the ascii code of that character in stringB, or a negative number if the ascii code of the first different character in stringA is less than the ascii code of that character in stringB. If any parameter is NULL/0 then it returns -1 unless stringA == stringB. ErrorsNone. Exampleunicode stringa[128]; unicode stringb[128]; u64 max; /* set stringa and stringb to something */ if ( v3StrCmpU( stringa, stringb, max ) != 0 ) { /* Strings do not match */ } v3StrCharASyntax#include "cosmio.h" ascii * v3StrCharA( const ascii * string, ascii match_char, u64 max_chars ); DescriptionLocate match_char in the string, which is up to max_chars long. Return ValuesA pointer to the first ascii character matching match_char in the string, or NULL if it does not occur in the string. ErrorsNone. Exampleascii stringa[128]; ascii * found; u64 max; /* read in string */ if ( ( found = v3StrCharA( stringa, 'a', max ) ) != NULL ) { /* the character 'a' was found in the string */ } v3StrCharUSyntax#include "cosmio.h" unicode * v3StrCharU( const unicode * string, unicode match_char, u64 max_chars ); DescriptionLocate match_char in the string, which is up to max_chars long. Return ValuesA pointer to the first unicode character matching match_char in the string, or NULL if it does not occur in the string. ErrorsNone. Exampleunicode stringa[128]; unicode * found; u64 max; /* read in string */ if ( ( found = v3StrCharU( stringa, (unicode) 'a', max ) ) != NULL ) { /* the character 'a' was found in the string */ } v3StrStrASyntax#include "cosmio.h" ascii * v3StrStrA( const ascii * string, const ascii * substring, u64 max_chars ); DescriptionLocate substring in the string, which is up to max_chars-1 long. Return ValuesA pointer to the first match of substring in string, or NULL if it does not occur in the string. ErrorsNone. Examplev3StrStrUSyntax#include "cosmio.h" unicode * v3StrStrU( const unicode * string, const unicode * substring, u64 max_chars ); DescriptionLocate substring in the string, which is up to max_chars-1 long. Return ValuesA pointer to the first match of substring in string, or NULL if it does not occur in the string. ErrorsNone. Examplev3{integral type}ASyntax#include "cosmio.h" s32 v3u32A( u32 * result, ascii ** end, const ascii * string, u32 radix ); s32 v3s32A( s32 * result, ascii ** end, const ascii * string, u32 radix ); s32 v3u64A( u64 * result, ascii ** end, const ascii * string, u32 radix ); s32 v3s64A( s64 * result, ascii ** end, const ascii * string, u32 radix ); s32 v3u128A( u128 * result, ascii ** end, const ascii * string, u32 radix ); s32 v3s128A( s128 * result, ascii ** end, const ascii * string, u32 radix ); DescriptionConvert the ascii string written in radix to the number type. Numbers are of the form: [space*][+|-][0|0x|0X]{0-9a-zA-Z}+ radix must be 2-36 or 0. If radix is 0, numbers starting with "0x" or "0X" will be read as base 16, numbers starting with 0 will be interpreted as base 8, and all others will be base 10. If end is not NULL, it will be set to the character after the last character used in the number. For u8 and u16 types use the u32 function and typecast the result. Note that use of radixes other then 2, 8, 10, or 16 are generally useless. Return ValuesSets result to the number and returns V3_PASS on success, or sets result to 0 and returns V3_FAIL on failure. ErrorsNone. Examplev3{integral type}USyntax#include "cosmio.h" s32 v3u32U( u32 * result, unicode ** end, const unicode * string, u32 radix ); s32 v3s32U( s32 * result, unicode ** end, const unicode * string, u32 radix ); s32 v3u64U( u64 * result, unicode ** end, const unicode * string, u32 radix ); s32 v3s64U( s64 * result, unicode ** end, const unicode * string, u32 radix ); s32 v3u128U( u128 * result, unicode ** end, const unicode * string, u32 radix ); s32 v3s128U( s128 * result, unicode ** end, const unicode * string, u32 radix ); DescriptionConvert the unicode string written in radix to the number type. Numbers are of the form: [space*][+|-][0|0x|0X]{0-9a-zA-Z}+ Numbers may be in any language form supported by unicode. radix must be 2-36 or 0. If radix is 0, numbers starting with "0x" or "0X" will be read as base 16, numbers starting with 0 will be interpreted as base 8, and all others will be base 10. If end is not NULL, it will be set to the character after the last character used in the number. For u8 and u16 types use the u32 function and typecast the result. Note that use of radixes other then 2, 8, 10, or 16 are generally useless. Return ValuesSets result to the number and returns V3_PASS on success, or sets result to 0 and returns V3_FAIL on failure. ErrorsNone. Examplev3{float type}ASyntax#include "cosmio.h" s32 v3f32A( f32 * result, ascii ** end, const ascii * string ); s32 v3f64A( f64 * result, ascii ** end, const ascii * string ); DescriptionConvert the ascii base-10 string to a floating point number. Numbers are of the form: [space*][+|-]{{0-9}+[.{0-9}*]|.{0-9}+}[{e|E}[+|-]{0-9}+] If end is not NULL, it will be set to the character after the last character used in the number. Return ValuesSets result to the number and returns V3_PASS on success, or sets result to +/- HUGE_VAL if the number was too large or 0 if the string wasn't a number and returns V3_FAIL on failure. ErrorsNone. Examplev3{float type}USyntax#include "cosmio.h" s32 v3f32U( f32 * result, unicode ** end, const unicode * string ); s32 v3f64U( f64 * result, unicode ** end, const unicode * string ); DescriptionConvert the unicode base-10 string to a floating point number. Numbers are of the form: [space*][+|-]{{0-9}+[.{0-9}*]|.{0-9}+}[{e|E}[+|-]{0-9}+] Digits can be the base-10 digits of any language that unicode supports. If end is not NULL, it will be set to the character after the last character used in the number. Return ValuesSets result to the number and returns V3_PASS on success, or sets result to +/- HUGE_VAL if the number was too large or 0 if the string wasn't a number and returns V3_FAIL on failure. ErrorsNone. Examplev3PrintASyntax#include "cosmio.h" u64 v3PrintA( const ascii * format, ... ); DescriptionPrints the formatted ascii text to the standard output device. All output buffers will be flushed before return. %[width][.prec]type_char [width]
Return ValuesNumber of characters output. ErrorsNone. Exampleascii * output; void * foo; u64 size; _V3_SET64( size, 00000000, 000000FF ); output = v3MemAlloc( size, (u32) V3_MEM_NORMAL ); /* ... */ v3PrintA( (ascii *) "Error: %.*s\n", v3u32u64( size ), output ); v3PrintA( (ascii *) "Size: %04v bytes\n", size ); v3PrintA( (ascii *) "foo points to: %p\nAddress of size is: %p\n", foo, &size ); This would print something similar to (on a 32-bit machine): Error: Timeout -- try again later Size: 0255 bytes foo points to: 08049860 Address of size is: 7FFFF7A8 v3PrintUSyntax#include "cosmio.h" u64 v3PrintU( const unicode * format, ... ); DescriptionPrints the formatted unicode text to the standard output device. All output buffers will be flushed before return. See v3PrintA for format usage. Return ValuesNumber of characters output. ErrorsNone. Exampleunicode string[256]; unicode * output; void * foo; u64 size; _V3_SET64( size, 00000000, 00000100 ); output = v3MemAlloc( v3u64Lsh( size, 1 ), (u32) V3_MEM_NORMAL ); /* ... */ if ( v3StrCopyUA( string, (ascii *) "Error: %.*s\n", size ) != V3_PASS ) { /* Error */ } v3PrintU( string, v3u32u64( size ), output ); if ( v3StCopyUA( string, (ascii *) "Size: %04v bytes\n", size ) != V3_PASS ) { /* Error */ } v3PrintU( string, size ); if ( v3StrCopyUA( string, (ascii *) "foo points to: %p\nAddress of size is: %p\n", size ) != V3_PASS ) { /* Error */ } v3PrintU( string, foo, &size ); This would print something similar to (on a 32-bit machine): Error: Timeout -- try again later Size: 0255 bytes foo points to: 08049860 Address of size is: 7FFFF7A8 v3PrintAStrSyntax#include "cosmio.h" u64 v3PrintAStr( ascii * string, u64 max_chars, const ascii * format, ... ); DescriptionPrints the formatted ascii text to the string. No more than max_chars-1 characters will be written to the string. See v3PrintA for format usage. Return ValuesNumber of characters written to string, or -1 if truncated due to max_chars. ErrorsNone. Exampleascii * output; void * foo; u64 size; u64 chars_output; _V3_SET64( size, 00000000, 000000FF ); output = v3MemAlloc( size, (u32) V3_MEM_NORMAL ); /* ... */ chars_output = v3PrintAStr( output, size, (ascii *) "foo points to: %p\nAddress of size is: %p\n", foo, &size ); if ( !v3u64Eq( chars_output, v3u64u32( 0 ) ) { /* Error */ } v3PrintUStrSyntax#include "cosmio.h" u64 v3PrintUStr( unicode * string, u64 max_chars, const unicode * format, ... ); DescriptionPrints the formatted unicode text to the string. No more the max_chars-1 characters will be written to the string. See v3PrintA for format usage. Return ValuesNumber of characters written to string, or -1 if truncated due to max_chars. ErrorsNone. Exampleunicode string[256]; unicode * output; void * foo; u64 size; u64 chars_output; _V3_SET64( size, 00000000, 00000100 ); output = v3MemAlloc( v3u64Lsh( size, 1 ), (u32) V3_MEM_NORMAL ); /* ... */ if ( v3StrCopyUA( string, (ascii *) "foo points to: %p\nAddress of size is: %p\n", size ) != V3_PASS ) { /* Error */ } chars_output = v3PrintUStr( output, size, string, foo, &size ); if ( v3u64Eq( chars_output, v3u64u32( 0 ) ) { /* Error */ } v3PrintAFileSyntax#include "cosmio.h" u64 v3PrintAFile( v3_FILE * file, const ascii * format, ... ); DescriptionPrints the formatted ascii text to the file. See v3PrintA for format usage. Return ValuesNumber of characters written to file. ErrorsNone. Exampleascii * output; u64 chars_written; u64 size; v3_FILE * file; _V3_SET64( size, 00000000, 000000FF ); /* ... */ chars_written = v3PrintAFile( file, (ascii *) "Error: %.*s\n", v3u32u64( size ), output ); if ( !v3u64Eq( chars_written, v3u64u32( 0 ) ) ) { /* Error */ } v3PrintUFileSyntax#include "cosmio.h" u64 v3PrintUFile( v3_FILE * file, const unicode * format, ... ); DescriptionPrints the formatted unicode text to the file. See v3PrintA for format usage. Return ValuesNumber of characters written to file. ErrorsNone. Exampleunicode string[256]; u64 chars_written; u64 size; v3_FILE * file; _V3_SET64( size, 00000000, 00000100 ); /* ... */ if ( v3StrCopyUA( string, (ascii *) "Test String.", size ) != V3_PASS ) { /* Error */ } chars_written = v3PrintUFile( file, string ); if ( v3u64Eq( chars_written, v3u64u32( 0 ) ) ) { /* Error */ }
© Copyright Mithral Communications & Design Inc.
1995-2003.
All rights reserved.
Mithral® and Cosm® are trademarks of
Mithral Communications & Design Inc.
|