int ZipPlatform::OpenFile(LPCTSTR lpszFileName, UINT iMode, int iShareMode) { switch (iShareMode) { case (CZipFile::shareExclusive): iShareMode = SH_DENYRW; break; case (CZipFile::shareDenyRead): iShareMode = SH_DENYRD; break; case (CZipFile::shareDenyWrite): iShareMode = SH_DENYWR; break; default: iShareMode = SH_DENYNO; } #if _MSC_VER >= 1400 int handle; if (_tsopen_s(&handle, lpszFileName, iMode, iShareMode, S_IREAD | S_IWRITE /*required only when O_CREAT mode*/) != 0) return -1; else return handle; #else return _tsopen(lpszFileName, iMode, iShareMode, S_IREAD | S_IWRITE /*required only when O_CREAT mode*/); #endif }
bool StageRealizer::Save(const TCHAR* dir, const TCHAR* basename, const Stage& stage, const MoveSequence& seq) { { DWORD attr; std::basic_string< TCHAR > stagedir; stagedir = GetDirPath(dir, basename); attr = GetFileAttributes(stagedir.c_str()); if (attr == -1) { return false; } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { return false; } } if (! MoveRealizer::Save(dir, basename, seq)) { return false; } ::pb::Stage idea; if (! StageRealizer::Idealize(&idea, stage)) { return false; } std::basic_string< TCHAR > path, tmppath, oldpath; path = GetFilePath(dir, basename); tmppath.append(path).append(_T(".tmp")); oldpath.append(path).append(_T(".old")); { int fd = -1; errno_t err = _tsopen_s(&fd, tmppath.c_str(), _O_WRONLY|_O_BINARY|_O_CREAT|_O_TRUNC, _SH_DENYWR, _S_IREAD|_S_IWRITE); if (err != 0) { goto fail; } google::protobuf::io::FileOutputStream out(fd); bool b = google::protobuf::TextFormat::Print(idea, &out); out.Flush(); out.Close(); if (!b) { goto fail; } } if (! DeleteFile(oldpath.c_str())) { DWORD err = GetLastError(); if (err != ERROR_FILE_NOT_FOUND) { goto fail; } } MoveFile(path.c_str(), oldpath.c_str()); if (! MoveFile(tmppath.c_str(), path.c_str())) { goto fail; } return true; fail: return false; }
bool StageRealizer::Load(Stage** ppStage, MoveSequence** ppSeq, const TCHAR* basepath, const TCHAR* subdir, const MotionGuideList& mgl) { if (ppStage == NULL) { return false; } if (basepath == NULL || basepath[0] == _T('\0')) { return false; } if (subdir == NULL || subdir[0] == _T('\0')) { return false; } if (! IsExist(basepath, subdir)) { return false; } MotionGuidePair mg; if (! MoveRealizer::IsExist(&mg, basepath, subdir, mgl)) { return false; } Stage* pStage = NULL; MoveSequence* pSeq = NULL; { int fd; std::basic_string< TCHAR > path = GetFilePath(basepath, subdir); errno_t err = _tsopen_s(&fd, path.c_str(), _O_RDONLY|_O_BINARY, _SH_DENYWR, _S_IREAD|_S_IWRITE); if (err != 0) { return false; } ::pb::Stage idea; google::protobuf::io::FileInputStream in(fd); bool parsed = google::protobuf::TextFormat::Parse(&in, &idea); _close(fd); if (!parsed) { return false; } std::basic_string< TCHAR > dirpath = GetDirPath(basepath, subdir); if (! StageRealizer::Realize(&pStage, idea, subdir, dirpath.c_str())) { return false; } } if (ppSeq) { if (! MoveRealizer::Load(&pSeq, basepath, subdir, mg)) { goto fail; } } *ppStage = pStage; if (ppSeq) { *ppSeq = pSeq; } return true; fail: if (pStage) { delete pStage; } if (pSeq) { delete pSeq; } if (ppStage) { *ppStage = NULL; } if (ppSeq) { *ppSeq = NULL; } return true; }
int openRW(const TCHAR *name) { int hFile=-1; errno_t error =_tsopen_s(&hFile,name, _O_BINARY | _O_RDWR | _O_NOINHERIT | _O_SEQUENTIAL, // open flags _SH_DENYRW, // share flags _S_IREAD | _S_IWRITE); // permission flag if (error) { _tprintf(L"Error opening file %s.\n",name); return -1; } else return hFile; }
int openWrite(const TCHAR* name) { if (exists(name)) _tremove(name); int hFile=-1; errno_t error =_tsopen_s(&hFile,name, _O_BINARY | _O_WRONLY | _O_CREAT | _O_NOINHERIT | _O_SEQUENTIAL, // open flags _SH_DENYRW, // share flags _S_IREAD | _S_IWRITE); // permission flag if (error) { _tprintf(L"Error creating file %s.\n",name); return -1; } else return hFile; }
void init(int ot) { m_dout = ot; if (m_dout == DOUT_FILE) { if (m_fd > 0) _close(m_fd); #if _MSC_VER >= 1400 errno_t e = _tsopen_s(&m_fd, _T("./debuglog.txt"), _O_APPEND|_O_CREAT|_O_TEXT|_O_WRONLY, _SH_DENYWR, _S_IWRITE); if (e != 0) { m_dout = DOUT_API; m_fd = -1; } #else m_fd = _topen(_T("./debuglog.txt"), _O_APPEND|_O_CREAT|_O_TEXT|_O_WRONLY, _S_IWRITE); if (m_fd == -1) m_dout = DOUT_API; #endif } }
FILE* __cdecl __topenfile( const _TSCHAR* filename, REG3 const _TSCHAR* mode, int shflag, FILE* str ) { REG2 int modeflag; int streamflag = _commode; int commodeset = 0; int scanset = 0; int whileflag; int filedes; REG1 FILE* stream; BOOL encodingFlag = FALSE; _ASSERTE(filename != NULL); _ASSERTE(mode != NULL); _ASSERTE(str != NULL); /* Parse the user's specification string as set flags in (1) modeflag - system call flags word (2) streamflag - stream handle flags word. */ /* Skip leading spaces */ while (*mode == _T(' ')) { ++mode; } /* First mode character must be 'r', 'w', or 'a'. */ switch (*mode) { case _T('r'): modeflag = _O_RDONLY; streamflag |= _IOREAD; break; case _T('w'): modeflag = _O_WRONLY | _O_CREAT | _O_TRUNC; streamflag |= _IOWRT; break; case _T('a'): modeflag = _O_WRONLY | _O_CREAT | _O_APPEND; streamflag |= _IOWRT; break; default: _VALIDATE_RETURN(("Invalid file open mode", 0), EINVAL, NULL); } /* There can be up to three more optional mode characters: (1) A single '+' character, (2) One of 't' and 'b' and (3) One of 'c' and 'n'. */ whileflag = 1; while (*++mode && whileflag) switch (*mode) { case _T(' '): /* skip spaces */ break; case _T('+'): if (modeflag & _O_RDWR) { whileflag = 0; } else { modeflag |= _O_RDWR; modeflag &= ~(_O_RDONLY | _O_WRONLY); streamflag |= _IORW; streamflag &= ~(_IOREAD | _IOWRT); } break; case _T('b'): if (modeflag & (_O_TEXT | _O_BINARY)) { whileflag = 0; } else { modeflag |= _O_BINARY; } break; case _T('t'): if (modeflag & (_O_TEXT | _O_BINARY)) { whileflag = 0; } else { modeflag |= _O_TEXT; } break; case _T('c'): if (commodeset) { whileflag = 0; } else { commodeset = 1; streamflag |= _IOCOMMIT; } break; case _T('n'): if (commodeset) { whileflag = 0; } else { commodeset = 1; streamflag &= ~_IOCOMMIT; } break; case _T('S'): if (scanset) { whileflag = 0; } else { scanset = 1; modeflag |= _O_SEQUENTIAL; } break; case _T('R'): if (scanset) { whileflag = 0; } else { scanset = 1; modeflag |= _O_RANDOM; } break; case _T('T'): if (modeflag & _O_SHORT_LIVED) { whileflag = 0; } else { modeflag |= _O_SHORT_LIVED; } break; case _T('D'): if (modeflag & _O_TEMPORARY) { whileflag = 0; } else { modeflag |= _O_TEMPORARY; } break; case _T('N'): modeflag |= _O_NOINHERIT; break; case _T(','): encodingFlag = TRUE; whileflag = 0; break; default: _VALIDATE_RETURN(("Invalid file open mode", 0), EINVAL, NULL); } if (encodingFlag) { static const _TSCHAR ccsField[] = _T("ccs="); static const _TSCHAR utf8encoding[] = _T("UTF-8"); static const _TSCHAR utf16encoding[] = _T("UTF-16LE"); static const _TSCHAR unicodeencoding[] = _T("UNICODE"); /* Skip spaces */ while (*mode == _T(' ')) { ++mode; } /* * The length that we want to compare is numbers of elements in * csField -1 since this number also contains NULL terminator */ if (_tcsncmp(ccsField, mode, (_countof(ccsField)) - 1) != 0) { _VALIDATE_RETURN(("Invalid file open mode", 0), EINVAL, NULL); } mode += _countof(ccsField) - 1; if (_tcsicmp(mode, utf8encoding) == 0) { mode += _countof(utf8encoding) - 1; modeflag |= _O_U8TEXT; } else if (_tcsicmp(mode, utf16encoding) == 0) { mode += _countof(utf16encoding) - 1; modeflag |= _O_U16TEXT; } else if (_tcsicmp(mode, unicodeencoding) == 0) { mode += _countof(unicodeencoding) - 1; modeflag |= _O_WTEXT; } else { _VALIDATE_RETURN(("Invalid file open mode", 0), EINVAL, NULL); } } /* Skip trailing spaces */ while (*mode == _T(' ')) { ++mode; } _VALIDATE_RETURN((*mode == _T('\0')), EINVAL, NULL); /* Try to open the file. Note that if neither 't' nor 'b' is specified, _sopen will use the default. */ if (_tsopen_s(&filedes, filename, modeflag, shflag, _S_IREAD | _S_IWRITE) != 0) { return (NULL); } /* Set up the stream data base. */ #ifndef CRTDLL _cflush++; /* force library pre-termination procedure */ #endif /* CRTDLL */ /* Init pointers */ stream = str; stream->_flag = streamflag; stream->_cnt = 0; stream->_tmpfname = stream->_base = stream->_ptr = NULL; stream->_file = filedes; return (stream); }