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);
37 // Builtin URI protocol handlers
38 tURIHandler caBuiltinHandlers[] = {
39 {"file", 0, URI_file_Open, URI_file_Close, URI_file_Read, URI_file_Write}
41 #define NUM_BUILTIN_HANDLERS (sizeof(caBuiltinHandlers)/sizeof(caBuiltinHandlers[0]))
49 tURI *URI_Parse(const char *String)
51 const char *tmp = String;
55 if(!String) return NULL;
58 while( isalpha(*tmp) ) tmp++, protolen++;
61 if(tmp[0] == ':' && tmp[1] == '/' && tmp[2] == '/')
63 int hostlen, portlen, pathlen;
64 tmp += 3; // Eat '://'
65 ret = malloc(sizeof(tURI) + strlen(String) - 2);
67 ret->Proto = (char*)ret + sizeof(tURI);
68 memcpy(ret->Proto, String, protolen);
69 ret->Proto[protolen] = '\0';
71 ret->Host = ret->Proto + protolen + 1;
78 while( *tmp != ']' ) {
79 ret->Host[hostlen] = *tmp;
84 ret->Host[hostlen] = '\0';
89 while( *tmp != '/' && *tmp != ':' )
91 ret->Host[hostlen] = *tmp;
95 ret->Host[hostlen] = '\0';
100 ret->PortStr = ret->Host + hostlen + 1;
103 while(isalpha(*tmp) || isdigit(*tmp))
105 ret->PortStr[portlen] = *tmp;
109 ret->PortStr[portlen] = '\0';
111 ret->PortNum = atoi(ret->PortStr);
112 if(!ret->PortNum && !(ret->PortStr[0] == '0' && portlen == 1) )
130 // TODO: What to do on a parse error
137 ret->Path = ret->PortStr + portlen + 1;
139 ret->Path = ret->Host + hostlen + 1;
145 ret->Path[pathlen] = *tmp;
159 // Probably return file:///<path>
160 // but should I get the CWD and use append that?
161 ret = malloc( sizeof(tURI) + strlen(String) + 1 );
162 ret->Path = (char*)ret + sizeof(tURI);
163 strcpy(ret->Path, String);
172 tURIFile *URI_MakeHandle(int Mode, int Handle, tURIHandler *Handler)
176 ret = malloc(sizeof(tURIFile)+Handler->BlockSize);
177 if(!ret) return NULL;
179 ret->Handle = Handle;
181 ret->Handler = Handler;
182 ret->CurBlockOffset = 0;
187 tURIFile *URI_Open(int Mode, tURI *URI)
189 tURIHandler *handler;
197 for( i = 0; i < NUM_BUILTIN_HANDLERS; i ++ )
199 if(strcmp(URI->Proto, caBuiltinHandlers[i].Name) == 0)
203 if( i == NUM_BUILTIN_HANDLERS )
206 printf("URI_Open: Warning - Unknown URI handler\n");
210 handler = &caBuiltinHandlers[i];
212 printf("URI_Open: handler->Open = %p\n", handler->Open);
214 handle = handler->Open(URI->Host, URI->PortNum, URI->Path, Mode);
215 printf("URI_Open: handle = %i\n", handle);
216 if(handle == -1) return NULL;
218 printf("URI_MakeHandle(Mode=%i, handle=%i, handler=%p)\n",
219 Mode, handle, handler);
220 ret = URI_MakeHandle(Mode, handle, handler);
222 handler->Close( handle );
229 * \brief Read from a URI file
231 size_t URI_Read(tURIFile *File, size_t Bytes, void *Buffer)
237 printf("URI_Read(File=%p, Bytes=%u, Buffer=%p)\n",
238 File, Bytes, Buffer);
240 if(!File || !Buffer) return -1;
241 if(Bytes == 0) return 0;
243 if( !(File->Mode & URI_MODE_READ) ) return -1;
245 // Read from cache if avaliable
246 if(File->Handler->BlockSize && File->CurBlockOffset)
250 avail = File->Handler->BlockSize - File->CurBlockOffset;
253 memcpy(Buffer, File->Buffer, Bytes);
254 File->CurBlockOffset += Bytes;
255 File->CurBlockOffset %= File->Handler->BlockSize;
260 memcpy(Buffer, File->Buffer, avail);
261 File->CurBlockOffset = 0;
266 if( File->Handler->BlockSize )
269 while( rem >= File->Handler->BlockSize )
271 tmp = File->Handler->Read( File->Handle, File->Handler->BlockSize, buf );
272 if(tmp < File->Handler->BlockSize)
273 return Bytes - rem - tmp;
274 buf += File->Handler->BlockSize;
277 // Read the trailing part
280 File->Handler->Read( File->Handle, File->Handler->BlockSize, File->Buffer );
281 memcpy( buf, File->Buffer, rem );
282 File->CurBlockOffset += rem;
287 return File->Handler->Read( File->Handle, Bytes, Buffer );
291 * \brief Write to a URI file
298 int URI_file_Open(char *Host, int Port, char *Path, int Mode)
301 if(Mode & URI_MODE_READ) smode |= OPENFLAG_READ;
302 if(Mode & URI_MODE_WRITE) smode |= OPENFLAG_WRITE;
304 printf("URI_file_Open: open('%s', 0x%x)\n", Path, smode);
307 ret = open(Path, smode);
311 size_t URI_file_Read(int Handle, size_t Bytes, void *Buffer)
313 printf("URI_file_Read: (Handle=%i, Bytes=%i, Buffer=%p)\n",
314 Handle, Bytes, Buffer);
315 return read(Handle, Bytes, Buffer);
317 size_t URI_file_Write(int Handle, size_t Bytes, void *Buffer)
319 return write(Handle, Bytes, Buffer);
321 void URI_file_Close(int Handle)