Raised This Month: $ Target: $400
 0% 

Help with files


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
3d0tz
Junior Member
Join Date: Feb 2022
Old 02-19-2022 , 14:40   Help with files
Reply With Quote #1

Hello, i have for an example the string "[de_dust2]"
How can i remove the [ ] from the string?

I want to read the map name from a file and do something when map is the same as the current map

Last edited by 3d0tz; 02-19-2022 at 14:44.
3d0tz is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-19-2022 , 14:45   Re: Help with files
Reply With Quote #2

replace_all() called once per character you want to remove (i.e. replace with an empty string).
__________________
fysiks is offline
3d0tz
Junior Member
Join Date: Feb 2022
Old 02-19-2022 , 14:48   Re: Help with files
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
replace_all() called once per character you want to remove (i.e. replace with an empty string).
Cool, i totally forgot this function.. i haven't touched amxx for a lot of time and forgot most of the avaliable functions
Ty again solved my problem
3d0tz is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 02-19-2022 , 19:10   Re: Help with files
Reply With Quote #4

Alternatively, you can do it directly when reading the line from the file, by reading it from the 2nd character all the way to the 2nd to last.

Code:
fgets(file, line[1], min(strlen(line) - 1, charsmax(line)))

The purpose of min() is to prevent longer lines from going over the maximum buffer length.
__________________

Last edited by OciXCrom; 02-19-2022 at 19:13.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-19-2022 , 19:30   Re: Help with files
Reply With Quote #5

Quote:
Originally Posted by OciXCrom View Post
Alternatively, you can do it directly when reading the line from the file, by reading it from the 2nd character all the way to the 2nd to last.

Code:
fgets(file, line[1], min(strlen(line) - 1, charsmax(line)))

The purpose of min() is to prevent longer lines from going over the maximum buffer length.
That will just put the first bracket in line[1] and line[0] would be some random character (and even maybe EOS, effectively breaking the 'line' string).

I feel you need to read it in and validate it before you actually do anything with it. You never know what random stuff can be on that line, especially after the last bracket.
__________________
fysiks is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 02-20-2022 , 07:16   Re: Help with files
Reply With Quote #6

Nevermind, my late-night logic was very off there.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
3d0tz
Junior Member
Join Date: Feb 2022
Old 02-21-2022 , 11:16   Re: Help with files
Reply With Quote #7

fysiks solved my problem
3d0tz is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 02-21-2022 , 11:48   Re: Help with files
Reply With Quote #8

This will not work properly if there is a bracket in the string.
Assume we have this

Code:
[[hello] world]
with fysiks approach, you'll end up with this

Code:
hello world
while the expected would be

Code:
[hello] world
the proper solution for this case is to skip the first character and copy until the last occurence of ] in the string

Code:
#include <amxmodx> public plugin_init() {     TestOurCoolFunction() } TestOurCoolFunction() {     new const string[] = "[[hello] world]"     new buf[32]     CopyUntilLastOf(string[1], "]", buf, charsmax(buf))     server_print("^t buffer is => %s", buf) } CopyUntilLastOf(const string[], const token[], out[], maxlen) {     new pos = -1     for (new i = strlen(string) - 1; i >= 0; i--)     {         if (equali(string[i], token))         {             pos = i             break         }     }     if (pos == -1)     {         // copy whole string if token isn't found         return copy(out, maxlen, string)     }     // copy until token     return copy(out, min(pos, maxlen), string) }

If the strings you're working with don't contain brackets you will be ok with fysiks method.

I made a gist at github with a more advanced CopyUntilLastOf function where you can escape the string you're searching for and specify the case-sensitivity

https://gist.github.com/SmiteIsTrash...e-function-sma
__________________








CrazY. 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:01.


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