Checking useable characters?

BlitzMax Forums/BlitzMax Beginners Area/Checking useable characters?

Amon(Posted 2007) [#1]
How would I check if "Line$" upon pressing enter has any characters in it that are illegal? The characters were chosen by GetCHar in my saving routine.

The Keycodes I'm checking for that would be legal to use in a filename are >47 and <91.

Thanks. :)


Grey Alien(Posted 2007) [#2]
Use InStr and Chr.

Or iterate through the string and compare each char (get a char using Mid) with your valid range by using Asc. This is probably quicker.


Perturbatio(Posted 2007) [#3]
ummm... if you're using getChar, can't you just check if the returned value is >47 and <91 before adding it to line$?

failing that:
Global line:String = "4444z"
Global invalid:Int = False

For Local i:Int = 0 To line.length-1
	Print line[i]
	If line[i] < 48 Or line[i] > 90 Then invalid = True
Next

If invalid Then Print "Invalid String: " + line Else Print "Valid String: "+line



xlsior(Posted 2007) [#4]
It's not quite so black and white:

character 58 : valid for a path, but invalid for an actual file
character 32 space valid in filenames
character 59 ; invalid in filename
character 60 < invalid in filename
character 62 > invalid in filename
character 63 ? invalid in filename

in addition to that, pretty much all the international characters are also perfectly valid for files: ïîìÄÅÉæÆôöò and many others. In English they may be unheard of, but many other languages commonly use at least a few of them - German, French, Spanish, Swedish, Norwegian, etc.


Czar Flavius(Posted 2007) [#5]
You could tell the user only to save using certain charectors (normal alphabet ones) only, and perhaps enforce this as a work-around.

What happens if you try and save a file with an invalid name? If it simply doesn't create the file, you could test the existance of the file afterwards to see if it was a valid name or not.


Amon(Posted 2007) [#6]
Thanks for the replies. The thing is my code was like perturbatio's but it wouldn't work.

I discovered that it was only working with numbers and capital letters.

ie with Perts code which was basically like mine it will only work with Capitals.

Here's the part of the code I've got it in.

						Case (char = 13) 
							Local Yes:Int
							Local i:Int
							For i = 0 To Line$.Length - 1
								If line[i]> 90 or line[i]< 47
									Yes = 0
								Else
									Yes = 1
								End If
							Next
							
							If Yes = 1
								WriteData = 1
								Exit
							Else
								Line$ = Null
							End If



The code above when used appears to only accept Capitals and number for "Yes" to equal 1. Then it saves the map.


Perturbatio(Posted 2007) [#7]
lowercase characters are higher than your range.




Grey Alien(Posted 2007) [#8]
yep 97 plus.


Amon(Posted 2007) [#9]
Hey Thanks. I was going by the keycodes list in the Max module reference.

All working fine now. :)