AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   function for safe filenames? (https://forums.alliedmods.net/showthread.php?t=46232)

sluggo 10-21-2006 23:15

function for safe filenames?
 
Hi,
Just wondering if there is a function to create safe filenames?
If i use peoples nicks to create a file they often have "|" or "\" or "/", I have seen somewhere a function for "safe sql-queries" but is there one for filenames?

I have made my own but I don't think it is... well it's probably not that efficient, and I don't know how to check some characters :S

If someone could point me to an "safe filename" function or help me with this one I would be greatefull!
Code:
public myReplaceFileName(string[33]){     replace_all(string,32,":","_")     replace_all(string,32,"/","_")     replace_all(string,32,"\","_") // this one looks different in the compiler (the _ is blue instead of red... something is wrong?     replace_all(string,32,"?","_")     replace_all(string,32,"<","_")     replace_all(string,32,">","_")     replace_all(string,32,"|","_")     replace_all(string,32,"^"","_") // dont know if this works... want to replace " in the filename.     replace_all(string,32,"'","_")     replace_all(string,32,"`","_")     replace_all(string,32,"´","_") //  replace_all(string,32,"^","_") // dont know how to replace ^ }

I know some of the characters ARE valid in filenames but I don't want them in my filenames thats why they are in the function.

Bad_Bud 10-22-2006 03:26

Re: function for safe filenames?
 
You can try doing the carrot in single quotes. Single quotes stand for a single character. You'll probably have to do something other than the "replace_all" function to do it, though. I don't really know.

[ --<-@ ] Black Rose 10-22-2006 05:35

Re: function for safe filenames?
 
Quote:

Originally Posted by sluggo
Code:
replace_all(string,32,"^"","_")

It will work


Code:
//  replace_all(string,32,"^","_")
-->
Code:
replace_all(string,32,"^^","_")


Quote:

Originally Posted by sluggo
PHP Code:

replace_all(string,32,"\","_") // this one looks different in the compiler (the _ is blue instead of red... something is wrong? 


I assume you mean amxx studio. Nothing is wrong.

sluggo 10-22-2006 10:02

Re: function for safe filenames?
 
Thank you!
There is no more efficent way to do what i'm trying to do it?

Well nevermind, this works and if there isn't a verry good way of doing it I'll stick to this.
Thanks again!

jim_yang 10-22-2006 11:07

Re: function for safe filenames?
 
Code:
public myReplaceFileName(string[]) {         new i = 0         new len = strlen(string)         new c         while(i < len)         {                 c = string[i]                 if(c == 47 || c == 58 || .....)                         string[i] = 95                 i++         } }
47, 58, 95 stuff are ASCII -> http://www.lookuptables.com/

sluggo 01-21-2007 23:17

Re: function for safe filenames?
 
Hi, again.
Don't know if I have stumbled onto a bug? :s

The replace_all works... sort of.
When I try to replace | <'more' in unix i believe the character is> it works ok.
When I try double || after eachother it works.
But the other day I was recording a guy with the nick "Ban |Vle p|Z !" = " Ban Me plZ ! " then it refused to record the demo.

I figured out that if i used the nick "| | !" it would not record but if i used "| |" it recorded. Well, when i say it didn't record I really mean it didn't create the file.
So... I'm lost here, why didn't replace_all work?
According to my code above with the corrections from Black Rose.

I'll try jim_yang's way to see if that solves it.

XxAvalanchexX 01-21-2007 23:24

Re: function for safe filenames?
 
"| | !" doesn't work
"| |" works

What's the difference? The exclamation mark. So replace your exclamation mark with an underscore as well. I'd just make a function like this, if it's for demos. It converts anything that isn't a letter or number to an underscore.

Code:
stock convert_to_alnum(string[]) {      new len = strlen(string), i;      for(i=0;i<len;i++)      {           if(!isalnum(string[i]))                string[i] = '_';      } }

sluggo 01-21-2007 23:56

Re: function for safe filenames?
 
Wow, that's quick!
That's perfect, thank you!

XxAvalanchexX 01-22-2007 00:25

Re: function for safe filenames?
 
I don't know what isalnum returns on the null character (string terminator), so to be sure, you might want to change "i<len" to "i<len-1", for safety.

jim_yang 01-22-2007 00:31

Re: function for safe filenames?
 
i<len won't reach the \0


All times are GMT -4. The time now is 04:54.

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