2 * Acess2 - URI Parser and opener
3 * By John Hodge (thePowersGang)
24 tURI *URI_Parse(const char *String);
25 tURIFile *URI_MakeHandle(int Mode, int Handle, tURIHandler *Handler);
26 tURIFile *URI_Open(int Mode, tURI *URI);
27 size_t URI_Read(tURIFile *File, size_t Bytes, void *Buffer);
28 size_t URI_Write(tURIFile *File, size_t Bytes, void *Buffer);
29 void URI_Close(tURIFile *File);
30 // --- file:/// handler
31 int URI_file_Open(char *Host, int Port, char *Path, int Mode);
32 size_t URI_file_Read(int Handle, size_t Bytes, void *Buffer);
33 size_t URI_file_Write(int Handle, size_t Bytes, void *Buffer);
34 void URI_file_Close(int Handle);
35 off_t URI_file_GetSize(int Handle);
38 // Builtin URI protocol handlers
39 tURIHandler caBuiltinHandlers[] = {
40 {"file", 0, URI_file_Open, URI_file_Close, URI_file_Read, URI_file_Write, URI_file_GetSize}
42 #define NUM_BUILTIN_HANDLERS (sizeof(caBuiltinHandlers)/sizeof(caBuiltinHandlers[0]))
50 tURI *URI_Parse(const char *String)
52 const char *tmp = String;
56 if(!String) return NULL;
59 while( isalpha(*tmp) || isdigit(*tmp) ) tmp++, protolen++;
62 if(tmp[0] == ':' && tmp[1] == '/' && tmp[2] == '/')
64 int hostlen, portlen, pathlen;
65 tmp += 3; // Eat '://'
66 ret = malloc(sizeof(tURI) + strlen(String) - 2);
68 ret->Proto = (char*)ret + sizeof(tURI);
69 memcpy(ret->Proto, String, protolen);
70 ret->Proto[protolen] = '\0';
72 ret->Host = ret->Proto + protolen + 1;
79 while( *tmp && *tmp != ']' ) {
80 ret->Host[hostlen] = *tmp;
85 ret->Host[hostlen] = '\0';
90 while( *tmp && *tmp != '/' && *tmp != ':' )
92 ret->Host[hostlen] = *tmp;
96 ret->Host[hostlen] = '\0';
101 ret->PortStr = ret->Host + hostlen + 1;
104 while(isalpha(*tmp) || isdigit(*tmp))
106 ret->PortStr[portlen] = *tmp;
110 ret->PortStr[portlen] = '\0';
112 ret->PortNum = atoi(ret->PortStr);
113 if(!ret->PortNum && !(ret->PortStr[0] == '0' && portlen == 1) )
132 // TODO: What to do on a parse error
139 ret->Path = ret->PortStr + portlen + 1;
141 ret->Path = ret->Host + hostlen + 1;
147 ret->Path[pathlen] = *tmp;
161 // Probably return file:///<path>
162 // but should I get the CWD and use append that?
163 ret = malloc( sizeof(tURI) + strlen(String) + 1 );
164 ret->Path = (char*)ret + sizeof(tURI);
165 strcpy(ret->Path, String);
174 tURIFile *URI_MakeHandle(int Mode, int Handle, tURIHandler *Handler)
178 ret = malloc(sizeof(tURIFile)+Handler->BlockSize);
179 if(!ret) return NULL;
181 ret->Handle = Handle;
183 ret->Handler = Handler;
184 ret->CurBlockOffset = 0;
189 tURIFile *URI_Open(int Mode, tURI *URI)
191 tURIHandler *handler;
199 for( i = 0; i < NUM_BUILTIN_HANDLERS; i ++ )
201 if(strcmp(URI->Proto, caBuiltinHandlers[i].Name) == 0)
205 if( i == NUM_BUILTIN_HANDLERS )
208 printf("URI_Open: Warning - Unknown URI handler\n");
212 handler = &caBuiltinHandlers[i];
214 printf("URI_Open: handler->Open = %p\n", handler->Open);
216 handle = handler->Open(URI->Host, URI->PortNum, URI->Path, Mode);
217 printf("URI_Open: handle = %i\n", handle);
218 if(handle == -1) return NULL;
220 printf("URI_MakeHandle(Mode=%i, handle=%i, handler=%p)\n",
221 Mode, handle, handler);
222 ret = URI_MakeHandle(Mode, handle, handler);
224 handler->Close( handle );
230 int URI_GetSize(tURIFile *File, size_t *Size)
232 if( !File || !Size ) return -1;
234 if( File->Handler->GetSize )
236 *Size = File->Handler->GetSize(File->Handle);
240 return 1; // Size not avaliable
244 * \brief Read from a URI file
246 size_t URI_Read(tURIFile *File, size_t Bytes, void *Buffer)
252 printf("URI_Read(File=%p, Bytes=%u, Buffer=%p)\n",
253 File, (unsigned int)Bytes, Buffer);
255 if(!File || !Buffer) return -1;
256 if(Bytes == 0) return 0;
258 if( !(File->Mode & URI_MODE_READ) ) return -1;
260 // Read from cache if avaliable
261 if(File->Handler->BlockSize && File->CurBlockOffset)
265 avail = File->Handler->BlockSize - File->CurBlockOffset;
268 memcpy(Buffer, File->Buffer, Bytes);
269 File->CurBlockOffset += Bytes;
270 File->CurBlockOffset %= File->Handler->BlockSize;
275 memcpy(Buffer, File->Buffer, avail);
276 File->CurBlockOffset = 0;
281 if( File->Handler->BlockSize )
284 while( rem >= File->Handler->BlockSize )
286 tmp = File->Handler->Read( File->Handle, File->Handler->BlockSize, buf );
287 if(tmp < File->Handler->BlockSize)
288 return Bytes - rem - tmp;
289 buf += File->Handler->BlockSize;
292 // Read the trailing part
295 File->Handler->Read( File->Handle, File->Handler->BlockSize, File->Buffer );
296 memcpy( buf, File->Buffer, rem );
297 File->CurBlockOffset += rem;
302 return File->Handler->Read( File->Handle, Bytes, Buffer );
306 * \brief Write to a URI file
313 int URI_file_Open(char *Host, int Port, char *Path, int Mode)
316 if(Mode & URI_MODE_READ) smode |= OPENFLAG_READ;
317 if(Mode & URI_MODE_WRITE) smode |= OPENFLAG_WRITE;
319 // printf("URI_file_Open: open('%s', 0x%x)\n", Path, smode);
322 ret = _SysOpen(Path, smode);
326 size_t URI_file_Read(int Handle, size_t Bytes, void *Buffer)
328 // printf("URI_file_Read: (Handle=%i, Buffer=%p, Bytes=%i)\n",
329 // Handle, Buffer, (int)Bytes);
330 return _SysRead(Handle, Buffer, Bytes);
332 size_t URI_file_Write(int Handle, size_t Bytes, void *Buffer)
334 return _SysWrite(Handle, Buffer, Bytes);
336 void URI_file_Close(int Handle)
340 off_t URI_file_GetSize(int Handle)
342 uint64_t curpos = _SysTell(Handle);
344 _SysSeek(Handle, 0, SEEK_END);
345 ret = _SysTell(Handle);
346 _SysSeek(Handle, curpos, SEEK_SET);