Kernel/IPStack - (minor) TODO retransmit timer
[tpg/acess2.git] / Usermode / Libraries / liburi.so_src / main.c
index 57de6f6..1bb76f5 100644 (file)
@@ -2,7 +2,6 @@
  * Acess2 - URI Parser and opener
  * By John Hodge (thePowersGang)
  */
-#include <acess/sys.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -12,7 +11,7 @@
 // === STRUCTURES ===
 struct sURIFile
 {
-        int    Handle;
+       void    *Handle;
         int    Mode;
        tURIHandler     *Handler;
         int    CurBlockOffset;
@@ -22,22 +21,26 @@ struct sURIFile
 // === PROTOTYPES ===
  int   SoMain(void);
 tURI   *URI_Parse(const char *String);
-tURIFile       *URI_MakeHandle(int Mode, int Handle, tURIHandler *Handler);
+tURIFile       *URI_MakeHandle(int Mode, void *Handle, tURIHandler *Handler);
 tURIFile       *URI_Open(int Mode, tURI *URI);
 size_t URI_Read(tURIFile *File, size_t Bytes, void *Buffer);
 size_t URI_Write(tURIFile *File, size_t Bytes, void *Buffer);
 void   URI_Close(tURIFile *File);
 // --- file:/// handler
- int   URI_file_Open(char *Host, int Port, char *Path, int Mode);
-size_t URI_file_Read(int Handle, size_t Bytes, void *Buffer);
-size_t URI_file_Write(int Handle, size_t Bytes, void *Buffer);
-void   URI_file_Close(int Handle);
-size_t URI_file_GetSize(int Handle);
+void   *URI_file_Open(const char *Host, int Port, const char *Path, int Mode);
+void   URI_file_Close(void *Handle);
+size_t URI_file_Read(void *Handle, size_t Bytes, void *Buffer);
+size_t URI_file_Write(void *Handle, size_t Bytes, const void *Buffer);
+off_t  URI_file_GetSize(void *Handle);
 
 // === CONSTANTS ===
 // Builtin URI protocol handlers
 tURIHandler    caBuiltinHandlers[] = {
-       {"file", 0, URI_file_Open, URI_file_Close, URI_file_Read, URI_file_Write, URI_file_GetSize}
+       {"file", 0,
+               URI_file_Open, URI_file_Close,
+               URI_file_Read, URI_file_Write,
+               URI_file_GetSize
+       }
 };
 #define NUM_BUILTIN_HANDLERS   (sizeof(caBuiltinHandlers)/sizeof(caBuiltinHandlers[0]))
 
@@ -61,9 +64,9 @@ tURI *URI_Parse(const char *String)
        // true URI
        if(tmp[0] == ':' && tmp[1] == '/' && tmp[2] == '/')
        {
-                int    hostlen, portlen, pathlen;
+                int    hostlen, portlen;
                tmp += 3;       // Eat '://'
-               ret = malloc(sizeof(tURI) + strlen(String) - 2);
+               ret = malloc(sizeof(tURI) + protolen + 1 + strlen(tmp) + 1);
                
                ret->Proto = (char*)ret + sizeof(tURI);
                memcpy(ret->Proto, String, protolen);
@@ -120,6 +123,7 @@ tURI *URI_Parse(const char *String)
                else {
                        ret->PortStr = NULL;
                        ret->PortNum = -1;
+                       portlen = 0;
                }
                
                if(*tmp == '\0')
@@ -139,14 +143,7 @@ tURI *URI_Parse(const char *String)
                else
                        ret->Path = ret->Host + hostlen + 1;
                
-               tmp ++;
-               pathlen = 0;
-               while(*tmp)
-               {
-                       ret->Path[pathlen] = *tmp;
-                       tmp ++;
-                       pathlen ++;
-               }
+               strcpy(ret->Path, tmp);
                
                return ret;
        }
@@ -170,7 +167,7 @@ tURI *URI_Parse(const char *String)
        }
 }
 
-tURIFile *URI_MakeHandle(int Mode, int Handle, tURIHandler *Handler)
+tURIFile *URI_MakeHandle(int Mode, void *Handle, tURIHandler *Handler)
 {
        tURIFile        *ret;
        
@@ -189,7 +186,7 @@ tURIFile *URI_Open(int Mode, tURI *URI)
 {
        tURIHandler     *handler;
        tURIFile        *ret;
-        int    handle;
+       void    *handle;
         int    i;
        
        if(!URI)
@@ -214,7 +211,7 @@ tURIFile *URI_Open(int Mode, tURI *URI)
        
        handle = handler->Open(URI->Host, URI->PortNum, URI->Path, Mode);
        printf("URI_Open: handle = %i\n", handle);
-       if(handle == -1)        return NULL;
+       if(handle == NULL)      return NULL;
        
        printf("URI_MakeHandle(Mode=%i, handle=%i, handler=%p)\n",
                Mode, handle, handler);
@@ -249,7 +246,7 @@ size_t URI_Read(tURIFile *File, size_t Bytes, void *Buffer)
        size_t  tmp;
        
        printf("URI_Read(File=%p, Bytes=%u, Buffer=%p)\n",
-               File, Bytes, Buffer);
+               File, (unsigned int)Bytes, Buffer);
        
        if(!File || !Buffer)    return -1;
        if(Bytes == 0)  return 0;
@@ -309,39 +306,42 @@ size_t URI_Read(tURIFile *File, size_t Bytes, void *Buffer)
 // ====
 // Builtin Handlers
 // ====
-int URI_file_Open(char *Host, int Port, char *Path, int Mode)
+void *URI_file_Open(const char *Host, int Port, const char *Path, int Mode)
 {
-        int    smode = 0;
-       if(Mode & URI_MODE_READ)        smode |= OPENFLAG_READ;
-       if(Mode & URI_MODE_WRITE)       smode |= OPENFLAG_WRITE;
+       const char      *smode = NULL;
+       if( (Mode & URI_MODE_READ) && (Mode & URI_MODE_WRITE))
+               smode = "r+";
+       else if( (Mode & URI_MODE_READ) )
+               smode = "r";
+       else if( (Mode & URI_MODE_WRITE) )
+               smode = "w";
+       else
+               smode = "";
        
-       printf("URI_file_Open: open('%s', 0x%x)\n", Path, smode);
-       {
-                int    ret;
-               ret = open(Path, smode);
-               return ret;
-       }
+//     printf("URI_file_Open: open('%s', 0x%x)\n", Path, smode);
+       return fopen(Path, smode);
 }
-size_t URI_file_Read(int Handle, size_t Bytes, void *Buffer)
+size_t URI_file_Read(void *Handle, size_t Bytes, void *Buffer)
 {
-       printf("URI_file_Read: (Handle=%i, Bytes=%i, Buffer=%p)\n",
-               Handle, Bytes, Buffer);
-       return read(Handle, Bytes, Buffer);
+//     printf("URI_file_Read: (Handle=%i, Buffer=%p, Bytes=%i)\n",
+//             Handle, Buffer, (int)Bytes);
+       return fread(Buffer, Bytes, 1, Handle);
 }
-size_t URI_file_Write(int Handle, size_t Bytes, void *Buffer)
+size_t URI_file_Write(void *Handle, size_t Bytes, const void *Buffer)
 {
-       return write(Handle, Bytes, Buffer);
+       return fwrite(Buffer, Bytes, 1, Handle);
 }
-void URI_file_Close(int Handle)
+void URI_file_Close(void *Handle)
 {
-       close(Handle);
+       fclose(Handle);
 }
-size_t URI_file_GetSize(int Handle)
+off_t URI_file_GetSize(void *Handle)
 {
-       uint64_t curpos = tell(Handle);
-       size_t ret;
-       seek(Handle, 0, SEEK_END);
-       ret = tell(Handle);
-       seek(Handle, curpos, SEEK_SET);
+       off_t curpos = ftell(Handle);
+       off_t ret;
+       fseek(Handle, 0, SEEK_END);
+       ret = ftell(Handle);
+       fseek(Handle, curpos, SEEK_SET);
        return ret;
 }
+

UCC git Repository :: git.ucc.asn.au