1. stddeff.e
- Posted by Falkon <Falkn13 at IBM.NET> Jul 11, 1998
- 424 views
long ago and far away, David Cuny said: >I offer the slice() function for that same hypothetical include file >("goodies.e"), along with: > global constant TRUE = 1, FALSE = 0 >I'd include the new DOS functions that were posted the other day, but >I'm thinking that the routines should be usable by both Win32 and >DOS32. >Any more suggestions of things to include? This is what I have in my stddef.e. For what it's worth. Might also add types like word, dword, qword, etc for ease of interacting with other languages...but I haven't needed to do that yet. (And yes I know 0 is easier to type than ZERO, but I think the word is good for readability at times, to show that you're intentionally zeroing something...in initializations and such) ------------- Warning: code follows -------------- --stddef.e --standard constants, types, and routines global constant FALSE = 0, TRUE = 1, NULL = {}, ZERO = 0, MININT = -1073741824, --#C0 00 00 00 MAXINT = 1073741823 --#3F FF FF FF ---- B U I L T I N T Y P E S ---- ----these three allow the built-in types to be called by routineID type satom( object v ) return( atom( v )) end type type sinteger( object v ) return( integer( v ) ) end type type ssequence( object v ) return( sequence( v ) ) end type ----routineIDs global constant ATOMS = routine_id( "satom" ), INTEGERS = routine_id( "sinteger" ), SEQUENCES = routine_id( "ssequence" ) ---- I N T E G E R T Y P E S ---- global type validIndex( object v ) --valid sequence index if integer(v) then return(v > 0) end if return( FALSE ) end type global type posInt( object v ) if integer( v ) then return( v > -1 ) end if return( FALSE ) end type global type booleanInt( object v ) if integer( v ) then return( (v = TRUE) or (v = FALSE) ) end if return( FALSE ) end type global type char( object v ) if posInt( v ) then return( v < 256 ) end if return( FALSE ) end type ----routineIDs global constant BOOLEANINTS = routine_id( "booleanInt" ), POSINTS = routine_id( "posInt" ), VALIDINDEXES = routine_id( "validIndex" ), CHARS = routine_id( "char" ) ---- S E Q U E N C E T Y P E S ---- global function setOf( integer t, object v ) --Boolean: tests whether object v is a set of --objects of the type identified by routineID t --example: if setOf( STRINGS, buffer ) then if sequence( v ) then for count = 1 to length( v ) do if not call_func( t, {v[count]} ) then return( FALSE ) end if end for return( TRUE ) end if return( FALSE ) end function global type string( object v ) return( setOf( CHARS, v ) ) end type global type c_string( object v ) --C compatible zero-terminated string if setOf( CHARS, v ) then if not find( 0, v[1..length(v)-1] ) then return( v[length(v)] = 0 ) end if end if return( FALSE ) end type global type p_string( object v ) --Pascal compatible string: first byte = length in bytes if setOf( CHARS, v ) then return( v[1] = ( length( v ) - 1 ) ) end if return( FALSE ) end type ----routineIDs global constant STRINGS = routine_id( "string" ), C_STRINGS = routine_id( "c_string" ), P_STRINGS = routine_id( "p_string" ) ---- C O M M O N R O U T I N E S ---- global function rfind( object v, sequence s ) --Sequence: recurses down through subsequences until it finds an object --returns {0} if object not found --or a sequence of indices to the object if found --ex: --s = rfind( 'C', {{{1, 2, 3}, {4, 5, 6}},{ 11, {"ABCD", "EFGH", "IJKL"}, 22, 17},{}} ) ----s = { 2, 2, 1, 3 } --s = rfind( "IJKL", {{{1, 2, 3}, {4, 5, 6}},{ 11, {"ABCD", "EFGH", "IJKL"}, 22, 17},{}} ) ----s = { 2, 2, 3 } --s = rfind( 'V', {{{1, 2, 3}, {4, 5, 6}},{ 11, {"ABCD", "EFGH", "IJKL"}, 22, 17},{}} ) ----s = { 0 } sequence loc loc = {} for count = 1 to length( s ) do if compare( v, s[count] ) = 0 then return( {count} ) elsif sequence( s[count] ) then loc = rfind( v, s[count] ) if loc[1] then return( count & loc ) end if end if end for return( {0} ) end function global function divm( object v1, object v2 ) --Object: divide two objects and return --{ integer_part(s)_of_answer, remainder(s) } --or atom -1 if attempting to div by zero --ex: --x = divm( 27, {3, 5} ) ----x = { {9, 5}, {0, 2} } --x = divm( {20, {35, 42}, 27}, {7, {5, 9}, 3} ) ----x = { {2, { 7, 4}, 9}, {6, {0, 6}, 0} } --x = divm( {20, {35, 42}, 27}, {7, {5, 0}, 3} ) ----x = -1 sequence lz if atom(v2) then if v2 = 0 then return( -1 ) end if else lz = rfind( 0, v2 ) if lz[1] then return( -1 ) end if end if return( { floor(v1/v2), remainder( v1, v2 ) } ) end function global function getf( sequence filename ) --Object: read a whole file and return a sequence of characters --or the atom -1 if unable to open integer fn, ch sequence ts fn = open( filename, "rb" ) if (fn = -1) then printf( 1, "\n\nUnable to open %s\n", {filename} ) return( -1 ) end if ts = {} while 1 do ch = getc( fn ) if ch = -1 then exit end if ts = ts & ch end while close( fn ) return( ts ) end function global function putf( sequence filename, sequence contents ) --Integer: write a sequence of characters to binary file --return 0 if successful, -1 if unable to open, -2 if --given invalid sequence integer fn if not setOf( CHAR, contents ) then return( -2 ) end if fn = open( filename, "wb" ) if (fn = -1) then printf( 1, "\n\nUnable to open %s\n", {filename} ) return( -1 ) end if puts( fn, contents ) close( fn ) return( 0 ) end function ------------- Warning: end of code --------------