Best (fastest) way to check a valid string?

5 posts / 0 new
Last post
jabirulo
jabirulo's picture
Offline
Last seen: 4 days 21 hours ago
Joined: 2013-05-30 00:53
Best (fastest) way to check a valid string?
Hi, just creating a simple too to get version strings, when there is a "buggy" I load the first 64K of the file and then try to fiind '$VER', just want to know which "method" can be the best/fastest.
  1. int32 buf_len;
  2. uint8 *buf = IExec->AllocVecTags(65535, AVT_ClearWithValue,0, TAG_DONE);
  3.  
  4. res = FALSE;
  5. n = 0;
  6.  
  7. if(buf) {
  8. BPTR fhBinFile = IDOS->Open(name, MODE_OLDFILE);
  9. //if(!fhBinFile) {
  10. buf_len = IDOS->Read(fhBinFile, buf, 65535); // only on first 64K of file
  11. buf_len-=3;
  12. while(buf_len--) {
  13. ...
Then for checking for '$VER' I manage to create 3 different modes, they seem to work fine, but if someone finds a problem using such modes I'm open to all possible suggestions. (BTW is it worth to use AVT_ClearWithValue,0?)
  1. if( !(*(buf+n)^0x24) && !(*(buf+n+1)^0x56) && !(*(buf+n+2)^0x45) && !(*(buf+n+3)^0x52) ) { // '$VER' = 0x24564552
  2.  
  3. if(IUtility->Strnicmp((STRPTR)(buf+n),"$VER",4) == 0) {
  4.  
  5. int32 foo = *(buf+n) << 24 | *(buf+n+1) << 16 | *(buf+n+2) << 8 | *(buf+n+3);
  6. if(foo == 0x24564552 ) { // '$VER' = 0x24564552
TIA
trixie
trixie's picture
Offline
Last seen: 2 weeks 6 days ago
Joined: 2011-02-03 13:58
Re: Best (fastest) way to check a valid string?
@jabirulo Don't know about your actual intentions here but personally I'd use GetSegListInfo() to obtain the version string, instead of reading data from the file directly. To do this you need to LoadSeg() from your file, then use GetSegListInfo() to obtain the value of the GSLI_VersionString tag (possibly also GSLI_ResidentVersionString as a fallback), and UnLoadSeg() when you're finished.
SAM440ep-flex @ 667MHz / 1GB RAM / Radeon 9250 / AmigaOS 4.1 Final Edition Author of [url=http://www.os4depot.net/index.php?function=showfile&file=utility/scienti... for OS4
jabirulo
jabirulo's picture
Offline
Last seen: 4 days 21 hours ago
Joined: 2013-05-30 00:53
Re: Best (fastest) way to check a valid string?

Already doing thath first, but some software has "weird" $VER strings (empty) and thus GetSegListInfo() fails. Example: metadata.library (os4depot):

#version ram:MetadataLibrary/libs/metadata.library file full
$ver:
#getversion ram:MetadataLibrary/libs/metadata.library
metadata.library 1.1 (20.5.2016)

Uploaded my unfished GetVersion here: http://jabirulo.site90.com/temp/GetVersionRC.7zip

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P
TSK
TSK's picture
Offline
Last seen: 2 weeks 4 days ago
Joined: 2011-06-28 02:06
Re: Best (fastest) way to check a valid string?
I had old mistake still in metadata.library which I've done in all my software searching for the version tag using string comparison and static "$ver" string. That confuses every piece of software which is searching for version tags. Nowadays I'm using GetSegListInfo() everywhere. I uploaded a fixed version of the metadata.libary to os4depot a moment ago. If you want to create your own routine don't use string comparison but compare data byte by byte to avoid the mentioned issue.
jabirulo
jabirulo's picture
Offline
Last seen: 4 days 21 hours ago
Joined: 2013-05-30 00:53
Re: Best (fastest) way to check a valid string?
What I do in my little getVersion tool:
  1. // ELF FILES
  2. if(res == FALSE) {
  3. if( (seglist=IDOS->LoadSeg(name)) ) {
  4. IDOS->GetSegListInfoTags(seglist,
  5. GSLI_VersionString,&str_ver,
  6. GSLI_ResidentVersionString,&str_res, TAG_DONE);
  7. if(str_ver) { IUtility->SNPrintf(temp, 512, "%s",str_ver); res = TRUE; }
  8. else if(str_res) { IUtility->SNPrintf(temp, 512, "%s",str_res); res = TRUE; }
  9.  
  10. IDOS->UnLoadSeg(seglist);
  11.  
  12. // Incorrect $VER data? -> "brute force" search
  13. if(IUtility->Strlen(temp) < 10) {
  14. int32 buf_len;
  15. uint8 *buf_txt = IExec->AllocVecTags(65535, AVT_ClearWithValue,0, TAG_DONE);
  16.  
  17. res = FALSE;
  18. n = 0;
  19.  
  20. if(buf_txt) {
  21. BPTR fhBinFile = IDOS->Open(name, MODE_OLDFILE);
  22. //if(!fhBinFile) {
  23. buf_len = IDOS->Read(fhBinFile, buf_txt, 65535); // only on first 64K of file
  24. ...
AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P
Log in or register to post comments