11 #include <sys/types.h>
15 #if defined(AJA_WINDOWS)
19 #elif defined(AJA_BAREMETAL)
34 #include <mach-o/dyld.h>
41 #if defined(AJA_WINDOWS)
45 int64_t FileTime_to_POSIX(FILETIME ft)
48 LARGE_INTEGER date, adjust;
49 date.HighPart = ft.dwHighDateTime;
50 date.LowPart = ft.dwLowDateTime;
53 adjust.QuadPart = 11644473600000 * 10000;
56 date.QuadPart -= adjust.QuadPart;
59 return (int64_t)(date.QuadPart / 10000000);
61 #elif defined(AJA_BAREMETAL)
67 char * val = getenv( key.c_str() );
68 return val ==
NULL ? string(
"") : string(val);
75 #if defined(AJA_WINDOWS)
99 #if defined(AJA_WINDOWS)
101 return _wstat(fileName.c_str(), &dummy) != -1;
102 #elif defined(AJA_BAREMETAL)
116 #if defined(AJA_WINDOWS)
118 return _stat(fileName.c_str(), &dummy) != -1;
119 #elif defined(AJA_BAREMETAL)
124 bool bExists = stat(fileName.c_str(), &dummy) != -1;
132 const std::wstring& fileName,
134 const int properties)
136 #if defined(AJA_WINDOWS)
137 DWORD desiredAccess = 0;
138 DWORD creationDisposition = 0;
139 DWORD flagsAndAttributes = 0;
144 (0 != fileName.length()))
150 desiredAccess |= GENERIC_READ;
151 shareMode = FILE_SHARE_READ;
155 desiredAccess |= GENERIC_WRITE;
156 shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
160 creationDisposition |= CREATE_ALWAYS;
162 creationDisposition |= CREATE_NEW;
164 creationDisposition |= TRUNCATE_EXISTING;
166 creationDisposition |= OPEN_EXISTING;
169 flagsAndAttributes |= FILE_FLAG_NO_BUFFERING;
171 mFileDescriptor = CreateFileW(
187 #elif defined(AJA_BAREMETAL)
202 const std::string& fileName,
204 const int properties)
206 #if defined(AJA_WINDOWS)
212 #elif defined(AJA_BAREMETAL)
217 string flagsAndAttributes;
219 if ((mpFile ==
NULL) && (0 != fileName.length()))
225 flagsAndAttributes =
"r";
230 flagsAndAttributes =
"w";
232 flagsAndAttributes =
"w+";
238 flagsAndAttributes =
"w+";
243 flagsAndAttributes =
"a+";
246 flagsAndAttributes =
"w+";
250 if (
true == flagsAndAttributes.empty())
255 mpFile = fopen(fileName.c_str(), flagsAndAttributes.c_str());
258 int fd = fileno(mpFile);
262 fcntl(fd, F_NOCACHE, 1);
283 #if defined(AJA_WINDOWS)
288 if (TRUE == CloseHandle(mFileDescriptor))
295 #elif defined(AJA_BAREMETAL)
304 retVal = fclose(mpFile);
319 #if defined(AJA_WINDOWS)
322 return (
NULL != mpFile);
330 #if defined(AJA_WINDOWS)
335 ReadFile(mFileDescriptor, pBuffer, length, &bytesRead,
NULL);
338 #elif defined(AJA_BAREMETAL)
347 bytesRead = read(fileno(mpFile), pBuffer, length);
349 bytesRead = fread(pBuffer, 1, length, mpFile);
352 retVal = uint32_t(bytesRead);
362 #if defined(AJA_WINDOWS)
363 buffer.resize(length);
364 uint32_t actual_bytes =
Read((uint8_t*) &buffer[0], length);
365 buffer.resize(actual_bytes);
367 #elif defined(AJA_BAREMETAL)
371 buffer.resize(length);
372 uint32_t actual_bytes =
Read((uint8_t*) &buffer[0], length);
373 buffer.resize(actual_bytes);
382 #if defined(AJA_WINDOWS)
383 DWORD bytesWritten = 0;
387 WriteFile(mFileDescriptor, pBuffer, length, &bytesWritten,
NULL);
390 #elif defined(AJA_BAREMETAL)
397 size_t bytesWritten = 0;
400 if ((bytesWritten = write(fileno(mpFile), pBuffer, length)) > 0)
402 retVal = uint32_t(bytesWritten);
407 if ((bytesWritten = fwrite(pBuffer, 1, length, mpFile)) > 0)
409 retVal = uint32_t(bytesWritten);
421 #if defined(AJA_WINDOWS)
422 return Write((uint8_t*) buffer.c_str(), (uint32_t)buffer.length());
424 return Write((uint8_t*) buffer.c_str(), (uint32_t)buffer.length());
432 #if defined(AJA_WINDOWS)
436 if (FlushFileBuffers(mFileDescriptor) != 0)
440 #elif defined(AJA_BAREMETAL)
447 int fd = fileno(mpFile);
461 #if defined(AJA_WINDOWS)
466 int64_t offset =
Tell();
472 if (SetEndOfFile(mFileDescriptor) != 0)
481 #elif defined(AJA_BAREMETAL)
488 int fd = fileno(mpFile);
491 int res = ftruncate(fd, size);
505 #if defined(AJA_WINDOWS)
509 LARGE_INTEGER liDistanceToMove;
510 liDistanceToMove.HighPart = 0;
511 liDistanceToMove.LowPart = 0;
513 LARGE_INTEGER liCurrentFilePointer;
515 BOOL status = SetFilePointerEx(mFileDescriptor, liDistanceToMove, &liCurrentFilePointer, FILE_CURRENT);
519 retVal = (int64_t)-1;
523 retVal = liCurrentFilePointer.QuadPart;
527 #elif defined(AJA_BAREMETAL)
535 retVal = lseek(fileno(mpFile), 0, SEEK_CUR);
537 retVal = (int64_t)ftello(mpFile);
547 #if defined(AJA_WINDOWS)
557 moveMethod = FILE_BEGIN;
561 moveMethod = FILE_CURRENT;
565 moveMethod = FILE_END;
571 LARGE_INTEGER liDistanceToMove;
572 liDistanceToMove.HighPart = (LONG)(distance>>32);
573 liDistanceToMove.LowPart = (DWORD)distance;
575 retVal = SetFilePointerEx(mFileDescriptor, liDistanceToMove,
NULL, moveMethod);
576 if ( retVal == TRUE )
583 #elif defined(AJA_BAREMETAL)
612 retVal = lseek(fileno(mpFile), (off_t)distance, whence);
614 retVal = fseeko(mpFile, (off_t)distance, whence);
629 #if defined(AJA_WINDOWS)
631 return FileInfo(createTime, modTime, size, filePath);
634 return FileInfo(createTime, modTime, size, filePath);
641 createTime = modTime = size = 0;
643 #if defined(AJA_WINDOWS)
646 createTime = modTime = size = 0;
653 if( GetFileTime(mFileDescriptor,&cTime,&aTime,&wTime) )
655 LARGE_INTEGER sizeInfo;
656 if(GetFileSizeEx(mFileDescriptor,&sizeInfo))
658 size = (int64_t)sizeInfo.QuadPart;
659 createTime = FileTime_to_POSIX(cTime);
660 modTime = FileTime_to_POSIX(wTime);
662 const DWORD maxFilePathLen = 2048;
663 filePath.resize(maxFilePathLen);
665 retVal = GetFinalPathNameByHandleA(mFileDescriptor, &filePath[0], maxFilePathLen, FILE_NAME_NORMALIZED);
669 filePath.resize(retVal);
679 #elif defined(AJA_BAREMETAL)
687 struct stat fileStatus;
688 int fd = fileno(mpFile);
689 int fErr = fstat(fd, &fileStatus);
693 size = fileStatus.st_size;
694 createTime = fileStatus.st_ctime;
695 modTime = fileStatus.st_mtime;
697 #if defined(AJA_LINUX)
703 filePath.resize(PATH_MAX);
704 n = readlink(procPath.c_str(), &filePath[0], PATH_MAX);
716 #elif defined(AJA_MAC)
720 filePath.resize(PATH_MAX);
721 if (fcntl(fd, F_GETPATH, &filePath[0]) != -1)
724 filePath.resize(strlen(filePath.c_str()));
732 #warning "'AJAFileIO::FileInfo' does not support path retrieval."
746 #if defined(AJA_WINDOWS)
749 if (0 != fileName.length())
751 if (DeleteFileA(fileName.c_str()))
757 #elif defined(AJA_BAREMETAL)
763 if (0 != fileName.length())
765 if (0 == unlink(fileName.c_str()))
778 #if defined(AJA_WINDOWS)
781 if (0 != fileName.length())
783 if (DeleteFileW(fileName.c_str()))
789 #elif defined(AJA_BAREMETAL)
806 const std::string& directory,
807 const std::string& filePattern,
808 std::vector<std::string>& fileContainer)
810 #if defined(AJA_WINDOWS)
811 WIN32_FIND_DATAA fileData;
813 string qualifiedName;
816 fileContainer.clear();
818 if ((0 != directory.length()) && (0 != filePattern.length()))
820 if (TRUE == SetCurrentDirectoryA(directory.c_str()))
823 (hSearch = FindFirstFileA(filePattern.c_str(), &fileData)))
825 qualifiedName = directory +
"/" + fileData.cFileName;
826 fileContainer.push_back(qualifiedName);
828 while (FindNextFileA(hSearch, &fileData) != 0)
830 qualifiedName = directory +
"/" + fileData.cFileName;
831 fileContainer.push_back(qualifiedName);
835 if (0 != fileContainer.size())
843 #elif defined(AJA_BAREMETAL)
848 struct dirent** ppNamelist;
851 string convertedPath;
853 char resolvedPath[PATH_MAX];
855 if ((0 != directory.length()) && (0 != filePattern.length()))
858 convertedPath = directory;
859 for (string::iterator it = convertedPath.begin();
860 it < convertedPath.end();
868 upperPattern = filePattern;
872 if (!realpath(convertedPath.c_str(), resolvedPath))
875 nEntries = scandir(resolvedPath, &ppNamelist, 0, alphasort);
879 for (
int ndx = 0; ndx < nEntries; ndx++)
881 char* pName = ppNamelist[ndx]->d_name;
884 char upperName[PATH_MAX];
886 size_t length = strlen( pName );
888 for (i = 0; i < length; i++)
890 upperName[i] = toupper( *pChar++ );
894 if (!fnmatch(upperPattern.c_str(), upperName, FNM_PERIOD))
896 fileEntry = (directory +
"/");
899 fileContainer.push_back(fileEntry);
913 const std::wstring& directory,
914 const std::wstring& filePattern,
915 std::vector<std::wstring>& fileContainer)
917 #if defined(AJA_WINDOWS)
918 WIN32_FIND_DATAW fileData;
920 wstring qualifiedName;
923 fileContainer.clear();
925 if ((0 != directory.length()) && (0 != filePattern.length()))
927 if (TRUE == SetCurrentDirectoryW(directory.c_str()))
930 (hSearch = FindFirstFileW(filePattern.c_str(), &fileData)))
932 qualifiedName = directory + L
"/" + fileData.cFileName;
933 fileContainer.push_back(qualifiedName);
935 while (FindNextFileW(hSearch, &fileData) != 0)
937 qualifiedName = directory + L
"/" + fileData.cFileName;
938 fileContainer.push_back(qualifiedName);
942 if (0 != fileContainer.size())
950 #elif defined(AJA_BAREMETAL)
959 vector<string> aContainer;
961 for(vector<string>::iterator i = aContainer.begin(); i != aContainer.end(); ++i)
965 fileContainer.push_back(tmp);
975 const std::string& directory,
976 const std::string& filePattern)
978 #if defined(AJA_WINDOWS)
979 WIN32_FIND_DATAA fileData;
984 if ((0 != directory.length()) && (0 != filePattern.length()))
986 if( !GetCurrentDirectoryA(
MAX_PATH, savePath) )
989 if (TRUE == SetCurrentDirectoryA(directory.c_str()))
992 (hSearch = FindFirstFileA(filePattern.c_str(), &fileData)))
999 SetCurrentDirectoryA(savePath);
1003 #elif defined(AJA_BAREMETAL)
1008 vector<string> fileList;
1010 if ((0 != directory.length()) && (0 != filePattern.length()))
1015 if( fileList.size() >= 2 )
1026 const std::wstring& directory,
1027 const std::wstring& filePattern)
1029 #if defined(AJA_WINDOWS)
1030 WIN32_FIND_DATAW fileData;
1035 if ((0 != directory.length()) && (0 != filePattern.length()))
1037 if( !GetCurrentDirectoryW(
MAX_PATH, savePath) )
1040 if (TRUE == SetCurrentDirectoryW(directory.c_str()))
1043 (hSearch = FindFirstFileW(filePattern.c_str(), &fileData)))
1050 SetCurrentDirectoryW(savePath);
1054 #elif defined(AJA_BAREMETAL)
1072 #if defined(AJA_WINDOWS)
1074 #elif defined(AJA_BAREMETAL)
1080 if (0 != directory.length())
1082 DIR* pDir = opendir( directory.c_str() );
1097 #if defined(AJA_WINDOWS)
1099 #elif defined(AJA_BAREMETAL)
1128 #if defined(AJA_WINDOWS)
1133 #elif defined(AJA_BAREMETAL)
1148 #if defined(AJA_WINDOWS)
1153 #elif defined(AJA_BAREMETAL)
1168 #if defined(AJA_WINDOWS)
1173 if (GetTempPathA(
MAX_PATH, &temp[0]))
1177 directory = &temp[0];
1186 #elif defined(AJA_BAREMETAL)
1192 vector<string> environ_vars;
1193 environ_vars.push_back(
"TMPDIR");
1194 environ_vars.push_back(
"TMP");
1195 environ_vars.push_back(
"TEMP");
1196 environ_vars.push_back(
"TEMPDIR");
1197 vector<string>::iterator it;
1199 for(it=environ_vars.begin() ; it != environ_vars.end() ; ++it)
1232 #if defined(AJA_WINDOWS)
1235 vector<wchar_t> temp;
1237 if (GetTempPathW(
MAX_PATH, &temp[0]))
1241 directory = &temp[0];
1250 #elif defined(AJA_BAREMETAL)
1271 char buf[AJA_MAX_PATH+1] =
"";
1273 #if defined(AJA_WINDOWS)
1274 _getcwd(buf, AJA_MAX_PATH);
1275 #elif defined(AJA_BAREMETAL)
1279 char* result = getcwd(buf, AJA_MAX_PATH);
1283 cwd = std::string(buf);
1303 const size_t lastSlashIndex = path.rfind(
AJA_PATHSEP);
1307 if (std::string::npos != lastSlashIndex) {
1308 directory = path.substr(0, lastSlashIndex);
1322 if (std::wstring::npos != lastSlashIndex) {
1323 directory = path.substr(0, lastSlashIndex);
1333 const size_t lastSlashIndex = path.rfind(
AJA_PATHSEP);
1337 if (std::string::npos != lastSlashIndex) {
1338 filename = path.substr(lastSlashIndex + 1, path.length() - lastSlashIndex);
1352 if (std::wstring::npos != lastSlashIndex) {
1353 filename = path.substr(lastSlashIndex + 1, path.length() - lastSlashIndex);
1363 #if defined(AJA_BAREMETAL)
1367 char buf[AJA_MAX_PATH];
1368 memset((
void*)buf, 0, AJA_MAX_PATH);
1370 #if defined(AJA_WINDOWS)
1371 bufSize = ::GetModuleFileNameA(
NULL, &buf[0], AJA_MAX_PATH);
1372 #elif defined(AJA_LINUX)
1373 bufSize = readlink(
"/proc/self/exe", &buf[0], AJA_MAX_PATH);
1374 #elif defined(AJA_MAC)
1375 uint32_t pathLen = 0;
1376 char exe_path[AJA_MAX_PATH];
1377 _NSGetExecutablePath(
NULL, &pathLen);
1378 if (_NSGetExecutablePath(&exe_path[0], &pathLen) == 0 && realpath(exe_path, buf)) {
1379 bufSize = (size_t)pathLen;
1383 path = std::string(buf, strlen(buf));
1394 #if defined(AJA_WINDOWS)
1395 wchar_t buf[AJA_MAX_PATH];
1396 size_t bufSize = ::GetModuleFileNameW(
NULL, &buf[0], AJA_MAX_PATH);
1398 path = std::wstring(buf, wcslen(buf));
1401 #elif defined(AJA_BAREMETAL)
1405 std::string pathStr;
1417 #if defined(AJA_WINDOWS)
1418 mFileDescriptor = (
HANDLE)_get_osfhandle(fileno(fp));
1419 #elif defined(AJA_BAREMETAL)