[C++] validate of CString geldige filename is

Pagina: 1
Acties:

  • skiwi
  • Registratie: Februari 2001
  • Laatst online: 05-04 14:13
Het kan aan mij liggen... maar ik kan met geen mogelijkheid in MSDN een functie vinden om na te gaan of een C++ CString een geldige windows filenaam zou zijn.

Een 'dirty' methode is een file met de desbetreffende filenaam aanmaken en de error code afvangen als de filenaam niet geldig is. maar ja...

[ Voor 3% gewijzigd door skiwi op 23-04-2003 16:06 ]


  • LordLarry
  • Registratie: Juli 2001
  • Niet online

LordLarry

Aut disce aut discede

FindFirst/FindNext/FindClose zou ook nog kunnen

We adore chaos because we like to restore order - M.C. Escher


  • skiwi
  • Registratie: Februari 2001
  • Laatst online: 05-04 14:13
De file bestaat nog niet. Het gaat erom dat een gebruiker zelf een filenaam kan specificeren. Die wil ik eerst valideren op ongeldige karakters zoals ? " * etc.

  • PKP B-Man
  • Registratie: November 2002
  • Laatst online: 19-07-2023
LordLarry schreef op 23 april 2003 @ 16:09:
FindFirst/FindNext/FindClose zou ook nog kunnen
Totdat je wil controleren of een filename voor het saven van een file juist is, dan bestaat deze file nog niet.

[edit:] Zeg ik >:)

[ Voor 5% gewijzigd door PKP B-Man op 23-04-2003 16:13 ]


  • PKP B-Man
  • Registratie: November 2002
  • Laatst online: 19-07-2023
Trouwens, als je gebruik maakt van een "file dialog box" is het niet nodig om de filename te controleren, quote uit de Win32 helpfile (topic: File and Directory Validation):
The dialog box automatically validates filenames typed by the user to ensure that the names contain only valid characters. To override the filename character validation, set the OFN_NOVALIDATE flag.

  • skiwi
  • Registratie: Februari 2001
  • Laatst online: 05-04 14:13
PKP B-Man schreef op 23 April 2003 @ 16:18:
Trouwens, als je gebruik maakt van een "file dialog box" is het niet nodig om de filename te controleren.
[...]
Weet ik... Sorry, ik had er iets meer over moeten vertellen: de gebruiker kan zelf een SQL server database naam opgeven. Wat SQL Server gaat doen is in de data directory files aanmaken met de opgegeven database naam. Als een gebruiker dus ongeldige karakters opgeeft gaat dit fout.
Ik wil dit dus eigenlijk afvangen voordat SQL Server hiermee aan de gang gaat.

  • MSalters
  • Registratie: Juni 2001
  • Laatst online: 21-08 17:14
Je kunt van een filenaam niet a priori vaststellen of het een valide naam is. Het probleem is dat je van een filenaam niet weet naar welke drive deze geschreven wordt, dus ook niet wat voor File system. Elk file systeem heeft eigen regels wat wel en niet valide is. Je kunt dus alleen een compleet pad valideren.

Man hopes. Genius creates. Ralph Waldo Emerson
Never worry about theory as long as the machinery does what it's supposed to do. R. A. Heinlein


  • Reptile209
  • Registratie: Juni 2001
  • Laatst online: 21:42

Reptile209

- gers -

misschien valt in de source van TFileOpenDialog te vinden hoe daar een file gevalideerd wordt -> inpikken? :P
Of misschien rolt er toch een Win32-call uit waar je wat mee kan.

Zo scherp als een voetbal!


  • madwizard
  • Registratie: Juli 2002
  • Laatst online: 26-10-2024

madwizard

Missionary to the word of ska

PSDK: "Naming a file"
[...]
Naming Conventions

The following rules enable applications to create and process valid names for files and directories regardless of the file system in use:


• Use a period (.) to separate the base file name from the extension in a directory name or file name.
• Backslashes (\) are used to separate components in paths (dividing the file name from the path to it, or directories from one another in a path). You cannot use them in file or directory names, however, they may be required as part of volume names— for example, "C:\". Note that UNC names must adhere to the following format: \\<server>\<share>.
• Use any character in the current code page for a name, except characters in the range 0 through 31 or any character explicitly disallowed by the file system. A name can contain characters in the extended character set (128–255). However, it cannot contain the following reserved characters:
< > : " / \ |

• The following reserved device names cannot be used as the name of a file: CON, PRN, AUX, CLOCK$, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed by an extension (for example, NUL.tx7).
• Do not assume case sensitivity. Consider names such as OSCAR, Oscar, and oscar to be the same.
• Do not end a file or directory name with a trailing space or a period. Although the underlying file system may support such names, the operating system does not.
• Use a period (.) as a directory component in a path to represent the current directory.
• Use two consecutive periods (..) as a directory component in a path to represent the parent of the current directory.
[...]
Staan verder nog wat dingen over maximale lengte enzo op diezelfde pagina.

www.madwizard.org


Verwijderd

- Windows 95 and NT:

- 1 to 255 character name.
- Complete path for a file or project name cannot exceed 259
characters, including the separators.
- May not begin or end with a space.
- May not begin with a $
- May contain 1 or more file extensions (eg. MyFile.Ext1.Ext2.Ext3.Txt).
- Legal characters in the range of 32 - 255 but not ?"/\<>*|:
- Filenames may be mixed case.
- Filename comparisons are case insensitive (eg. ThIs.TXT = this.txt).

  • skiwi
  • Registratie: Februari 2001
  • Laatst online: 05-04 14:13
Maar er is dus geen standaard klasse die controleert of een CString deze tekens bevat:

< > : " / \ |

Hmm, dan moeten we zelf wat gaan bouwen ;)

Verwijderd

De stl-string heeft een "find_first_of", hoogstwaarschijnlijk heeft CString die ook.

Om te checken of een std::string deze tekens bevat, zou je kunnen doen:
C++:
1
2
3
4
if (fileName.find_first_of("<>:\"\\|") != std::string::npos)
{
    //string not valid
}

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 22-08 13:19

.oisyn

Moderator Devschuur®

Demotivational Speaker

Dat kan met CString::FindOneOf ()

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.

Pagina: 1