[vwnc] How can I check if a String is a valid System Filename?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[vwnc] How can I check if a String is a valid System Filename?

Gruenewald, Tom

Hello.
How can I check if a String is a valid system filename?

E.g. 'c:\temp\ foo' asFilename is not valid for Windows, because ' foo' includes a space character at the first position.

Including

  • Illegal characters.
  • Illegal volume name.
  • Improper space characters.
  • Double separators.
  • Exceeding maximum length.

The filename does not need to exist. It must be simply valid.

Thank you for your answers.

Best regards,
Tom Grünewald

________

Carl Zeiss Industrielle Messtechnik GmbH
Softwareentwicklung/Software Development

T o m G r ü n e w a l d

73446 Oberkochen, Germany
tel: +49.7364.20-8541
fax: +49.7364.20-4800
email: [hidden email]
http://www.zeiss.de/imt

Carl Zeiss Industrielle Messtechnik GmbH, Carl-Zeiss-Straße 22, 73447 Oberkochen
Aufsichtsratsvorsitzender: Dr. Dieter Kurz
Geschäftsführer: Dr. Rainer Ohnheiser, Felix Hoben, Hanspeter Mürle
Sitz der Gesellschaft: 73446 Oberkochen, Deutschland
Handelsregister: Amtsgericht Ulm, HRB 501561
USt-IdNr.: DE 811 515 346


----------------------------------------
This message is intended for a particular addressee only and may contain business or company secrets. If you have received this email in error, please contact the sender and delete the message immediately. Any use of this email, including saving, publishing, copying, replication or forwarding of the message or the contents is not permitted.


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] How can I check if a String is a valid System Filename?

thomas.hawker

Tom,

 

I am not sure all of those conditions can be tested, and certainly not simply.

 

One of the easiest checks is:

 

            valid := ([inputString asFilename] on: Error do: [:ex | ex return: nil])

                                    ifNil: [false]

                                    ifNotNil: [:fn | fn asString asUppercase = inputString asString asUppercase].

 

This should handle illegal characters, improper space characters (if those are checked, because they’re legal on Unix), and double separators.  Double separators are not usually an issue except in the old MacOS (pre-OSX), since the double colon (“::”) meant the equivalent of Unix’s “..” for parent directory.  They’re typically ignored, but the string comparison will detect the removal of duplicates.  If anything else got removed, say because of length restrictions, the two strings won’t compare either.  I don’t have the code up so I don’t know if there is a maximum length check in the various platform specializations.

 

Checking for legal volume names requires knowing that you’re on Windows (or VMS) and making explicit system library calls to validate the volume information.  There have been other postings on either the VW-DEV or VWNC lists on how to do that.  Without going through that hassle, you could extend the above to do the following instead:

 

            valid := ([inputString asFilename] on: Error do: [:ex | ex return: nil])

                                    ifNil: [false]

                                    ifNotNil: [:fn | (fn asString asUppercase = inputString asString asUppercase)

and: [[fn definitelyExists. true] on: Error do: [:ex | ex return: false]]].

 

I think this will work even if the file name references non-existent files or directories.  Because on Unix this would basically be a stat() call, any error in reaching the target would result in a failure.  Only if the stat actually succeeded would the associated primitive action return true.  But I’m not looking at the VM code, either, so I don’t know if some conditions would result in a false (e.g., “not found” for missing directories) and others would result in an exception (e.g., illegal file name string).  If they’re split, then the above should detect them.

                                  

Cheers!

 

Tom Hawker

Senior Framework Developer

Home

+1 (408) 274-4128

The Environment:

We take it personally

Office

+1 (408) 576-6591

Mobile

+1 (408) 835-3643

 


From: [hidden email] [mailto:[hidden email]] On Behalf Of Gruenewald, Tom
Sent: Tuesday, November 10, 2009 3:57 AM
To: [hidden email]
Subject: [vwnc] How can I check if a String is a valid System Filename?

 

Hello.
How can I check if a String is a valid system filename?

E.g. 'c:\temp\ foo' asFilename is not valid for Windows, because ' foo' includes a space character at the first position.

Including

  • Illegal characters.
  • Illegal volume name.
  • Improper space characters.
  • Double separators.
  • Exceeding maximum length.


The filename does not need to exist. It must be simply valid.

Thank you for your answers.

Best regards,
Tom Grünewald

________

Carl Zeiss Industrielle Messtechnik GmbH
Softwareentwicklung/Software Development

T o m G r ü n e w a l d

73446 Oberkochen, Germany
tel: +49.7364.20-8541
fax: +49.7364.20-4800
email: [hidden email]
http://www.zeiss.de/imt

Carl Zeiss Industrielle Messtechnik GmbH, Carl-Zeiss-Straße 22, 73447 Oberkochen
Aufsichtsratsvorsitzender: Dr. Dieter Kurz
Geschäftsführer: Dr. Rainer Ohnheiser, Felix Hoben, Hanspeter Mürle
Sitz der Gesellschaft: 73446 Oberkochen, Deutschland
Handelsregister: Amtsgericht Ulm, HRB 501561
USt-IdNr.: DE 811 515 346


----------------------------------------
This message is intended for a particular addressee only and may contain business or company secrets. If you have received this email in error, please contact the sender and delete the message immediately. Any use of this email, including saving, publishing, copying, replication or forwarding of the message or the contents is not permitted.

IMPORTANT NOTICE
Email from OOCL is confidential and may be legally privileged.  If it is not
intended for you, please delete it immediately unread.  The internet
cannot guarantee that this communication is free of viruses, interception
or interference and anyone who communicates with us by email is taken
to accept the risks in doing so.  Without limitation, OOCL and its affiliates
accept no liability whatsoever and howsoever arising in connection with
the use of this email.  Under no circumstances shall this email constitute
a binding agreement to carry or for provision of carriage services by OOCL,
which is subject to the availability of carrier's equipment and vessels and
the terms and conditions of OOCL's standard bill of lading which is also
available at http://www.oocl.com.

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc