From: Dirk B. <db...@us...> - 2007-05-26 10:06:14
|
Update of /cvsroot/win32forth/win32forth/doc In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv3872/doc Added Files: SQLite.htm WinSock.htm Log Message: - Tom Dixon's WinSock and SQLite library's added --- NEW FILE: WinSock.htm --- <?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta name="GENERATOR" content="dexh v03"> <meta name="ProgId" content="FrontPage.Editor.Document"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title> </title><style><!-- h1 { font-family: Tahoma; font-size: 24pt; font-weight: bold } h2 { font-family: Tahoma; font-size: 18pt; font-weight: bold } --> </style> </head> <body><h1 align="center"> <a href="mailto:win...@ya...?subject=DOC:Doc error in $Id: WinSock.htm,v 1.1 2007/05/26 10:06:10 dbu_de Exp $"> <img border="0" src="TELLUS.gif" align="left" width="32" height="32"></a> <img border="0" src="FORTHPRO.gif" width="32" height="32"> Win32Forth</h1> <hr /><h1>Simple Socket Library </h1><hr /><h3><i>Tom Dixon - July 2006 </i></h3><p></p><p>This is intended to be a simple wordset for sockets in forth. The words do not match the standard socket api. It has been adapted to be easier to use in Forth. It's simplicity should make it easy to port network apps to other forth systems. </p><h2>Network Formatting Words </h2>These words are for converting 16-bit and 32-bit values to the right format so any machine should be able to convert them back into their values. <pre><b><a name="0">1 PROC htonl ( hostlong -- u_long ) </a></b></pre><p>Convert a 32-bit number on the stack to a network acceptable byte-ordered value. </p><pre><b><a name="1">1 PROC htons ( hostshort -- u_short ) </a></b></pre><p>Convert a 16-bit number on the stack to a network acceptable byte-ordered value. </p><pre><b><a name="2">1 PROC ntohl ( netlong -- u_long ) </a></b></pre><p>Convert a network compatible 32-bit number on the stack to the correct 32-bit integer </p><pre><b><a name="3">1 PROC ntohs ( netshort -- u_short ) </a></b></pre><p>Convert a network compatible 16-bit number on the stack to the correct 16-bit integer </p><h2>Socket Library and Initialization Words </h2>These words are for initializing and unloading the windows socket dll. They are automatically called when the console is initialized and right before it closes, so normally a developer would never need to use these. <pre><b><a name="4">: wsocket2-init ( -- ) </a></b></pre><p>Initializes the windows socket dll <br /> called in initialization-chain </p><pre><b><a name="5">: wsocket2-cleanup ( -- ) </a></b></pre><p>Initializes the windows socket dll <br /> called in initialization-chain </p><h2>Main Socket Words </h2>These words represent the core of the socket library. They have been written to be thread-safe. <pre><b><a name="6">: host>iaddr ( str len -- iaddr ) </a></b></pre><p>This function converts a host string to an ip address <br /> The host string could be anything from a domain name to ip address. <br /> Returns 0 if the host is unable to be looked up. </p><pre><b><a name="7">: iaddr>str ( iaddr -- str len ) </a></b></pre><p>This converts an ip address to a readable string. It does not look up the host name, the string is in the "255.255.255.255" format </p><br><br><U>Example:</U> simple host lookup.<br> <pre>s" www.win32forth.org" host>iaddr dup . \ should be anything other than 0 iaddr>str type \ should return ip address of win32forth.org </pre><pre><b><a name="8">: sock-open ( addr len port -- sock ) </a></b></pre><p>This opens up a new socket to a host name on a given port number <br /> the host name will be looked up and the port number is converted implicitly <br /> If the socket cannot be opened, a exception will be thrown. </p><pre><b><a name="9">: sock-read ( addr len sock -- len ) </a></b></pre><p>Reads data from the socket to a buffer. <br /> It works very similarly to 'read-file', but has different return parameters <br /> a returned 'len' of -1 means there was a socket error (SOCKET_ERROR) <br /> If the provided 'len' is larger than the amount of data ready to be read from the socket, the socket will block until it has revceived the full amount of data.<br /> If the socket is a non-blocking socket, it will read what it can and return right away. </p><pre><b><a name="10">: sock-write ( addr len sock -- len ) </a></b></pre><p>Write data from a buffer to the socket. <br /> It works very similarly to 'write-file' <br /> a returned 'len' of -1 means there was a socket error (SOCKET_ERROR) <br /> If the socket is currently unable to take any data, the socket will block until it has room in it's internal buffer to send the data.<br /> If the socket is a non-blocking socket, it will write what it can and return right away. (amount actually written is returned as 'len') </p><pre><b><a name="11">: sock-close ( sock -- ior ) </a></b></pre><p>Closes socket - very similar to close-file<br /> ior is 0 if the close was successful </p><br><br><U>Example:</U> Get data from a socket.<br> This will dump the html data from google's homepage through the use of sockets.<br> <pre>create tbuf 256 allot 0 value sock : sdump ( sock -- ) begin dup sock-read? if dup tbuf 256 rot sock-read tbuf swap type then dup sock-closed? key? or until sock-close drop ; s" www.google.com" 80 sock-open value sock s" GET / HTTP/1.0" sock sock-write . crlf$ count sock sock-write . crlf$ count sock sock-write . sock sdump </pre><h2>Socket Listening Words </h2>These words are for writting the serving-end of network applications.<br /> They have also been written to be thread-safe. <pre><b><a name="12">: sock-create ( p -- sock ) </a></b></pre><p>Make a new socket for listening on port 'p' Used only for server-side sockets </p><pre><b><a name="13">: sock-listen ( n sock -- ) </a></b></pre><p>This tells a socket to start queuing sockets that want to connect.<br /> 'n' is the size of the queue that should be created to listen. after 'n' sockets have tried to connect and have yet to be accepted, further sockets will be refused until waiting sockets are accepted. (standard queue size is 5) </p><pre><b><a name="14">: sock-accept ( sock -- sock iaddr ) </a></b></pre><p>This will accept a socket that is in the listening queue. <br /> 'iaddr' is the ip address of the connecting socket and can be converted into an easy-to-read number through the 'iaddr>str' word. <br /> If no sockets are in queue to be accepted, this function will block until one tries to connect. <br /> If the socket is a non-blocking socket, then the function will fail and return immediately if the queue has no sockets to accept. <br /> If the function fails, it will return '0' as the iaddr and '-1' (or INVALID_SOCKET) as the socket. </p><h2>Asyncronous Socket Words </h2>These words are for the ability to use the sockets without having them block.<br /> Very useful for apps that need to do many things at once. <pre><b><a name="15">: sock-read? ( sock -- n ) </a></b></pre><p>This function returns the amount of data that the socket can read without blocking. It is useful for working with socket asyncronously.<br /> It will return -1 if the socket has no data to read (will block, or socket closed). </p><pre><b><a name="16">: sock-write? ( sock -- flag ) </a></b></pre><p>This function returns true if the socket can write data without blocking.<br /> You can send 0-1024 bytes to the socket asyncronously without blocking if the flag is true. </p><pre><b><a name="17">: sock-accept? ( sock -- flag ) </a></b></pre><p>This function returns true if the socket has other sockets in queue that want to be connected. It is to be used in conjunction with 'sock-accept' so you can call sock-accept without blocking. </p><pre><b><a name="18">: sock-closed? ( sock -- flag ) </a></b></pre><p>This function tests to see if the socket has been closed at the other end or broken at any point. </p><pre><b><a name="19">: sock-err? ( sock -- n ) </a></b></pre><p>This function tests to see if there are any errors on the socket. </p><pre><b><a name="20">: sock-blocked ( flag sock -- ) </a></b></pre><p>This function sets a socket to blocked or unblocked mode.<br /> If the flag is false, the socket will be set to 'unblocked'.<br /> If the flag is true, the socket will be set to 'blocked'.<br /> </p><hr><p>Document $Id: WinSock.htm,v 1.1 2007/05/26 10:06:10 dbu_de Exp $</p> </body></html> --- NEW FILE: SQLite.htm --- <?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta name="GENERATOR" content="dexh v03"> <meta name="ProgId" content="FrontPage.Editor.Document"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title> </title><style><!-- h1 { font-family: Tahoma; font-size: 24pt; font-weight: bold } h2 { font-family: Tahoma; font-size: 18pt; font-weight: bold } --> </style> </head> <body><h1 align="center"> <a href="mailto:win...@ya...?subject=DOC:Doc error in $Id: SQLite.htm,v 1.1 2007/05/26 10:06:10 dbu_de Exp $"> <img border="0" src="TELLUS.gif" align="left" width="32" height="32"></a> <img border="0" src="FORTHPRO.gif" width="32" height="32"> Win32Forth</h1> <hr /><h1>SQLite -- Database Class for SQLite </h1><hr /><h3><i>Tom Dixon </i></h3><p>This class provides an interface to SQLite databases. </p><h2>SQLite Behavior </h2>SQLite operates on database files. Files are opened as read-only, if no update occurs, and can be shared through several applications. Writes cause a block until the file can be opened as read/write. This makes the locking scheme very efficient and easy to work with.<br /><br /> SQLite is very fast, flexible, and simple. One very nice feature is that SQLite will convert types as best as it can for you if you want a type different from the native database type.<br /><br /> For more information about SQLite, please see <A href="http://www.sqlite.org/">www.sqlite.org</A> <pre><b><a name="0">:class SQLiteDB <SUPER Object </a></b></pre><p>SQLiteDB is an interface to SQLite. </p><pre><b><a name="1">:M ERR: ( -- ) \ thows an error on any problem </a></b></pre><p>Displays any error that might have occured. </p><pre><b><a name="2">:M Open: ( str len -- ) </a></b></pre><p>Opens a database file so we can execute operations on it.<br /> If the string is ":memory:" the database is actually created in memory instead of on disk, and should be faster. </p><pre><b><a name="3">:M Close: ( -- flag ) </a></b></pre><p>Closes the database. You can still open another database with the same object after closing, if desired. </p><pre><b><a name="4">:M Version: ( -- str len ) </a></b></pre><p>Returns the version of SQLite being used </p><pre><b><a name="5">:M Execute: ( str len -- ) </a></b></pre><p>Execute a SQL query on the cursor. Any returned data will be in the cursor. </p><pre><b><a name="6">:M Requery: ( -- ) </a></b></pre><p>Rerun the last query. </p><pre><b><a name="7">:M FieldCnt: ( -- n ) </a></b></pre><p>Returns the number of columns in the current record. </p><pre><b><a name="8">:M FieldType: ( field -- DataTypeEnum ) </a></b></pre><p>Returns the data type constant of the given column. Possible data types are: </p><table border="1"><tr><td>SQLITE_INTEGER </td> </tr> <tr><td>SQLITE_FLOAT </td> </tr> <tr><td>SQLITE_TEXT </td> </tr> <tr><td>SQLITE_BLOB </td> </tr> <tr><td>SQLITE_NULL </td> </tr> </table><pre><b><a name="9">:M FieldName: ( field -- str len ) </a></b></pre><p>Returns the column name of the given column number. </p><pre><b><a name="10">:M GetInt: ( field -- int ) </a></b></pre><p>Returns an integer value of the given column on the current row. </p><pre><b><a name="11">:M GetDouble: ( field -- d ) </a></b></pre><p>Returns the double of the given column on the current row. </p><pre><b><a name="12">:M GetFloat: ( field -- float ) </a></b></pre><p>Returns the floating point value of the given column on the current row. </p><pre><b><a name="13">:M GetStr: ( field -- str len ) </a></b></pre><p>Returns the string of the given column on the current row. May be much longer than 255 </p><pre><b><a name="14">:M GetBLOB: ( field -- addr len ) </a></b></pre><p>Returns the Binary Buffer of the given column on the current row. This binary data may be anything. </p><pre><b><a name="15">:M isNull?: ( field -- flag ) </a></b></pre><p>Returns true if the given field for the given flag is null </p><pre><b><a name="16">:M NextRow: ( -- flag ) </a></b></pre><p>Goes to the next row of the query result. If there are no more rows, it will return true </p><pre><b><a name="17">:M BindInt: ( n i -- ) </a></b></pre><p>Binds a '?' in the query string to a integer. If there are no more question marks in the query string, the query will execute. </p><pre><b><a name="18">:M BindDouble: ( d i -- ) </a></b></pre><p>Binds a '?' in the query string to a double int. If there are no more question marks in the query string, the query will execute. </p><pre><b><a name="19">:M BindFloat: ( f i -- ) </a></b></pre><p>Binds a '?' in the query string to a floating point number. If there are no more question marks in the query string, the query will execute. </p><pre><b><a name="20">:M BindStr: ( str len i -- ) </a></b></pre><p>Binds a '?' in the query string to a string. If there are no more question marks in the query string, the query will execute. </p><pre><b><a name="21">:M BindBlob: ( str len i -- ) </a></b></pre><p>Binds a '?' in the query string to a blob (binary buffer object, or in simpler terms, a bunch of bytes). If there are no more question marks in the query string, the query will execute. </p><h2>Examples of Usage: </h2><br><U>Creating/Opening a Database:</U><br><br> <pre>SQLiteDB sqlite s" c:\test.db" open: sqlite </pre><br><U>Creating a Database in RAM:</U><br><br> <pre>SQLiteDB sqlite s" :memory:" open: sqlite </pre><br><U>Closing a Database:</U><br><br> <pre>close: sqlite </pre><br><U>Creating a table:</U><br><br> <pre>s" CREATE TABLE idtoname (id int, name varchar)" execute: sqlite </pre><br><U>Inserting into a table:</U><br><br> <pre>s" INSERT INTO idtoname(id, name) VALUES(?,?)" execute: sqlite 1 0 bindint: sqlite s" Jim Hawkins" 1 bindstr: sqlite s" INSERT INTO idtoname(id, name) VALUES(?,?)" execute: sqlite 2 0 bindint: sqlite s" Billy Bones" 1 bindstr: sqlite s" INSERT INTO idtoname(id, name) VALUES(?,?)" execute: sqlite 3 0 bindint: sqlite s" Long John Silver" 1 bindstr: sqlite </pre><br><U>Executing SQL:</U><br><br> <pre>: qdump ( -- ) fieldcnt: sqlite 0 ?do i fieldname: sqlite type tab loop cr cr begin fieldcnt: sqlite 0 ?do i getstr: sqlite type tab loop cr nextrow: sqlite until ; s" SELECT * FROM idtoname WHERE id < 1000 ORDER BY name DESC" execute: sqlite cr qdump </pre><br><U>Deleting a table:</U><br><br> <pre>s" DROP TABLE idtoname" execute: sqlite </pre> |