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

Solved [ASK, HELP] GetFileTypeName


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PeEzZ
Senior Member
Join Date: Jan 2014
Location: Hungary
Old 10-06-2015 , 13:13   [ASK, HELP] GetFileTypeName
Reply With Quote #1

My stock:

Code:
stock GetFileTypeName(String: FilePath[], String: FileType[], FileTypeLen)
{
    if(StrContains(FilePath, ".") != -1)
    {
        for(new i = strlen(FilePath); i >= 0; i--)
        {
            if(StrContains(FilePath[i], ".") != -1)
            {
                i += 1;
                strcopy(FileType, FileTypeLen, FilePath[i]);
                break;
            }
        }
    }
}
This code is working but i dont know, the code syntax is good, or it could be better.
Code:
new String: TYPE[256];
GetFileTypeName("wow/lol.mp3", TYPE, sizeof(TYPE));
PrintToServer(TYPE); // Console is saying: "mp3"
My ask: This syntax is good?

EDIT:
The answer:
Code:
stock bool:GetFileTypeName(const String:FilePath[], String:FileType[], FileTypeLen)
{
     new loc = FindCharInString(FilePath, '.', true);
     if (loc == -1)
     {
          FileType[0] = '\0';
          return false;
     }
     //strcopy(FileType, FileTypeLen, FilePath[loc]); //this get the file's name and the dot, like "lol.wav" >> ".wav"
     strcopy(FileType, FileTypeLen, FilePath[loc + 1]); //this get only the file's name like "lol.wav" >> "wav"
     return true;
}
__________________
I MAKING MY PLUGINS/MAPS STILL, BUT I DON'T HAVE ENOUGH TIME, PLEASE STAND BY.
Sorry for my bad english, correct me if i'm wrong.
Magyarok írjanak PM-et ha fontos!

If you want, you can donate for me here
My plugins here / My maps here

Last edited by PeEzZ; 09-16-2016 at 16:31.
PeEzZ is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 10-06-2015 , 13:43   Re: [ASK, HELP] GetFileTypeName
Reply With Quote #2

I would use upper-case only for constants. The only prog. languages where I have ever seen upper-case variables are Cobol and CL...

And ofc it's bad to declare string of size 256 when you use only 4.
KissLick is offline
Starbish
AlliedModders Donor
Join Date: Oct 2011
Location: South Korea
Old 10-06-2015 , 13:46   Re: [ASK, HELP] GetFileTypeName
Reply With Quote #3

how about this one?

PHP Code:
stock void GetFileExtension(char[] cExtensionName, const char[] cFileNameint maxlength){

    
Format(cExtensionNamemaxlength"%s"cFileName[FindCharInString(cFileName'.'true) + 1]);

__________________

Last edited by Starbish; 10-06-2015 at 13:46.
Starbish is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 10-06-2015 , 13:48   Re: [ASK, HELP] GetFileTypeName
Reply With Quote #4

Quote:
Originally Posted by Starbish View Post
how about this one?

PHP Code:
stock void GetFileExtension(char[] cExtensionName, const char[] cFileNameint maxlength){

    
Format(cExtensionNamemaxlength"%s"cFileName[FindCharInString(cFileName'.'true) + 1]);

wouldn't that get "file.mp3" if the filename is: "some.file.mp3"
Mitchell is offline
Starbish
AlliedModders Donor
Join Date: Oct 2011
Location: South Korea
Old 10-06-2015 , 13:52   Re: [ASK, HELP] GetFileTypeName
Reply With Quote #5

Quote:
Originally Posted by Mitchell View Post
wouldn't that get "file.mp3" if the filename is: "some.file.mp3"
no.

FindCharInString()'s third parameter sets whether the starting point is 0 or the last index of the string.
__________________
Starbish is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 10-06-2015 , 13:56   Re: [ASK, HELP] GetFileTypeName
Reply With Quote #6

Quote:
Originally Posted by Starbish View Post
no.

FindCharInString()'s third parameter sets whether the starting point is 0 or the last index of the string.
ah nice, some reason i just assumed it was for case sensitivity. (no need to explain why, I can already guess why)
Mitchell is offline
Starbish
AlliedModders Donor
Join Date: Oct 2011
Location: South Korea
Old 10-06-2015 , 13:58   Re: [ASK, HELP] GetFileTypeName
Reply With Quote #7

Quote:
Originally Posted by Mitchell View Post
ah nice, some reason i just assumed it was for case sensitivity. (no need to explain why, I can already guess why)
ya. that looks quite assumable.
__________________
Starbish is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-06-2015 , 14:01   Re: [ASK, HELP] GetFileTypeName
Reply With Quote #8

The FindCharInString stock might make this simpler, plus you benefit from any optimizations that the SourceMod devs make to FindCharInString later (well... if the plugin is recompiled again later):

Code:
stock bool:GetFileTypeName(const String:FilePath[], String:FileType[], FileTypeLen)
{
    new loc = FindCharInString(FilePath, '.', true);
    if (loc == -1)
    {
        FileType[0] = '\0';
        return false;
    }

    strcopy(FileType, FileTypeLen, FilePath[loc]);
    return true;
}
Edit: This is what I get for delaying submitting this.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 10-06-2015 at 14:01.
Powerlord is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-06-2015 , 14:09   Re: [ASK, HELP] GetFileTypeName
Reply With Quote #9

Quote:
Originally Posted by Starbish View Post
that looks almost same as what i did.... except Format().

is strcopy() faster than Format()?
See the Edit to said post.

When I originally started typing that, there were no replies to the thread, but I got sidetracked before posting it.

As for strcopy versus Format, the answer to that is going to be yes, strcopy is faster. After all, a %s Format is going to do the same thing as strcopy does, but you have to parse the formatting string first...
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
PeEzZ
Senior Member
Join Date: Jan 2014
Location: Hungary
Old 10-06-2015 , 14:14   Re: [ASK, HELP] GetFileTypeName
Reply With Quote #10

Thanks
__________________
I MAKING MY PLUGINS/MAPS STILL, BUT I DON'T HAVE ENOUGH TIME, PLEASE STAND BY.
Sorry for my bad english, correct me if i'm wrong.
Magyarok írjanak PM-et ha fontos!

If you want, you can donate for me here
My plugins here / My maps here
PeEzZ 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 13:54.


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