SOL9 2.0 Class: Folder

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * Folder.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
//
//    Folder.h
// FilePath splitter and directory(folder) creator
// 2008/09/29 Fixed bugs in splitPath method and exists method.

#pragma once

#include <sol\Object.h>
#include <sol\Exception.h>
#include <sol/String.h>
#include <sol/StringT.h>
#include <sol\StringConverter.h>


/** 
 */
namespace SOL {

class Folder :public Object {

private:

    wchar_t directory[_MAX_PATH];

    wchar_t fileName[_MAX_PATH];

    /** fileName only without dotExtension */
    wchar_t name[_MAX_PATH];

public:
    /**
     * Split filePath into directory and filename.
     * @param filePath    This takes a file path string like:"c:\work\doc\some.txt"
     */
    Folder(const char* filePath)
    {
        StringConverter converter;
        wchar_t* wfilePath = converter.toWideChar(filePath);

        directory[0] = Zero;
        fileName [0] = Zero;
        name[0]         = Zero;

        bool rc = splitPath(wfilePath);

        delete [] wfilePath;

        if (rc ==false) {
            throw (int)E_FAIL;
        }
    }

public:
    /**
     * Split filePath into directory and filename.
     * @param filePath    This takes a file path string like:"c:\work\doc\some.txt"
     */
    Folder(const wchar_t* filePath)
    {
        directory[0] = Zero;
        fileName [0] = Zero;
        name[0]         = Zero;
        
        wchar_t wfilePath[_MAX_PATH];
        wcscpy_s(wfilePath, SizeOf(wfilePath), filePath);
        int rc = splitPath(wfilePath);

        if (rc == false) {
            printf("Failed to split\n");
            throw (int)E_FAIL;
        }
    }

public:
    /**
     */
    bool splitPath(wchar_t* path) {
        bool rc = false;
    
        wchar_t* delim = wcsrchr(path, (wchar_t)'\\');
        if (delim) {
            *delim = Zero;    //
            ++delim;

            wcscpy_s(this->directory, sizeof(this->directory), path);
            wcscpy_s(this->fileName, sizeof(this->fileName), delim);

            wchar_t* dot = wcsrchr(delim, (wchar_t)'.');
            if (dot) {
                *dot = Zero;
            }
            wcscpy_s(this->name, sizeof(this->name), delim);

            rc = true;
        } else {
            //<added date="2008/09/29">
            wcscpy_s(this->fileName, sizeof(this->fileName), path);

            wchar_t* dot = wcsrchr(path, (wchar_t)'.');
            if (dot) {
                *dot = Zero;
            }
            wcscpy_s(this->name, sizeof(this->name), path);
            //</added>
            rc = true;
            
        }
        return rc;
    }


public:    
    /**
     */
    bool  createFolder(SECURITY_ATTRIBUTES* attr=NULL) {
        bool rc = false;

        if (directory[0] == (wchar_t)'\0' || 
            wcscmp(directory, L".") == 0 ||
                wcscmp(directory, L"..") == 0) {
            return rc;
        }
        
        if (GetFileAttributesW(directory) != 0xffffffff) {
            //printf("Folder#make,1,already exists %s\n", directory);
            return true;
        }

        wchar_t dir[_MAX_PATH];
        wcscpy_s(dir, sizeof(dir), directory);

        wchar_t* ptr = dir;
        
        while(ptr) {
            //Find delimiter '\\' from the beginning of the dir.
            ptr = wcschr(ptr, (wchar_t)'\\');
            if(ptr) { 
                *ptr = (wchar_t)'\0';
                if (GetFileAttributesW(dir) == 0xffffffff) {
                    if (CreateDirectoryW(dir, attr)) {
                        //printf("Folder#make,2, created a directory%s\n", dir);

                        rc = true;
                    } else {
                        //printf("Folder#make,3,Failed to create a directory%s\n", dir);
                        rc = false;
                        break;
                    }
                }
                *ptr = '\\';

            } else {
                if (GetFileAttributesW(dir) == 0xffffffff) {
                    if (CreateDirectoryW(dir, attr)) {
                        //printf("Folder#make,4,Created a directory %s\n", dir);
                        rc = true;
                    } else {
                        //printf("Folder#make,5, Failed to create a directory3 %s\n", dir);
                        rc = false;
                    }
                } else {
                    rc = true;
                }
                break;
            }
            ptr++;
        }

        return rc;
    }
    

public:    
    /**
     */
    bool exists() {
        bool rc = true;
        //2008/09/29: Added wcslen(direcotry)>0 
        if (wcslen(directory)>0 && GetFileAttributesW(directory) == 0xFFFFFFFF) {
            rc = false;    
        } 
        return rc;

    }

public:
    wchar_t* getDirectory()
    {
        return this->directory;
    }

public:
    wchar_t* getFileName()
    {
        return this->fileName;
    }

public:
    wchar_t* getName()
    {
        return this->name;
    }

public:
    void  getDirectory(String& dir) {
#ifdef UNICODE
        dir = this->directory;
#else
        StringConverter converter;
        char* mbstring = converter.toMultiByte(this->directory);
        dir.shallowCopy(mbstring);
#endif
    }

public:
    void  getFileName(String& fname) {
#ifdef UNICODE
        fname = this->fileName;
#else
        StringConverter converter;
        char* mbstring = converter.toMultiByte(this->fileName);
        fname.shallowCopy(mbstring);
#endif

    }    

public:
    /**
     * Return file name only without extension
     */
    void getName(String& name) {
#ifdef UNICODE
        name = this -> name;
#else
        StringConverter converter;
        char* mbstring = converter.toMultiByte(this->name);
        name.shallowCopy(mbstring);
#endif

    }
};

}


Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.