/*! \brief Helper for file opening */ FILE* openFile(const struct XsString* filename, const struct XsString* mode) { #ifdef _WIN32 wchar_t filenameW[XS_MAX_FILENAME_LENGTH]; wchar_t modeW[16]; (void)XsString_copyToWCharArray(filename, filenameW, XS_MAX_FILENAME_LENGTH); (void)XsString_copyToWCharArray(mode, modeW, 16); return _wfopen(filenameW, modeW); #else return fopen(filename->m_data, mode->m_data); #endif }
/*! \relates XsFile \brief Retrieves the full path for a filename \param[in] filename The filename to expand \param[out] fullPath The filename with a fully expanded path is returned in this parameter. This parameter must be allocated by the caller. \returns XRV_OK if the fullpath could be retrieved, XRV_NULLPTR if fullPath is NULL, XRV_ERROR otherwise */ XsResultValue XsFile_fullPath(const struct XsString* filename, struct XsString* fullPath) { XsResultValue result = XRV_OK; if (fullPath == NULL) { result = XRV_NULLPTR; } else { #ifdef _WIN32 wchar_t filenamew[XS_MAX_FILENAME_LENGTH]; wchar_t fullpath[XS_MAX_FILENAME_LENGTH]; (void)XsString_copyToWCharArray(filename, filenamew, XS_MAX_FILENAME_LENGTH); if (_wfullpath(fullpath, filenamew, XS_MAX_FILENAME_LENGTH) == NULL) result = XRV_ERROR; else XsString_assignWCharArray(fullPath, fullpath); #else // based on the assumption that this doesn't concern the serial port, handle // it the same way using realpath(). Apparently realpath() doesn't require a // maximum length. One would possibly want to write a wrapper for it. char fullpath[XS_MAX_FILENAME_LENGTH*2]; if (realpath(filename->m_data, fullpath) == NULL) result = XRV_ERROR; else XsString_assignCharArray(fullPath, fullpath); #endif } return result; }
/*! \relates XsFile \brief Reopens a file \param filename Name of the file to open after the current one has been close \param mode Mode to reopen the file with \returns 0 if a file is open, another value otherwise */ XsResultValue XsFile_reopen(struct XsFile *thisPtr, const struct XsString* filename, const struct XsString* mode) { #ifdef _WIN32 wchar_t filenameW[XS_MAX_FILENAME_LENGTH]; wchar_t modeW[16]; (void)XsString_copyToWCharArray(filename, filenameW, XS_MAX_FILENAME_LENGTH); (void)XsString_copyToWCharArray(mode, modeW, 16); thisPtr->m_handle = _wfreopen(filenameW, modeW, thisPtr->m_handle); #else thisPtr->m_handle = freopen(filename->m_data, mode->m_data, thisPtr->m_handle); #endif if (thisPtr->m_handle == NULL) return XRV_OUTPUTCANNOTBEOPENED; else return XRV_OK; }
/*! \relates XsFile \brief Checks if the file exists (can be accessed) \param filename Name of the file to check for existence \returns 0 if the file exists, another value otherwise */ int XsFile_exists(const struct XsString* filename) { #ifdef _WIN32 wchar_t filenameW[XS_MAX_FILENAME_LENGTH]; struct _stat buffer; (void)XsString_copyToWCharArray(filename, filenameW, XS_MAX_FILENAME_LENGTH); return _wstat(filenameW, &buffer); #else struct stat buffer; return stat(filename->m_data, &buffer); #endif }
/*! \brief Dynamically load a library \param[in,out] thisp the XsLibraryLoader object handle \param[in] libraryName the name of the library to load. The library should be present in the current search path, or be specified by a path \return non-zero if the library could be loaded, zero otherwise */ int XsLibraryLoader_load(XsLibraryLoader* thisp, const XsString* libraryName) { #ifdef __GNUC__ if (XsLibraryLoader_isLoaded(thisp)) return 0; thisp->m_handle = dlopen(libraryName->m_data, RTLD_LAZY); #elif defined(_MSC_VER) wchar_t *libraryNameW; XsSize required; if (XsLibraryLoader_isLoaded(thisp)) return 0; required = XsString_copyToWCharArray(libraryName, NULL, 0); libraryNameW = (wchar_t*)malloc(required * sizeof(wchar_t)); (void)XsString_copyToWCharArray(libraryName, libraryNameW, required); thisp->m_handle = LoadLibrary(libraryNameW); free(libraryNameW); #endif return XsLibraryLoader_isLoaded(thisp); }
/*! \relates XsFile \brief Deletes a file with name \a filename \param filename Name of the file to delete \returns XRV_OK if the file was erased, an error otherwise */ XsResultValue XsFile_erase(const struct XsString* filename) { #ifdef _WIN32 wchar_t filenameW[XS_MAX_FILENAME_LENGTH]; (void)XsString_copyToWCharArray(filename, filenameW, XS_MAX_FILENAME_LENGTH); if (_wunlink(filenameW) != 0) #else if (unlink(filename->m_data) != 0) #endif { switch (errno) { case EACCES: return XRV_READONLY; case ENOENT: return XRV_NOTFOUND; default: return XRV_ERROR; } } else return XRV_OK; }