VIZ++ Class: CharFileReader

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

Source code

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


//2016/02/27
//2016/07/10 Modified to specify share mode in opeReadOnly.
#pragma once

#include <viz++/File.h>

namespace VIZ {

class CharFileReader :public File {
private:
  char*  data;
  size_t size;
  
public:
  CharFileReader(const TCHAR* fileName, bool nullTerminated=true)
  :File(),
  data(NULL),
  size(0)
  {
    //Set sharemode
    if (openReadOnly(fileName, FILE_SHARE_READ | FILE_SHARE_WRITE) == FALSE) {
      String name(fileName);
      throw IException("Failed to open a file. %s", (const char*)name.getCharString());
    }
    
    size = fileLength();
    if (nullTerminated) {
      data = new char[size+1];
    } else {
      data = new char[size];
    }
        
    memset(data, 0, size);
  
    size_t remain = size;
    char* ptr = data;
    while (remain > 0) {
      size_t len = read(ptr, remain);
      remain -= len;
      ptr += len;
    }
    if (nullTerminated) {
      data[size] = ZERO;
    }
    close();
  }
  

public:
  ~CharFileReader()
  {
    
    delete[] data;
    data = NULL;
  }  
public:
  const char* getData() const
  {
    return data;
  }
  
  const size_t getDataSize() const
  {
    return size;
  }
};

} 
   

Last modified: 10 Feb 2017

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