Raised This Month: $ Target: $400
 0% 

function for safe filenames?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sluggo
Member
Join Date: May 2006
Old 10-21-2006 , 23:15   function for safe filenames?
Reply With Quote #1

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.
sluggo is offline
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 10-22-2006 , 03:26   Re: function for safe filenames?
Reply With Quote #2

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.
Bad_Bud is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 10-22-2006 , 05:35   Re: function for safe filenames?
Reply With Quote #3

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.

Last edited by [ --<-@ ] Black Rose; 10-22-2006 at 05:41.
[ --<-@ ] Black Rose is offline
sluggo
Member
Join Date: May 2006
Old 10-22-2006 , 10:02   Re: function for safe filenames?
Reply With Quote #4

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!
sluggo is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 10-22-2006 , 11:07   Re: function for safe filenames?
Reply With Quote #5

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/
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>

Last edited by jim_yang; 10-22-2006 at 11:31.
jim_yang is offline
sluggo
Member
Join Date: May 2006
Old 01-21-2007 , 23:17   Re: function for safe filenames?
Reply With Quote #6

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.
sluggo is offline
XxAvalanchexX
Veteran Member
Join Date: Oct 2004
Location: abort73.com
Old 01-21-2007 , 23:24   Re: function for safe filenames?
Reply With Quote #7

"| | !" 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] = '_';      } }
__________________
No longer around. Thanks your support, everyone! As always:
THIS ONES FOR YOU
3000 PTS
XxAvalanchexX is offline
sluggo
Member
Join Date: May 2006
Old 01-21-2007 , 23:56   Re: function for safe filenames?
Reply With Quote #8

Wow, that's quick!
That's perfect, thank you!
sluggo is offline
XxAvalanchexX
Veteran Member
Join Date: Oct 2004
Location: abort73.com
Old 01-22-2007 , 00:25   Re: function for safe filenames?
Reply With Quote #9

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.
__________________
No longer around. Thanks your support, everyone! As always:
THIS ONES FOR YOU
3000 PTS
XxAvalanchexX is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 01-22-2007 , 00:31   Re: function for safe filenames?
Reply With Quote #10

i<len won't reach the \0
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang 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 04:54.


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