"Fossies" - the Fresh Open Source Software Archive

Member "tvnserver-2.0.4/file-lib/File.cpp" (3 Aug 2011, 7600 Bytes) of archive /windows/misc/tvnserver-2.0.4-src.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "File.cpp" see the Fossies "Dox" file reference documentation.

    1 // Copyright (C) 2008, 2009, 2010 GlavSoft LLC.
    2 // All rights reserved.
    3 //
    4 //-------------------------------------------------------------------------
    5 // This file is part of the TightVNC software.  Please visit our Web site:
    6 //
    7 //                       http://www.tightvnc.com/
    8 //
    9 // This program is free software; you can redistribute it and/or modify
   10 // it under the terms of the GNU General Public License as published by
   11 // the Free Software Foundation; either version 2 of the License, or
   12 // (at your option) any later version.
   13 //
   14 // This program is distributed in the hope that it will be useful,
   15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17 // GNU General Public License for more details.
   18 //
   19 // You should have received a copy of the GNU General Public License along
   20 // with this program; if not, write to the Free Software Foundation, Inc.,
   21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   22 //-------------------------------------------------------------------------
   23 //
   24 
   25 #include "File.h"
   26 
   27 #include "util/DateTime.h"
   28 #include <crtdbg.h>
   29 
   30 TCHAR File::s_separatorChar = _T('\\');
   31 
   32 File::File(const TCHAR *pathname)
   33 {
   34   m_pathName.setString(pathname);
   35 }
   36 
   37 File::File(const TCHAR *parent, const TCHAR *child)
   38 {
   39   m_pathName.setString(parent);
   40 
   41   if (!m_pathName.endsWith(File::s_separatorChar)) {
   42 
   43     TCHAR separator[2];
   44     separator[0] = File::s_separatorChar;
   45     separator[1] = '\0';
   46 
   47     m_pathName.appendString(&separator[0]);
   48   }
   49 
   50   m_pathName.appendString(child);
   51 }
   52 
   53 File::~File()
   54 {
   55 }
   56 
   57 bool File::canRead() const
   58 {
   59   return tryCreateFile(GENERIC_READ, OPEN_EXISTING);
   60 }
   61 
   62 bool File::canWrite() const
   63 {
   64   return tryCreateFile(GENERIC_WRITE, OPEN_EXISTING);
   65 }
   66 
   67 bool File::createNewFile() const
   68 {
   69   return tryCreateFile(GENERIC_READ | GENERIC_WRITE, CREATE_NEW);
   70 }
   71 
   72 bool File::remove() const
   73 {
   74   if (!exists()) {
   75     return false;
   76   }
   77   if ((isDirectory()) && (RemoveDirectory(m_pathName.getString()) != 0)) {
   78     return true;
   79   }
   80   if (isFile() && DeleteFile(m_pathName.getString()) != 0) {
   81     return true;
   82   }
   83   return false;
   84 }
   85 
   86 bool File::exists() const
   87 {
   88   WIN32_FIND_DATA fileInfo;
   89   return getFileInfo(&fileInfo);
   90 }
   91 
   92 void File::getName(StringStorage *name) const
   93 {
   94   if (m_pathName.getLength() == 0) {
   95     name->setString(_T(""));
   96     return ;
   97   }
   98 
   99   const TCHAR *buffer = m_pathName.getString();
  100   size_t i = m_pathName.getLength();
  101 
  102   for (; i > 0; i--) {
  103     if (buffer[i - 1] == File::s_separatorChar) {
  104       break;
  105     } 
  106   } 
  107 
  108   m_pathName.getSubstring(name, i, m_pathName.getLength() - 1);
  109 }
  110 
  111 void File::getFileExtension(StringStorage *ext) const
  112 {
  113   StringStorage fileName;
  114 
  115   getName(&fileName);
  116 
  117   int pointPos = fileName.findLast(_T('.'));
  118 
  119   if (pointPos == -1) {
  120     ext->setString(_T(""));
  121   } else {
  122     fileName.getSubstring(ext, pointPos + 1, fileName.getLength() - 1);
  123   }
  124 }
  125 
  126 bool File::isFile() const
  127 {
  128   if (exists() && !isDirectory()) {
  129     return true;
  130   }
  131   return false;
  132 }
  133 
  134 bool File::isDirectory() const
  135 {
  136   WIN32_FIND_DATA fileInfo;
  137   if (getFileInfo(&fileInfo)) {
  138     if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  139       return true;
  140     } 
  141   } 
  142   return false;
  143 }
  144 
  145 void File::getPath(StringStorage *pathname) const
  146 {
  147   *pathname = m_pathName;
  148 }
  149 
  150 UINT64 File::lastModified() const
  151 {
  152   WIN32_FIND_DATA fileInfo;
  153 
  154   if (!getFileInfo(&fileInfo)) {
  155     return 0;
  156   }
  157 
  158   DateTime dt(fileInfo.ftLastWriteTime);
  159 
  160   return dt.getTime();
  161 }
  162 
  163 UINT64 File::length() const
  164 {
  165   WIN32_FIND_DATA fileInfo;
  166 
  167   if (!getFileInfo(&fileInfo)) {
  168     return 0;
  169   }
  170 
  171   INT64 maxDWORDPlusOne = 1 + (INT64)MAXDWORD;
  172 
  173   return fileInfo.nFileSizeHigh * maxDWORDPlusOne + fileInfo.nFileSizeLow;
  174 }
  175 
  176 bool File::list(StringStorage *fileList, UINT32 *filesCount) const
  177 {
  178   StringStorage folderPath(m_pathName.getString());
  179   folderPath.appendString(_T("\\*"));
  180 
  181   UINT32 index = 0;
  182 
  183   HANDLE hfile;
  184   WIN32_FIND_DATA findFileData;
  185 
  186   UINT savedErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
  187 
  188   hfile = FindFirstFile(folderPath.getString(), &findFileData);
  189 
  190   SetErrorMode(savedErrorMode);
  191 
  192   if (hfile == INVALID_HANDLE_VALUE) {
  193     return false;
  194   }
  195 
  196   do {
  197 
  198     if (_tcscmp(findFileData.cFileName, _T(".")) == 0 ||
  199         _tcscmp(findFileData.cFileName, _T("..")) == 0) {
  200       continue;
  201     }
  202 
  203     if (fileList != NULL) {
  204       fileList[index].setString(findFileData.cFileName);
  205     }
  206 
  207     index++;
  208 
  209   } while (FindNextFile(hfile, &findFileData));
  210 
  211   FindClose(hfile);
  212 
  213   if (fileList == NULL) {
  214     *filesCount = index;
  215   }
  216 
  217   return true;
  218 }
  219 
  220 bool File::listRoots(StringStorage *rootList, UINT32 *rootsCount)
  221 {
  222   TCHAR drivesList[256];
  223 
  224   if (GetLogicalDriveStrings(255, drivesList) == 0) {
  225     return false;
  226   }
  227 
  228   UINT32 count = 0;
  229   int i = 0;
  230 
  231   while (drivesList[i] != '\0') {
  232 
  233     TCHAR *drive = _tcsdup(&drivesList[i]);
  234     TCHAR *backslash = _tcsrchr(drive, '\\');
  235 
  236     if (backslash != NULL) {
  237       *backslash = '\0';
  238     }
  239 
  240     if (rootList != NULL) {
  241       rootList[count].setString(drive);
  242     }
  243 
  244     free(drive);
  245 
  246     i += _tcscspn(&drivesList[i], _T("\0")) + 1;
  247     count++;
  248   } 
  249 
  250   if (rootList == NULL) {
  251     *rootsCount = count;
  252   }
  253 
  254   return true;
  255 }
  256 
  257 bool File::mkdir() const
  258 {
  259   if (CreateDirectory(m_pathName.getString(), NULL) == 0) {
  260     return false;
  261   }
  262   return true;
  263 }
  264 
  265 bool File::renameTo(const TCHAR *destPathName)
  266 {
  267   File destFile(destPathName);
  268   return renameTo(&destFile);
  269 }
  270 
  271 bool File::renameTo(File *dest)
  272 {
  273   StringStorage destPathName;
  274   dest->getPath(&destPathName);
  275   if (MoveFile(m_pathName.getString(), destPathName.getString()) == 0) {
  276     return false;
  277   }
  278   return true;
  279 }
  280 
  281 bool File::setLastModified(INT64 time)
  282 {
  283   _ASSERT(time >= 0);
  284 
  285   HANDLE hfile = CreateFile(m_pathName.getString(),
  286                             GENERIC_READ | GENERIC_WRITE,
  287                             0,
  288                             NULL,
  289                             OPEN_EXISTING,
  290                             FILE_ATTRIBUTE_NORMAL,
  291                             NULL);
  292   if (hfile == INVALID_HANDLE_VALUE) {
  293     return false;
  294   }
  295 
  296   FILETIME ft;
  297 
  298   DateTime dt((UINT64)time);
  299 
  300   dt.toFileTime(&ft);
  301 
  302   if (SetFileTime(hfile, NULL, NULL, &ft) == FALSE) {
  303     CloseHandle(hfile);
  304     return false;
  305   }
  306 
  307   CloseHandle(hfile);
  308 
  309   return true;
  310 }
  311 
  312 bool File::truncate()
  313 {
  314   if (exists()) {
  315     if (!remove()) {
  316       return false;
  317     }
  318   }
  319   return createNewFile();
  320 }
  321 
  322 bool File::getFileInfo(WIN32_FIND_DATA *fileInfo) const
  323 {
  324   HANDLE fileHandle;
  325 
  326   UINT savedErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
  327 
  328   fileHandle = FindFirstFile(m_pathName.getString(), fileInfo);
  329 
  330   SetErrorMode(savedErrorMode);
  331 
  332   if (fileHandle == INVALID_HANDLE_VALUE) {
  333     return false;
  334   } 
  335 
  336   FindClose(fileHandle);
  337   return true;
  338 }
  339 
  340 bool File::tryCreateFile(DWORD desiredAccess, DWORD creationDisposition) const
  341 {
  342   HANDLE hfile = CreateFile(m_pathName.getString(),
  343                             desiredAccess,
  344                             0,
  345                             NULL,
  346                             creationDisposition,
  347                             FILE_ATTRIBUTE_NORMAL,
  348                             NULL);
  349 
  350   if (hfile == INVALID_HANDLE_VALUE) {
  351     return false;
  352   }
  353 
  354   CloseHandle(hfile);
  355 
  356   return true;
  357 }