home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / source / xfile.cpp < prev    next >
C/C++ Source or Header  |  1998-03-27  |  16KB  |  619 lines

  1. #include "XFile.h"
  2. #include "XFileInf.h"
  3.  
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7.  
  8. /*@
  9. @class XIO
  10. @parent XObject
  11. @type overview
  12. @symbol _
  13. */
  14.  
  15. /*@
  16. @class XFile
  17. @parent XIO
  18. @type overview
  19. @symbol _
  20. */
  21.  
  22. ULONG XFile :: SetMaxFiles( ULONG handles )
  23. {
  24.    return DosSetMaxFH(handles);
  25. }
  26.  
  27.  
  28. /*@ XFile::GetFileInfo(XFileInfo * info)
  29. @group misc
  30. @remarks Query information about files. To access information the
  31. file must be open and XFILE_SHARE_DENYWRITE must be set!
  32. @parameters XFileInfo * pointer to an instance of XFileInfo
  33. @returns    LONG                    result of the operatingsystem
  34. */
  35. LONG XFile::GetFileInfo(XFileInfo * info)
  36. {
  37.    FILESTATUS3 buffer;
  38.  
  39.    LONG res = DosQueryFileInfo(handle, 1, &buffer, sizeof(buffer));
  40.    if(res == 0)
  41.    {
  42.       char * p = (char*) &info->buffer;
  43.       p += 4;
  44.       memcpy( p, &buffer, sizeof(buffer));
  45.    }
  46.    return res;
  47. }
  48.  
  49.  
  50. LONG XFile::GetPathInfo( const char * path, XFileInfo * info)
  51. {
  52.    FILESTATUS3 buffer;
  53.  
  54.    LONG res = DosQueryPathInfo( (PSZ) path, 1, &buffer, sizeof(buffer));
  55.    if(res == 0)
  56.    {
  57.       char * p = (char*) &info->buffer;
  58.       p += 4;
  59.       memcpy( p, &buffer, sizeof(buffer));
  60.    }
  61.    return res;
  62. }
  63.  
  64.  
  65. void XEAList :: ClearList()
  66. {
  67.    for(int i = 0; i <count; i++)
  68.       delete list[i];
  69.    if(list)
  70.       free (list);
  71.    count = 0;
  72.    list = NULL;
  73. }
  74.  
  75.  
  76. XEA :: ~XEA()
  77. {
  78.    if(value)
  79.       free(value);
  80. }
  81.  
  82.  
  83. /*@ XFile::GetEAList ( char*, XEAList* )
  84. @group EAs
  85. @parameters
  86. <t '°' c=2>
  87. °char * fileName  °name of the file
  88. °XEAList* list    °buffer of type XEAList
  89. </t>
  90. @returns ULONG result
  91. @remarks Read all EAs of a file.
  92. */
  93. ULONG XFile :: GetEAList( const char * path, XEAList * eaList)
  94. {
  95.    UCHAR buffer[500] = {0};
  96.    ULONG ulEnumCnt = 0;
  97.    FEA2 *ptr = NULL;
  98.    ULONG  ulTemp = 0;
  99.    ULONG rc;
  100.    ulEnumCnt = (ULONG)-1;
  101.  
  102.    rc = DosEnumAttribute(ENUMEA_REFTYPE_PATH, (PSZ) (char*) path, 1L, (PVOID)&buffer, sizeof(buffer), &ulEnumCnt, ENUMEA_LEVEL_NO_VALUE);
  103.  
  104.    if (rc == 0)
  105.    {
  106.       eaList->ClearList();
  107.       eaList->count = ulEnumCnt;
  108.       eaList->list = (XEA**) malloc( ulEnumCnt * sizeof(void*));
  109.       ptr = (FEA2 *)buffer;
  110.       for ( int i = 0; i < ulEnumCnt; i++)
  111.       {
  112.          eaList->list[i] = new XEA();
  113.          eaList->list[i]->name = ptr->szName;
  114.          ulTemp = ptr->oNextEntryOffset + (ULONG)ptr;
  115.          ptr = (FEA2 *)ulTemp;
  116.       }
  117.    }
  118.    else
  119.       return rc;
  120.  
  121.    for( int i = 0; i < ulEnumCnt; i++)
  122.    {
  123.       rc = XFile::GetEA( path, eaList->list[i], eaList->list[i]->name);
  124.       if(rc)
  125.          return rc;
  126.    }
  127.    return 0;
  128. }
  129.  
  130.  
  131. /*@ XFile::SetEAList ( char*, XEAList* )
  132. @group EAs
  133. @parameters
  134. <t '°' c=2>
  135. °char* filename   °name of the file
  136. °XEAList* list    °list of EAs to save
  137. </t>
  138. @returns ULONG result
  139. @remarks Save a list of EAs to a file
  140. */
  141. ULONG XFile :: SetEAList( const char * path, const XEAList * list)
  142. {
  143.    for(int i = 0; i < list->GetEACount(); i++)
  144.    {
  145.       ULONG rc = SetEA( path, list->GetEA(i));
  146.       if(rc)
  147.          return rc;
  148.    }
  149.    return 0;
  150. }
  151.  
  152.  
  153. /*@ XFile::GetEA ( char* fileName, XEA*ea, char* eaName)
  154. @group EAs
  155. @parameters
  156. <t '°' c=2>
  157. °char * fileName °name of the file
  158. °XEA * ea        °buffer to a XEA
  159. °char * eaName   °name of the EA to read
  160. </t>
  161. @returns ULONG result
  162. @remarks Read a single EA-entry of a file
  163. */
  164. ULONG XFile :: GetEA( const char * path, XEA * ea, const char * name)
  165. {
  166.    EAOP2 eaop;
  167.    eaop.fpGEA2List = (GEA2LIST*)new char[500];
  168.    eaop.fpFEA2List = (FEA2LIST*)new char[65536];
  169.  
  170.    eaop.oError = 0;
  171.    eaop.fpGEA2List->list[0].oNextEntryOffset = 0;
  172.    eaop.fpGEA2List->list[0].cbName = (BYTE) strlen(name);
  173.    strcpy(eaop.fpGEA2List->list[0].szName, name);
  174.  
  175.    eaop.fpGEA2List->cbList = strlen(name)
  176.     + sizeof(eaop.fpGEA2List->list[0].oNextEntryOffset)
  177.     + sizeof(eaop.fpGEA2List->list[0].cbName)
  178.     + sizeof(eaop.fpGEA2List->cbList);
  179.    if (eaop.fpGEA2List->cbList % 4)
  180.       eaop.fpGEA2List->cbList+= 4-(eaop.fpGEA2List->cbList % 4);
  181.  
  182.    eaop.fpFEA2List->cbList = 65536;
  183.  
  184.    ULONG rc = DosQueryPathInfo((PCSZ) path, FIL_QUERYEASFROMLIST, &eaop, sizeof(eaop));
  185.    if (rc)
  186.    {
  187.       delete eaop.fpGEA2List;
  188.       delete eaop.fpFEA2List;
  189.       return rc;
  190.    }
  191.    ea->name = eaop.fpFEA2List->list[0].szName;
  192.    ea->type = eaop.fpFEA2List->list[0].fEA;
  193.    char * p = eaop.fpFEA2List->list[0].szName + eaop.fpFEA2List->list[0].cbName + 1;
  194.    ea->valueSize = eaop.fpFEA2List->list[0].cbValue;
  195.    if( ea->valueSize)
  196.    {
  197.       ea->value = malloc(eaop.fpFEA2List->list[0].cbValue);
  198.       memcpy( ea->value, p, eaop.fpFEA2List->list[0].cbValue);
  199.    }
  200.    delete eaop.fpGEA2List;
  201.    delete eaop.fpFEA2List;
  202.    return 0;
  203. }
  204.  
  205.  
  206. /*@ XFile::SetEA ( char*, XEA*)
  207. @group EAs
  208. @parameters
  209. <t '°' c=2>
  210. °char * fileame   °name of the file
  211. °XEA* ea          °buffer with EA to save
  212. </t>
  213. @returns ULONG
  214. @remarks Save an EA. If the EA exists it will be replaced otherwise added to the EA-list of the file.
  215. <p>An EA can also removed from the file's EA-list when you set valueSize to zero of the EA to remove.
  216. */
  217. ULONG XFile :: SetEA( const char * path, const XEA * ea)
  218. {
  219.    EAOP2 eaop;
  220.    memset(&eaop, 0, sizeof(eaop));
  221.    eaop.fpFEA2List = (FEA2LIST*)new char[65536];
  222.  
  223.    eaop.fpFEA2List->list[0].fEA = ea->type;
  224.    eaop.fpFEA2List->list[0].oNextEntryOffset = 0;
  225.    eaop.fpFEA2List->list[0].cbValue = ea->valueSize;
  226.    eaop.fpFEA2List->list[0].cbName = ea->name.GetLength();
  227.    char * x = eaop.fpFEA2List->list[0].szName + eaop.fpFEA2List->list[0].cbName+1;
  228.    eaop.fpFEA2List->cbList = eaop.fpFEA2List->list[0].cbValue + x-(char*)eaop.fpFEA2List;
  229.    if (eaop.fpFEA2List->cbList % 4)
  230.       eaop.fpFEA2List->cbList+= 4-(eaop.fpFEA2List->cbList % 4);
  231.  
  232.    strcpy(eaop.fpFEA2List->list[0].szName, (char*) ea->name);
  233.    char * p = eaop.fpFEA2List->list[0].szName + eaop.fpFEA2List->list[0].cbName + 1;
  234.    if( ea->valueSize)
  235.       memcpy( p, ea->value, ea->valueSize);
  236.  
  237.    ULONG rc = DosSetPathInfo((PCSZ) path, FIL_QUERYEASIZE, &eaop, sizeof(eaop), DSPI_WRTTHRU);
  238.    delete eaop.fpGEA2List;
  239.    delete eaop.fpFEA2List;
  240.    return rc;
  241. }
  242.  
  243. /*@ XFile::SetFileInfo(XFileInfo * info)
  244. @group misc
  245. @remarks Set file-information. To access information the
  246. file must be open with XFILE_READONLY and XFILE_SHARE_DENYWRITE must be set!
  247. @parameters XFileInfo * pointer to an instance of XFileInfo
  248. @returns    LONG                    result of the operatingsystem
  249. */
  250. LONG XFile::SetFileInfo( const XFileInfo * info)
  251. {
  252.    FILESTATUS3 buffer;
  253.  
  254.    char * p = (char*) &info->buffer;
  255.    p += 4;
  256.    memcpy( &buffer, p, sizeof(buffer));
  257.  
  258.    return DosSetFileInfo(handle, FIL_STANDARD, &buffer, sizeof(buffer));
  259. }
  260.  
  261.  
  262. LONG XFile::SetPathInfo( const char * path, const XFileInfo * info)
  263. {
  264.    FILESTATUS3 buffer;
  265.  
  266.    char * p = (char*) &info->buffer;
  267.    p += 4;
  268.    memcpy( &buffer, p, sizeof(buffer));
  269.  
  270.    return DosSetPathInfo( (PSZ) path, FIL_STANDARD, &buffer, sizeof(buffer), DSPI_WRTTHRU);
  271. }
  272.  
  273.  
  274. /*@ XFile::Open(const char *filename, const ULONG modeifexist, const ULONG modeopen, const ULONG size, const EAOP2 * ealist)
  275. @group open/close
  276. @remarks Open a file
  277. @parameters <t '°' c=2>
  278.             °char * path                  °the path of the file
  279.             °ULONG modeForOpen            °how to open, possible values are:
  280. <BR>
  281. XFILE_REPLACE_EXISTING override existing filea
  282. <BR>
  283. XFILE_OPEN_EXISTING open if file exists
  284. <BR>
  285. XFILE_FAIL_EXISTING cancel if the file exists
  286. <BR>
  287. XFILE_FAIL_IF_NEW cancel if the file doesn∩t exist
  288. <BR>
  289. XFILE_CREATE_IF_NEW  create a new file if it doesn∩t exist
  290. <BR>
  291.                                          (can be or-ed)
  292.             °ULONG accessMode             °mode for access, possible values are:
  293. <BR>
  294. XFILE_READONLY
  295. <BR>
  296. XFILE_WRITEONLY
  297. <BR>
  298. XFILE_READWRITE
  299. <BR>
  300.                                          (can be or-ed)
  301.             °ULONG shareMode             °mode for file-sharing, possible values are:
  302. <BR>
  303. XFILE_SHARE_DENYREAD
  304. <BR>
  305. XFILE_SHARE_DENYWRITE
  306. <BR>
  307. XFILE_SHARE_DENYREADWRITE
  308. <BR>
  309. XFILE_SHARE_DENYNONE
  310. <BR>
  311.                                          (can be or-ed)
  312.             °ULONG size                   °size to open (only if a file is created, default is 0)
  313.             °EAOP2 * eaList               °list with extended attributes (default is NULL)
  314.             </t>
  315. @returns    ULONG                        result returned by the OS
  316. @updated _
  317. @also XEA, XEAList, XPipe, XNamedPipe, XSocket
  318. */
  319. ULONG XFile::Open(const char *filename, const ULONG modeifexist, const ULONG modeopen, const ULONG modeAccess, const ULONG size, const EAOP2 * ealist)
  320. {
  321.    ULONG aktion, res;
  322.  
  323.    res = DosOpen((PSZ) filename, &handle, &aktion, size, 0, modeifexist, modeopen|modeAccess, (PEAOP2) ealist);
  324.    if( res )
  325.    {
  326.       handle = -1;
  327.       isOpen = FALSE;
  328.    } /* end if */
  329.    else
  330.       isOpen = TRUE;
  331.    return res;
  332. }
  333.  
  334.  
  335. /*@ XFile::IsDriveAvaible(const char drive)
  336. @group disks
  337. @remarks Query if a drive is avaible or not
  338. @parameters char drive     the drive (A, B, ....)
  339. @returns    BOOL result
  340. */
  341. BOOL XFile::IsDriveAvaible(const char drive)
  342. {
  343.    SHORT number = drive - 'A';
  344.    ULONG u1 = 0, u2 = 0;
  345.  
  346.    DosQueryCurrentDisk(&u1, &u2);
  347.    return ((u2 << (31 - number) >> 31) ? TRUE : FALSE);
  348. }
  349.  
  350.  
  351. /*@ XFile::GetCurrentDirectory(XString * buffer)
  352. @group directorys
  353. @remarks Query the current directory
  354. @parameters XString * buffer to hold the data
  355. */
  356. void XFile::GetCurrentDirectory(XString * buffer)
  357. {
  358.    ULONG r = 512;
  359.  
  360.    DosQueryCurrentDir(0, (PCH) buffer->GetBuffer(512), &r);
  361.    buffer->ReleaseBuffer();
  362. }
  363.  
  364.  
  365. /*@ XFile::SetDefaultDisk(const char drive)
  366. @group disks
  367. @remarks Set the dault disk
  368. @parameters
  369. <t '°' c=2>
  370. °char °the drive (A, B, ....)
  371. </t>
  372. */
  373. BOOL XFile::SetDefaultDisk(const char drive)
  374. {
  375.    char d = toupper(drive);
  376.    USHORT number = d - 'A' + 1;
  377.  
  378.    if (DosSetDefaultDisk(number) == 0)
  379.       return TRUE;
  380.    else
  381.       return FALSE;
  382. }
  383.  
  384.  
  385. /*@ XFile::GetCurrentDisk(char &buffer)
  386. @group disks
  387. @remarks Query the current disk
  388. @parameters char&  buffer to hold data (will contain A,B,..)
  389. */
  390. void XFile::GetCurrentDisk(char &buffer)
  391. {
  392.    ULONG u1, u2;
  393.  
  394.    DosQueryCurrentDisk(&u1, &u2);
  395.    buffer = (char) u1 + 'A' - 1;
  396. }
  397.  
  398.  
  399. /*@ XFile::CreateDirectory(const char *path)
  400. @group directorys
  401. @remarks Creates a directory
  402. @parameters char * path of the directory to create
  403. */
  404. ULONG XFile::CreateDirectory(const char *path)
  405. {
  406.    return DosCreateDir((PSZ) path, NULL);
  407. }
  408.  
  409.  
  410. /*@ XFile::DeleteDirectory(const char *path)
  411. @group directorys
  412. @remarks Delete a directory
  413. @parameters char * path of the directory to delete
  414. */
  415. ULONG XFile::DeleteDirectory(const char *path)
  416. {
  417.    return DosDeleteDir((PSZ) path);
  418. }
  419.  
  420. ////////////////docs only
  421. /*@ XFileDialog::GetCommand()
  422. @group misc
  423. @remarks Returns the action of the user
  424. @returns    SHORT                        result: USER_OK or USER_CANCEL
  425. */
  426.  
  427.  
  428. /*@ XFileDialog::GetFileCount()
  429. @group misc
  430. @remarks Returns the number of files selected
  431. @returns    SHORT                        count of files
  432. */
  433.  
  434. /*@ XIO::Read(PVOID, ULONG)
  435. @group I/O
  436. @definition Read(PVOID, ULONG)
  437. @remarks Read from I/O
  438. @parameters PVOID buffer                 pointer to memory
  439. <BR>
  440.             ULONG size                   count of bytes to read
  441. @returns    ULONG                        count of bytes which are read
  442. */
  443.  
  444.  
  445. /*@ XIO::Read( XString&, ULONG)
  446. @group I/O
  447. @definiton Read( XString&, ULONG)
  448. @remarks Read from I/O
  449. @parameters XString&                 string-buffer
  450. <BR>
  451.             ULONG size                   count of bytes to read
  452. @returns    ULONG                        count of bytes which are read
  453. @fixed
  454. */
  455.  
  456.  
  457. /*@ XIO::Read(LONG&)
  458. @group I/O
  459. @definiton Read(LONG&)
  460. @remarks Read from I/O
  461. @parameters LONG&            buffer
  462. @returns    ULONG                        count of bytes which are read
  463. */
  464.  
  465. /*@ XIO::Read(SHORT&)
  466. @definiton Read(SHORT&)
  467. @group I/O
  468. @remarks Read from I/O
  469. @parameters SHORT&            buffer
  470. @returns    ULONG                        count of bytes which are read
  471. */
  472.  
  473.  
  474. /*@ XIO::Read(CHAR&)
  475. @definiton Read(CHAR&)
  476. @group I/O
  477. @remarks Read from I/O
  478. @parameters CHAR&            buffer
  479. @returns    ULONG                        count of bytes which are read
  480. */
  481.  
  482.  
  483. /*@ XIO::Read(XTime&)
  484. @definiton Read(XTime&)
  485. @group I/O
  486. @remarks Read from I/O
  487. @parameters XTime&            buffer
  488. @returns    ULONG                        count of bytes which are read
  489. */
  490.  
  491.  
  492. /*@ XIO::Read(XDate&)
  493. @definiton Read(XDate&)
  494. @group I/O
  495. @remarks Read from I/O
  496. @parameters XDate&            buffer
  497. @returns    ULONG                        count of bytes which are read
  498. */
  499.  
  500.  
  501. /*@ XIO::Write(PVOID, ULONG)
  502. @group I/O
  503. @remarks Write to I/O
  504. @parameters PVOID buffer                 pointer to memory
  505. <BR>
  506.             ULONG size                   count of bytes to write
  507. @returns    ULONG                        count of bytes which are written
  508. */
  509.  
  510. /*@ XIO::Write(XString&)
  511. @group I/O
  512. @remarks Writes a string to I/O including terminating NULL.
  513. @parameters XString&            string
  514. @returns    ULONG                        count of bytes which are written
  515. */
  516.  
  517. /*@ XIO::Write(LONG)
  518. @group I/O
  519. @remarks Write LONG to I/O
  520. @parameters LONG             data
  521. @returns    ULONG                        count of bytes which are written
  522. */
  523.  
  524. /*@ XIO::Write(short)
  525. @group I/O
  526. @remarks Write SHORT to I/O
  527. @parameters SHORT         data
  528. @returns    ULONG                        count of bytes which are written
  529. */
  530.  
  531. /*@ XIO::Write(char)
  532. @group I/O
  533. @remarks Write CHAR to I/O
  534. @parameters CHAR         data
  535. @returns    ULONG                        count of bytes which are written
  536. */
  537.  
  538. /*@ XIO::Write(XTime&)
  539. @group I/O
  540. @remarks Write a time to I/O
  541. @parameters XTime&         data in OOL-format
  542. @returns    ULONG                        count of bytes which are written
  543. */
  544.  
  545.  
  546. /*@ XIO::Write(XDate&)
  547. @group I/O
  548. @remarks Write a date to I/O
  549. @parameters XDate&         data in OOL-format
  550. @returns    ULONG                        count of bytes which are written
  551. */
  552.  
  553.  
  554. /*@ XIO::Close()
  555. @group open/close
  556. @remarks Close I/O
  557. */
  558.  
  559.  
  560. /*@ XIO::GetPointerPos()
  561. @group misc
  562. @remarks Returns the position of the file-pointer relative to the beginning of I/O. Before calling
  563. this function you must call XIO::Seek()! (e.g. Seek(0, XFILE_CURRENT) )
  564. @returns    ULONG                        the position
  565. */
  566.  
  567.  
  568. /*@ XIO::Seek()
  569. @group misc
  570. @remarks Seek in the file
  571. @parameters <t '°' c=2>
  572.             °ULONG position               °position to seek to relative to relPos
  573.             °ULONG relPos                 °position relative to:
  574. <BR>
  575. XFILE_BEGIN  relative to the beginning of the file (default)
  576. <BR>
  577. XFILE_CURRENT relative to current position
  578. <BR>
  579. XFILE_END       relative to the end of the file
  580.             </t>
  581. @returns    ULONG                        count of bytes which are read
  582. */
  583.  
  584.  
  585. /*@ XFile::Remove()
  586. @group misc
  587. @remarks Delete a file
  588. @parameters char * fileName   name of the file to delete (including path if nessecary)
  589. @returns    BOOL success
  590. */
  591.  
  592.  
  593. /*@ XFile::Copy()
  594. @group misc
  595. @remarks Copy a file
  596. @parameters char * from    file to copy (including path if nessecary)<BR>
  597.             char * to      destination (including path if nessecary)
  598. @returns    ULONG result   returncode from the OS.
  599. */
  600.  
  601.  
  602. /*@ XFile::Move()
  603. @group misc
  604. @remarks Move a file
  605. @parameters char * from    file to copy (including path if nessecary)<BR>
  606.             char * to      destination (including path if nessecary). If the same directory is given,
  607.                            Move() renames the file.
  608. @returns    ULONG result   returncode from the OS.
  609. */
  610.  
  611.  
  612. /*@ XFile::ResetBuffer()
  613. @group misc
  614. @remarks Wait until all writen datas are physicaly saved
  615. @returns    ULONG result   returncode from the OS.
  616. */
  617.  
  618.  
  619.