Filename Handling Class

I got tired of constantly coding something like this...
	char 	drive[_MAX_DRIVE],
           	path[_MAX_PATH],
           	fileName[_MAX_FNAME),
           	ext[_MAX_EXT);
	CString newPath;
 
    	_splitpath(fullpathname,  drive, path, fileName, ext):
	.
	.
	.
	newPath = drive;
	newPath += path + fileName;
	newPath += differentExtension;
	.
	.
	.
	//	etc
		

I just added zip creation and self extracting exe functionality to my app and found myself coding this stuff yet again and thought - there has to be a better way. Then I remembered all the other places in my code that have similar lines - license checking, history files, and decided it's time I had a filespec class.

Herewith CFileSpec

This class can take a full or partial path name and will split it into the appropriate parts. You can change any part of the path and when you reference the whole object what comes back is a full filespec
class CFileSpec
{
public:
	enum FS_BUILTINS
	{
		FS_EMPTY,		// Nothing
		FS_APP,			// Full application path and name
		FS_APPDIR,		// Application folder
		FS_WINDIR,		// Windows folder
		FS_SYSDIR,		// System folder
		FS_TMPDIR,		// Temporary folder
		FS_DESKTOP,		// Desktop folder
		FS_FAVOURITES,		// Favourites folder
		FS_TEMPNAME		// Create a temporary name
	};

				CFileSpec(FS_BUILTINS spec = FS_EMPTY);
				CFileSpec(LPCTSTR spec);

//	Operations
	BOOL			Exists();

//	Access functions
	CString&		Drive()				{ return m_drive; }
	CString&		Path()				{ return m_path; }
	CString&		FileName()			{ return m_fileName; }
	CString&		Extension()			{ return m_ext; }
	CString			FullPathNoExtension();

				operator LPCTSTR()		{ return GetFullSpec(); }          // as a C string

	CString			GetFullSpec();
	void			SetFullSpec(LPCTSTR spec);
	void			SetFullSpec(FS_BUILTINS spec = FS_EMPTY);
	
	CString			GetFileNameEx();
	void			SetFileNameEx(LPCTSTR spec);

private:
	void			Initialise();
	void			Initialise(FS_BUILTINS spec);
	void			UnLock();
	void			GetShellFolder(LPCTSTR folder);

	CString			m_drive,
				m_path,
				m_fileName,
				m_ext;
};

Each part of a full file spec is independently accessible via reference so you can code something like

	CFileSpec fs(CFileSpec::FS_APP);
	CFile	  file;

	fs.Extension = ".lic";
	file.Open(fs, CFile::modeRead | CFile::typeBinary);

which initialises the filespec to the applications full path including drive letter and then changes the extension to .lic As you can see the dot isn't implicit - you do need to supply it.

There is an operator to return a LPCTSTR so that the CFileSpec object can be used in calls just like a CString object can.

There are two constructors. One takes an existing path name and the other takes an enumerated constant. The constants allow you to easily specify certain predefined paths. The list is documented in the class header file.

Note that you need to be careful when passing a path to the object. If you want to assign a path such as c:\winnt\system32 it is necessary to append a \ otherwise the class will assume the last name in the path is actually the filename.

Download source - 2KB



Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 13, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT If you are developing applications, you'll want to join us to learn how applications are changing as a result of gesture recognition. This technology will change how you and your users interact - not simply with your devices, but with the world around you. Your devices will be able to see and hear what your users are doing. Are your applications ready for this? Join us to learn about Intel® RealSense™ Technology, including never been …

  • Live Event Date: July 30, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this upcoming eSeminar that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds