VIZ++ Class: File

 VIZ++ Class Library  VIZ++ Samples  VIZ++ ClassTree 

Source code

/*
 * File.h 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


#pragma once

#include <io.h>
#include <viz++\Object.h>
//2008/07/09

#include <viz++\InvalidHandleException.h>

#include <viz++\String.h>
#include <viz++\AlreadyOpenedException.h>


namespace VIZ {

class File :public Object {
  UINT  mode;  
  HANDLE  fh;
  // 1999.03.09
  
  String name;

 protected:
    File(HANDLE handle) {
      fh = handle;
  }

public:
  File()
    :fh(INVALID_HANDLE_VALUE),
    mode(NULL)
  {
  }

public:
  ~File()
  {
    close();
  }

public:
  BOOL create(const TCHAR* filePath) 
  {
    isAlreadyOpened();

    BOOL rc = FALSE;

    if (filePath) {
      // If already opened, close it

      this->fh = ::CreateFile(filePath,  //  Name of the file 
          GENERIC_READ|GENERIC_WRITE,  // Access mode 
          0,              // Share mode 
          NULL,          // Security descriptor 
          CREATE_ALWAYS,      // How to create 
          FILE_ATTRIBUTE_NORMAL,  // File attributes 
          NULL);           // Attributes to copy  
      if(this->fh != INVALID_HANDLE_VALUE) {
        rc = TRUE;
        this->name = filePath;
        mode = OF_CREATE;

      } else {
        throw InvalidHandleException("Failed to create a file", GetLastError());

      }
    }
    return rc;
  }

public:
  BOOL create(const TCHAR* filePath, 
    DWORD access, 
    DWORD share,
      SECURITY_ATTRIBUTES*  securityAttrs, 
      DWORD  howToCreate,  
      DWORD  attrsAndFlags, 
      HANDLE  hTemplateFile)
  {
    isAlreadyOpened();

    BOOL rc = FALSE;

    if (filePath) {
      // If already opened, close it

      this->fh = ::CreateFile(filePath, 
        access, share,
          securityAttrs, 
          howToCreate,  
          attrsAndFlags, 
          hTemplateFile);

      if(this->fh != INVALID_HANDLE_VALUE){
        this->mode = OF_CREATE;
        this->name = filePath;

        rc = TRUE;
      } else {
        throw InvalidHandleException("Failed to create a file", GetLastError());
      }
    }
    return rc;
  }         
  
public:
  void isAlreadyOpened()
  {
    if(this->fh != INVALID_HANDLE_VALUE) {
      throw AlreadyOpenedException("FileAlreadyOpened");
    }    
  }

public:    
  virtual void close() 
  {
    if(this->fh != INVALID_HANDLE_VALUE) {
      ::FlushFileBuffers(this->fh);
      ::CloseHandle(this->fh);

      this->fh = INVALID_HANDLE_VALUE;
    }
  }

public:
  //2012/02/11
  bool isUnicodeFile(const TCHAR* filename)
  {
    bool rc = false;
    if (openReadOnly(filename)) {
      if (fileLength() >=2) {
        unsigned char buffer[2];
        read(buffer, sizeof(buffer));

        if (buffer[0] == 0xff && buffer[1] == 0xfe) {
          rc = true;
        }
      }
      close();
    }
    return rc;
  }

public:
  HANDLE getHandle() {
    //2009/10/18 Added NULL check for this->fh handle. 
    if (this->fh == INVALID_HANDLE_VALUE || this->fh == NULL) {
      throw InvalidHandleException("File::getHandle,1,InvalidFileHandle\n");
    }
    return this->fh;
  }

public:
  long fileLength() 
  {
    HANDLE h = getHandle();

    return (long)::GetFileSize(h, NULL);
  }
public:
  virtual void flush() 
  {
    HANDLE h = getHandle();
    ::FlushFileBuffers(h);
  }

public:
  BOOL isExistent(const TCHAR* fileName) 
  {
    // 2008/07/09
    BOOL rc = FALSE;
    if (fileName) {
      if (GetFileAttributes(fileName) != 0xffffffff) {
        rc = TRUE;
      }
    }
    return rc;
  }

public:
  BOOL openReadOnly(const TCHAR* filePath, int shareMode=0) 
  {
    isAlreadyOpened();

    BOOL rc = FALSE;
    if (filePath) {

      this->fh = ::CreateFile(filePath,    //  Name of the file 
          GENERIC_READ,      // Access mode 
          shareMode,        // Ahare mode 
          NULL,          // Security descriptor 
          OPEN_EXISTING,      // How to create 
          FILE_ATTRIBUTE_READONLY,  // File attributes 
          NULL);           // Attributes to copy

      if(this->fh != INVALID_HANDLE_VALUE) {
        this->mode = OF_READ;
        this->name = filePath;

        rc = TRUE;  

      } else {

      }
    }
    return rc;
  }

public:
  BOOL openReadWrite(const TCHAR* filePath, int shareMode=0) 
  {
    isAlreadyOpened();

    BOOL rc = FALSE;
    if (filePath) {

      this->fh = ::CreateFile(filePath,    //  name of the file 
          GENERIC_READ|GENERIC_WRITE,  // access mode 
          shareMode,            // share mode 
          NULL,          // security descriptor 
          OPEN_EXISTING,      // how to create 
          FILE_ATTRIBUTE_NORMAL,  // file attributes 
          NULL           // attributes to copy  
         );  
      if(this->fh != INVALID_HANDLE_VALUE) {
        this->mode = OF_READWRITE;
        this->name = filePath;

        rc = TRUE;
      }
    }
    return rc;
  }

public:
  BOOL openWriteOnly(const TCHAR* filePath, int shareMode=0) 
  {
    isAlreadyOpened();

    BOOL rc = FALSE;

    if (filePath) {  
      this->fh = ::CreateFile(name,    //  name of the file 
        GENERIC_WRITE,      // access mode 
        shareMode,            // share mode 
        NULL,          // security descriptor 
        OPEN_EXISTING,      // how to create 
        FILE_ATTRIBUTE_NORMAL,  // file attributes 
        NULL);           // attributes to copy  
      if (this->fh != INVALID_HANDLE_VALUE) {
        this->mode = OF_WRITE;
        this->name = filePath;

        rc = TRUE;
      }
    }
    return rc;
  }

public:
  BOOL remove(const TCHAR* filePath) 
  {
    BOOL rc = FALSE;

    if (filePath) {
      rc = ::DeleteFile(filePath);
    }
    return rc;
  }

public:
  // dir FILE_BEGIN/FILE_CURRENT/FILE_END
  long seek(long pos, int dir) 
  {
    HANDLE h = getHandle();
    return SetFilePointer(h, pos, NULL, dir);
  }

public:
  long seekBegin(long pos) 
  {
    return seek(pos, FILE_BEGIN);
  }

public:
  long seekCurrent(long pos) 
  {
    return seek(pos, FILE_CURRENT);
  }

public:
  long seekEnd(long pos) 
  {
    return seek(pos, FILE_END);
  }

public:
  unsigned long read(__out char* buffer, __in unsigned long size) 
  {
    HANDLE h = getHandle();
    unsigned long len = 0;

    ::ReadFile(h, buffer, size, (unsigned long*)&len, NULL);
    return len;
  }

public:
  unsigned long write(__in char* buffer, __in unsigned long size) 
  {
    HANDLE h = getHandle();
    unsigned long len = 0;
    ::WriteFile(h, buffer, size, (unsigned long*)&len, NULL);
    return len;
  }


public:
  //buffer is a Null-terminated string
  unsigned long write(__in char* buffer) 
  {
    __in unsigned long size = strlen(buffer);

    HANDLE h = getHandle();
    unsigned long len = 0;
    ::WriteFile(h, buffer, size, (unsigned long*)&len, NULL);
    return len;
  }

//2009/10/18
public:
  unsigned long read(__out unsigned char* buffer, __in unsigned long size) 
  {
    HANDLE h = getHandle();
    unsigned long len = 0;

    ::ReadFile(h, buffer, size, (unsigned long*)&len, NULL);
    return len;
  }
//2009/10/18

public:
  unsigned long write(__in unsigned char* buffer, __in unsigned long size) 
  {
    HANDLE h = getHandle();
    unsigned long len = 0;
    ::WriteFile(h, buffer, size, (unsigned long*)&len, NULL);
    return len;
  }

public:
  unsigned long write(wchar_t* buffer, unsigned long charLen) 
  {
    HANDLE h = getHandle();
    unsigned long len = 0;
    unsigned long bytesLen = charLen*2;

    ::WriteFile(h, buffer, bytesLen, (unsigned long*)&len, NULL);
    return len;
  }

public:
  //buffer is a Null-terminated string
  unsigned long write(const wchar_t* buffer) 
  {
    unsigned long charLen = wcslen(buffer);
    HANDLE h = getHandle();
    unsigned long len = 0;
    unsigned long bytesLen = charLen*2;

    ::WriteFile(h, buffer, bytesLen, (unsigned long*)&len, NULL);
    return len;
  }

public:
  BOOL getCreationTime(FILETIME& creation) 
  {
    HANDLE h = getHandle();
    FILETIME lastAccess;
    FILETIME lastWrite;
    return ::GetFileTime(h, &creation, &lastAccess, &lastWrite);
  }
public:
  BOOL getLastAccessTime(FILETIME& lastAccess) 
  {
    HANDLE h = getHandle();
    FILETIME creation;
    FILETIME lastWrite;
    return ::GetFileTime(h, &creation, &lastAccess, &lastWrite);
  }

public:

  BOOL getLastWriteTime(FILETIME& lastWrite) 
  {
    HANDLE h = getHandle();
    FILETIME creation;
    FILETIME lastAccess;
    return ::GetFileTime(h, &creation, &lastAccess, &lastWrite);
  }


public:
  DWORD  getAttributes(const TCHAR* name) {
      return ::GetFileAttributes(name);
  }

public:
  int  getMode() { 
    return mode; 
  }

public:
  BOOL  getFileInformation(BY_HANDLE_FILE_INFORMATION* info) {
    HANDLE h = getHandle();
    return ::GetFileInformationByHandle(h, info);
  }

public:
  DWORD  getType() { 
    HANDLE h = getHandle();
    return ::GetFileType(h); 
  }
  
public:
  BOOL  getTime(FILETIME& creation, 
      FILETIME& lastAccess, FILETIME& lastWrite) {

    HANDLE h = getHandle();
    return ::GetFileTime(h, &creation, &lastAccess, &lastWrite);
  }

public:
  BOOL  setTime(const FILETIME* creation, 
        const FILETIME* lastAccess, const FILETIME* lastWrite) {
    HANDLE h = getHandle();
    return ::SetFileTime(h, creation, lastAccess, lastWrite);
  }

public:
  void  setEnd() {
    HANDLE h = getHandle();

    ::SetEndOfFile(h);
  }
};

}




Last modified: 10 Feb 2017

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