Raised This Month: $51 Target: $400
 12% 

File IO - Binary


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
FatalisDK
Senior Member
Join Date: Mar 2006
Location: bacon
Old 02-21-2007 , 09:27   File IO - Binary
Reply With Quote #1

How would I search an entire file (a map bsp in this case) in binary mode, and search for a given string?
__________________

Last edited by FatalisDK; 02-21-2007 at 09:42.
FatalisDK is offline
VEN
Veteran Member
Join Date: Jan 2005
Old 02-21-2007 , 12:23   Re: File IO - Binary
Reply With Quote #2

Here is the function:
Code:
stock contain_binfile(const filename[], const string[], start = 0) {         new file = fopen(filename, "rb")         if (!file)                 return -1         if (start > 0)                 fseek(file, start, SEEK_SET)         new data, pos = 0, len = strlen(string)         while (!feof(file)) {                 fread(file, data, BLOCK_CHAR)                 if (data != string[pos])                         pos = 0                 else if (++pos == len) {                         new cur = ftell(file)                         fclose(file)                         return cur - pos                 }         }         fclose(file)         return -1 }

Here is the usage example:
Code:
new pos = 0 while ((pos = contain_binfile(filename, string, pos)) != -1)         server_print("^"%s^" string position in the ^"%s^" file is %d", string, filename, pos++) //
VEN is offline
FatalisDK
Senior Member
Join Date: Mar 2006
Location: bacon
Old 02-21-2007 , 12:55   Re: File IO - Binary
Reply With Quote #3

Thanks.
__________________
FatalisDK is offline
VEN
Veteran Member
Join Date: Jan 2005
Old 02-21-2007 , 14:20   Re: File IO - Binary
Reply With Quote #4

By the way, is your string is related to the entities data?
In this case it's possible to greatly improve a performance.

Code:
stock get_bsp_entdata_offset(const filename[], &datasize = 0) {         new file = fopen(filename, "rb")         if (!file)                 return -1         new offset         fseek(file, 4, SEEK_SET)         fread(file, offset, BLOCK_INT)         fread(file, datasize, BLOCK_INT)         fclose(file)         return offset }
Code:
// instead of new pos = 0 new pos = get_bsp_entdata_offset(filename)

Last edited by VEN; 02-22-2007 at 13:06.
VEN is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:29.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode