MCPcopy Create free account
hub / github.com/TorqueGameEngines/Torque3D / open

Method open

Engine/source/platformPOSIX/POSIXFileio.cpp:486–573  ·  view source on GitHub ↗

----------------------------------------------------------------------------- Open a file in the mode specified by openMode (Read, Write, or ReadWrite). Truncate the file if the mode is either Write or ReadWrite and truncate is true. Sets capability appropriate to the openMode. Returns the currentStatus of the file. -----------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

484// Returns the currentStatus of the file.
485//-----------------------------------------------------------------------------
486File::FileStatus File::open(const char *filename, const AccessMode openMode)
487{
488 AssertFatal(NULL != filename, "File::open: NULL filename");
489 AssertWarn(NULL == handle, "File::open: handle already valid");
490
491 // Close the file if it was already open...
492 if (Closed != currentStatus)
493 close();
494
495 char prefPathName[MaxPath];
496 char gamePathName[MaxPath];
497 char cwd[MaxPath];
498 getcwd(cwd, MaxPath);
499 MungePath(prefPathName, MaxPath, filename, GetPrefDir());
500 MungePath(gamePathName, MaxPath, filename, cwd);
501
502 int oflag;
503 struct stat filestat;
504 handle = (void *)dRealMalloc(sizeof(int));
505
506 switch (openMode)
507 {
508 case Read:
509 oflag = O_RDONLY;
510 break;
511 case Write:
512 oflag = O_WRONLY | O_CREAT | O_TRUNC;
513 break;
514 case ReadWrite:
515 oflag = O_RDWR | O_CREAT;
516 // if the file does not exist copy it before reading/writing
517 if (stat(prefPathName, &filestat) == -1)
518 bool ret = CopyFile(gamePathName, prefPathName);
519 break;
520 case WriteAppend:
521 oflag = O_WRONLY | O_CREAT | O_APPEND;
522 // if the file does not exist copy it before appending
523 if (stat(prefPathName, &filestat) == -1)
524 bool ret = CopyFile(gamePathName, prefPathName);
525 break;
526 default:
527 AssertFatal(false, "File::open: bad access mode"); // impossible
528 }
529
530 // if we are writing, make sure output path exists
531 if (openMode == Write || openMode == ReadWrite || openMode == WriteAppend)
532 Platform::createPath(prefPathName);
533
534 int fd = -1;
535 fd = x86UNIXOpen(prefPathName, oflag);
536 if (fd == -1 && openMode == Read)
537 // for read only files we can use the gamePathName
538 fd = x86UNIXOpen(gamePathName, oflag);
539
540 dMemcpy(handle, &fd, sizeof(int));
541
542#ifdef DEBUG
543 // fprintf(stdout,"fd = %d, handle = %d\n", fd, *((int *)handle));

Callers

nothing calls this directly

Calls 9

MungePathFunction · 0.85
GetPrefDirFunction · 0.85
x86UNIXOpenFunction · 0.85
dRealMallocFunction · 0.70
statClass · 0.70
CopyFileFunction · 0.70
dMemcpyFunction · 0.70
errorfFunction · 0.50
U32Enum · 0.50

Tested by

no test coverage detected