1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
|
/****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* OpenNI is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* OpenNI is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#ifndef ANDROID
// This define enables Large File Support (64bit version of I/O functions and data types).
// for more information - 'man 7 feature_test_macros'
// Moreover, it MUST be defined before ANY other include from this file.
#define _FILE_OFFSET_BITS 64
#define OFF_T off_t
#define LSEEK lseek
#else
#define OFF_T off64_t
#define LSEEK lseek64
#endif
#include <XnOS.h>
#include <libgen.h>
#include <errno.h>
#include <limits.h>
#include <XnLog.h>
//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
#ifndef XN_PLATFORM_LINUX_NO_GLOB
#include <glob.h>
XN_C_API XnStatus xnOSGetFileList(const XnChar* cpSearchPattern, const XnChar* cpPrefixPath, XnChar cpFileList[][XN_FILE_MAX_PATH], const XnUInt32 nMaxFiles, XnUInt32* pnFoundFiles)
{
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(cpSearchPattern);
XN_VALIDATE_OUTPUT_PTR(cpFileList);
XN_VALIDATE_OUTPUT_PTR(pnFoundFiles);
// Reset the number of found files counter
*pnFoundFiles = 0;
// now call the OS glob function
glob_t tGlob;
glob(cpSearchPattern, 0, NULL, &tGlob);
XnUInt32 nFoundFiles = XN_MIN(tGlob.gl_pathc, nMaxFiles);
for (XnUInt32 i = 0; i < nFoundFiles; ++i)
{
// Copy the file string into its place in the file list
xnOSStrCopy(cpFileList[i], tGlob.gl_pathv[i], XN_FILE_MAX_PATH);
if (cpPrefixPath != NULL)
{
xnOSStrPrefix(cpPrefixPath, cpFileList[i], XN_FILE_MAX_PATH);
}
}
// free memory allocated by OS
globfree(&tGlob);
// Return a file not found error if no files were found...
if (nFoundFiles == 0)
{
return (XN_STATUS_OS_FILE_NOT_FOUND);
}
// Write the temporary number of found files counter into the output
*pnFoundFiles = nFoundFiles;
// All is good...
return (XN_STATUS_OK);
}
#else
XN_C_API XnStatus xnOSGetFileList(const XnChar* cpSearchPattern, const XnChar* cpPrefixPath, XnChar cpFileList[][XN_FILE_MAX_PATH], const XnUInt32 nMaxFiles, XnUInt32* pnFoundFiles)
{
XN_ASSERT(FALSE);
return XN_STATUS_OS_FILE_NOT_FOUND;
}
#endif
XN_C_API XnStatus xnOSOpenFile(const XnChar* cpFileName, const XnUInt32 nFlags, XN_FILE_HANDLE* pFile)
{
// Local function variables
XnUInt32 nOSOpenFlags = 0;
XnStatus nRetVal = XN_STATUS_OK;
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(cpFileName);
XN_VALIDATE_OUTPUT_PTR(pFile);
// Update the OS Open flags according the user request
if ((nFlags & XN_OS_FILE_READ) && (nFlags & XN_OS_FILE_WRITE))
{
nOSOpenFlags |= O_RDWR | O_CREAT;
}
else if (nFlags & XN_OS_FILE_READ)
{
nOSOpenFlags |= O_RDONLY;
}
else if (nFlags & XN_OS_FILE_WRITE)
{
nOSOpenFlags |= O_WRONLY | O_CREAT;
}
if (nFlags & XN_OS_FILE_CREATE_NEW_ONLY)
{
// It's OK to create a new file, but fail if the file already exist
nOSOpenFlags |= O_EXCL;
}
if (nFlags & XN_OS_FILE_TRUNCATE)
{
// If the file exists, we need to truncate it to zero
nOSOpenFlags |= O_TRUNC;
}
if ((nFlags & XN_OS_FILE_WRITE) && (nFlags & XN_OS_FILE_AUTO_FLUSH))
{
nOSOpenFlags |= O_SYNC;
}
if (nFlags & XN_OS_FILE_APPEND)
{
nOSOpenFlags |= O_APPEND;
}
// Open the file via the OS (give read permissions to ALL)
*pFile = open(cpFileName, nOSOpenFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
// handle failure...
if (*pFile == XN_INVALID_FILE_HANDLE)
{
switch (errno)
{
case EEXIST:
return XN_STATUS_OS_FILE_ALREDY_EXISTS;
case ENOENT:
return XN_STATUS_OS_FILE_NOT_FOUND;
default:
return XN_STATUS_OS_FILE_OPEN_FAILED;
}
}
// All is good...
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSCloseFile(XN_FILE_HANDLE* pFile)
{
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(pFile);
// make sure this is a valid file descriptor
if (*pFile == XN_INVALID_FILE_HANDLE)
{
return XN_STATUS_OS_INVALID_FILE;
}
if (0 != close(*pFile))
{
// Something went wrong while trying to close the file...
return (XN_STATUS_OS_FILE_CLOSE_FAILED);
}
// make the user file descriptor invalid
*pFile = XN_INVALID_FILE_HANDLE;
return XN_STATUS_OK;
}
XN_C_API XnStatus xnOSReadFile(const XN_FILE_HANDLE File, void* pBuffer, XnUInt32* pnBufferSize)
{
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(pBuffer);
XN_VALIDATE_INPUT_PTR(pnBufferSize);
// Make sure the actual file handle isn't invalid
if (File == XN_INVALID_FILE_HANDLE)
{
return XN_STATUS_OS_INVALID_FILE;
}
// Read a buffer from a file handle via the OS
ssize_t nBytesRead = read(File, pBuffer, *pnBufferSize);
if (nBytesRead == -1)
{
return XN_STATUS_OS_FILE_READ_FAILED;
}
// update the number of bytes read
*pnBufferSize = nBytesRead;
// All is good...
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSWriteFile(const XN_FILE_HANDLE File, const void* pBuffer, const XnUInt32 nBufferSize)
{
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(pBuffer);
// Make sure the actual file handle isn't invalid
if (File == XN_INVALID_FILE_HANDLE)
{
return XN_STATUS_OS_INVALID_FILE;
}
// Write a buffer to a file handle via the OS
ssize_t nBytesWritten = write(File, pBuffer, nBufferSize);
// Make sure it succeeded (return value is not -1) and that the correct number of bytes were written
if ((nBytesWritten == -1) || (nBufferSize != nBytesWritten))
{
return XN_STATUS_OS_FILE_WRITE_FAILED;
}
// All is good...
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSSeekFile(const XN_FILE_HANDLE File, const XnOSSeekType SeekType, const XnInt32 nOffset)
{
// Local function variables
int nRealSeekType = 0;
OFF_T nRetOffset = 0;
// Make sure the actual file handle isn't invalid
if (File == XN_INVALID_FILE_HANDLE)
{
return XN_STATUS_OS_INVALID_FILE;
}
// Convert the Xiron seek type into OS seek type
switch (SeekType)
{
case XN_OS_SEEK_SET:
// Absolute seek from the file beginning
nRealSeekType = SEEK_SET;
break;
case XN_OS_SEEK_CUR:
// Relative seek from the current location
nRealSeekType = SEEK_CUR;
break;
case XN_OS_SEEK_END:
// Absolute seek from the file ending
nRealSeekType = SEEK_END;
break;
default:
return (XN_STATUS_OS_INVALID_SEEK_TYPE);
}
// Seek a file handle via the OS
nRetOffset = LSEEK(File, (OFF_T)nOffset, nRealSeekType);
// Make sure it succeeded (return value is valid) and that we reached the expected file offset
if (nRetOffset == (OFF_T)-1)
{
return (XN_STATUS_OS_FILE_SEEK_FAILED);
}
// All is good...
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSSeekFile64(const XN_FILE_HANDLE File, const XnOSSeekType SeekType, const XnInt64 nOffset)
{
// Local function variables
int nRealSeekType = 0;
OFF_T nRetOffset = 0;
// Make sure the actual file handle isn't invalid
if (File == XN_INVALID_FILE_HANDLE)
{
return XN_STATUS_OS_INVALID_FILE;
}
// Convert the Xiron seek type into OS seek type
switch (SeekType)
{
case XN_OS_SEEK_SET:
// Absolute seek from the file beginning
nRealSeekType = SEEK_SET;
break;
case XN_OS_SEEK_CUR:
// Relative seek from the current location
nRealSeekType = SEEK_CUR;
break;
case XN_OS_SEEK_END:
// Absolute seek from the file ending
nRealSeekType = SEEK_END;
break;
default:
return (XN_STATUS_OS_INVALID_SEEK_TYPE);
}
// Seek a file handle via the OS
nRetOffset = LSEEK(File, nOffset, nRealSeekType);
// Make sure it succeeded (return value is valid) and that we reached the expected file offset
if (nRetOffset == (OFF_T)-1)
{
return (XN_STATUS_OS_FILE_SEEK_FAILED);
}
// All is good...
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSTellFile(const XN_FILE_HANDLE File, XnUInt32* pnFilePos)
{
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_OUTPUT_PTR(pnFilePos);
// Make sure the actual file handle isn't invalid
if (File == XN_INVALID_FILE_HANDLE)
{
return XN_STATUS_OS_INVALID_FILE;
}
// Seek a file handle by 0 bytes in order to read the file position
OFF_T nFilePos = LSEEK(File, 0, SEEK_CUR);
// Make sure it succeeded (return value is valid)
if (nFilePos == (OFF_T)-1)
{
return (XN_STATUS_OS_FILE_TELL_FAILED);
}
// Enforce uint32 limitation
if ((nFilePos >> 32) != 0)
{
return XN_STATUS_INTERNAL_BUFFER_TOO_SMALL;
}
*pnFilePos = (XnUInt32)nFilePos;
// All is good...
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSTellFile64(const XN_FILE_HANDLE File, XnUInt64* pnFilePos)
{
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_OUTPUT_PTR(pnFilePos);
// Make sure the actual file handle isn't invalid
if (File == XN_INVALID_FILE_HANDLE)
{
return XN_STATUS_OS_INVALID_FILE;
}
// Seek a file handle by 0 bytes in order to read the file position
OFF_T nFilePos = LSEEK(File, 0, SEEK_CUR);
// Make sure it succeeded (return value is valid)
if (nFilePos == (OFF_T)-1)
{
return (XN_STATUS_OS_FILE_TELL_FAILED);
}
*pnFilePos = nFilePos;
// All is good...
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSFlushFile(const XN_FILE_HANDLE File)
{
// Make sure the actual file handle isn't invalid
if (File == XN_INVALID_FILE_HANDLE)
{
return XN_STATUS_OS_INVALID_FILE;
}
// flush via the OS
if (-1 == fsync(File))
{
return XN_STATUS_OS_FILE_FLUSH_FAILED;
}
return XN_STATUS_OK;
}
XN_C_API XnStatus xnOSFileExists(const XnChar* cpFileName, XnBool* bResult)
{
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(cpFileName);
XN_VALIDATE_OUTPUT_PTR(bResult);
// Reset the output result
*bResult = FALSE;
// Check if the file exists and update the result accordingly
if ((access(cpFileName, F_OK)) != -1)
{
*bResult = TRUE;
}
// All is good...
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSGetFileSize(const XnChar* cpFileName, XnUInt32* pnFileSize)
{
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(cpFileName);
XN_VALIDATE_OUTPUT_PTR(pnFileSize);
struct stat fileStat;
if (-1 == stat(cpFileName, &fileStat))
{
return (XN_STATUS_OS_FILE_GET_SIZE_FAILED);
}
// Enforce uint32 limitation
if (fileStat.st_size >> 32)
{
return XN_STATUS_INTERNAL_BUFFER_TOO_SMALL;
}
*pnFileSize = (XnUInt32)fileStat.st_size;
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSGetFileSize64(const XnChar* cpFileName, XnUInt64* pnFileSize)
{
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(cpFileName);
XN_VALIDATE_OUTPUT_PTR(pnFileSize);
struct stat fileStat;
if (-1 == stat(cpFileName, &fileStat))
{
return (XN_STATUS_OS_FILE_GET_SIZE_FAILED);
}
*pnFileSize = fileStat.st_size;
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSCreateDirectory(const XnChar* cpDirName)
{
// Local function variables
XnInt32 nRetVal = 0;
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(cpDirName);
nRetVal = mkdir(cpDirName, S_IRWXU | S_IRWXG | S_IRWXO);
if (nRetVal != 0)
{
return (XN_STATUS_OS_FAILED_TO_CREATE_DIR);
}
// All is good...
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSGetDirName(const XnChar* cpFilePath, XnChar* cpDirName, const XnUInt32 nBufferSize)
{
XnStatus nRetVal = XN_STATUS_OK;
// first copy the string (OS may change the argument)
XnChar cpInput[XN_FILE_MAX_PATH];
nRetVal = xnOSStrCopy(cpInput, cpFilePath, XN_FILE_MAX_PATH);
XN_IS_STATUS_OK(nRetVal);
// now call the OS
XnChar* cpResult = dirname(cpInput);
// copy result to user buffer
nRetVal = xnOSStrCopy(cpDirName, cpResult, nBufferSize);
XN_IS_STATUS_OK(nRetVal);
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSGetFileName(const XnChar* cpFilePath, XnChar* cpDirName, const XnUInt32 nBufferSize)
{
XnStatus nRetVal = XN_STATUS_OK;
// first copy the string (OS may change the argument)
XnChar cpInput[XN_FILE_MAX_PATH];
nRetVal = xnOSStrCopy(cpInput, cpFilePath, XN_FILE_MAX_PATH);
XN_IS_STATUS_OK(nRetVal);
// now call the OS
XnChar* cpResult = basename(cpInput);
// copy result to user buffer
nRetVal = xnOSStrCopy(cpDirName, cpResult, nBufferSize);
XN_IS_STATUS_OK(nRetVal);
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSGetFullPathName(const XnChar* strFilePath, XnChar* strFullPath, XnUInt32 nBufferSize)
{
XnStatus nRetVal = XN_STATUS_OK;
// pass a temp string (with max size)
XnChar strResult[PATH_MAX];
if (NULL == realpath(strFilePath, strResult))
{
xnLogWarning(XN_MASK_OS, "Failed getting full path name: errno is %d", errno);
return XN_STATUS_ERROR;
}
// now check if we can copy
if (strlen(strResult) >= nBufferSize)
{
return XN_STATUS_OUTPUT_BUFFER_OVERFLOW;
}
// and copy
strcpy(strFullPath, strResult);
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSGetCurrentDir(XnChar* cpDirName, const XnUInt32 nBufferSize)
{
XnStatus nRetVal = XN_STATUS_OK;
if (NULL == getcwd(cpDirName, nBufferSize))
{
if (errno == ERANGE)
return (XN_STATUS_OUTPUT_BUFFER_OVERFLOW);
else
return (XN_STATUS_ERROR);
}
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSSetCurrentDir(const XnChar* cpDirName)
{
if (0 != chdir(cpDirName))
{
return (XN_STATUS_ERROR);
}
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSDeleteFile(const XnChar* cpFileName)
{
// Local function variables
XnStatus nRetVal = XN_STATUS_OK;
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(cpFileName);
if (0 != unlink(cpFileName))
{
return (XN_STATUS_OS_FAILED_TO_DELETE_FILE);
}
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSDoesFileExist(const XnChar* cpFileName, XnBool* pbResult)
{
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(cpFileName);
XN_VALIDATE_OUTPUT_PTR(pbResult);
// Reset the output result
*pbResult = FALSE;
// Check if the file exists and update the result accordingly
if ((access(cpFileName, F_OK)) != -1)
{
*pbResult = TRUE;
}
// All is good...
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnOSDoesDirecotyExist(const XnChar* cpDirName, XnBool* pbResult)
{
// Validate the input/output pointers (to make sure none of them is NULL)
XN_VALIDATE_INPUT_PTR(cpDirName);
XN_VALIDATE_OUTPUT_PTR(pbResult);
// Reset the output result
*pbResult = FALSE;
// Check if the file exists and update the result accordingly
struct stat nodeStat;
if (stat(cpDirName, &nodeStat) == 0 &&
S_ISDIR(nodeStat.st_mode))
{
*pbResult = TRUE;
}
// All is good...
return (XN_STATUS_OK);
}
|