home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / fio.cpp < prev    next >
C/C++ Source or Header  |  2001-11-05  |  12KB  |  513 lines

  1. /*
  2. ** Module   :FIO.CPP
  3. ** Abstract :File I/O and related routines
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. **
  7. ** Log: Fri  11/04/1997       Extracted from EDITOR.CPP
  8. */
  9.  
  10. #define INCL_DOSFILEMGR
  11. #define INCL_DOSERRORS
  12. #ifdef __EMX__
  13. extern "C" {
  14. #include <os2emx.h>
  15. }
  16. #else
  17. #include <os2.h>
  18. #endif
  19. #include <fio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <_ctype.h>
  23. #include <regexp.h>
  24. #include <version.h>
  25.  
  26. //----------------------------------------------------------------------
  27. //
  28. //----------------------------------------------------------------------
  29.  
  30. int _lopen(char *name, int mode)
  31. {
  32.     HFILE  hfFileHandle = 0L;
  33.     ULONG  ulAction     = 0;
  34.     ULONG  ulOpenMode   = 0;
  35.     ULONG  ulFlags      = OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE;
  36.     UCHAR  *uchFileName = (UCHAR*)name;
  37.     APIRET rc           = NO_ERROR;
  38.  
  39.     switch(mode)
  40.     {
  41.         case OP_READ:
  42.             ulOpenMode = OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
  43.             ulFlags   |= OPEN_ACCESS_READONLY;
  44.             break;
  45.  
  46.         case OP_WRITE:
  47.             ulOpenMode = OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS;
  48.             ulFlags   |= OPEN_ACCESS_WRITEONLY;
  49.             break;
  50.  
  51.         case OP_PIPE:
  52.             ulOpenMode = OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
  53.             ulFlags   |= OPEN_ACCESS_WRITEONLY;
  54.             break;
  55.     }
  56.  
  57.     rc = DosOpen((PCSZ)uchFileName,
  58.                  &hfFileHandle,
  59.                  &ulAction,
  60.                  0L,
  61.                  FILE_ARCHIVED | FILE_NORMAL,
  62.                  ulOpenMode,
  63.                  ulFlags,
  64.                  0L);
  65.     return (rc) ? -1:hfFileHandle;
  66. }
  67.  
  68. int _lread(int handle, void * buff, int len)
  69. {
  70.     ULONG  ulBytesRead    = 0;
  71.     APIRET rc;
  72.     rc = DosRead ((HFILE) handle,
  73.                   buff,
  74.                   len,
  75.                   &ulBytesRead);
  76.     return (rc) ? 0 : ulBytesRead;
  77. }
  78.  
  79. int _lwrite(int handle, void * buff, int len)
  80. {
  81.     ULONG  ulWrote = 0;
  82.     APIRET rc      = NO_ERROR;
  83.     rc = DosWrite ((HFILE) handle,
  84.                    buff,
  85.                    len,
  86.                    &ulWrote);
  87.  
  88.     return (rc) ? 0 : ulWrote;
  89. }
  90.  
  91. int _lclose(int handle)
  92. {
  93.     return DosClose((HFILE) handle);
  94. }
  95.  
  96. int _lsize(int handle)
  97. {
  98.     FILESTATUS3 fsts3ConfigInfo = {{0}};
  99.     ULONG       ulBufSize       = sizeof(FILESTATUS3);
  100.     APIRET      rc              = NO_ERROR;
  101.  
  102.     rc = DosQueryFileInfo((HFILE) handle,
  103.                            FIL_STANDARD,
  104.                            &fsts3ConfigInfo,
  105.                            ulBufSize);
  106.     return (rc) ? 0:fsts3ConfigInfo.cbFile;
  107. }
  108.  
  109. int _file_exists(char *name)
  110. {
  111.     APIRET      rc;
  112.     FILESTATUS3 stInfo = {{0}};
  113.  
  114.     rc = DosQueryPathInfo((PCH)name, FIL_STANDARD, &stInfo, sizeof(FILESTATUS3));
  115.  
  116.     return (rc) ? 0:1;
  117. }
  118.  
  119. int match_name(char *name, char *mask)
  120. {
  121.     int rc = 0;
  122.     char* pName = 0;
  123.  
  124.     pName = strrchr(name,'\\');
  125.  
  126.     if(pName)
  127.         pName++;
  128.     else
  129.         pName = name;
  130.  
  131.     regexp* reg = regcomp(mask);
  132.  
  133.     if(!reg)
  134.         return 0;   //No match
  135.  
  136.     reg->regcase = 1;
  137.  
  138.     rc = regexec(reg, pName);
  139.  
  140.     delete (char*)reg;
  141.  
  142.     return rc;
  143. }
  144.  
  145. static char *_dayNames[]=
  146. {
  147.     "Sun",
  148.     "Mon",
  149.     "Tue",
  150.     "Wed",
  151.     "Thu",
  152.     "Fri",
  153.     "Sat"
  154. };
  155.  
  156. int curr_date_str(char *date)
  157. {
  158.     DATETIME DateTime = {0};
  159.     APIRET   rc       = 0;
  160.     char* ptr;
  161.     int p[3];
  162.     int i;
  163.  
  164.     *date = 0;
  165.  
  166.     rc = DosGetDateTime(&DateTime);
  167.  
  168.     if(rc)
  169.         return rc;
  170.  
  171.     ptr = _dayNames[DateTime.weekday];
  172.     while(*ptr)
  173.         *date++ = *ptr++;
  174.  
  175.     *date++ = ' ';
  176.     *date++ = ' ';
  177.  
  178.     switch(iDateFmt)
  179.     {
  180.         case 1:
  181.             p[0] = DateTime.day;
  182.             p[1] = DateTime.month;
  183.             p[2] = DateTime.year;
  184.             break;
  185.  
  186.         case 2:
  187.             p[0] = DateTime.year;
  188.             p[1] = DateTime.month;
  189.             p[2] = DateTime.day;
  190.             break;
  191.  
  192.         default:
  193.             p[0] = DateTime.month;
  194.             p[1] = DateTime.day;
  195.             p[2] = DateTime.year;
  196.             break;
  197.     }
  198.     for(i = 0; i < 3; i++)
  199.     {
  200.         if(p[i] < 32)
  201.         {
  202.             *date++ = char(p[i] / 10 + '0');
  203.             *date++ = char(p[i] % 10 + '0');
  204.         }
  205.         else
  206.         {
  207.             *date++ = char((p[i] / 1000)       + '0');
  208.             *date++ = char((p[i] % 1000) / 100 + '0');
  209.             *date++ = char((p[i] % 100)  / 10  + '0');
  210.             *date++ = char((p[i] % 10)         + '0');
  211.         }
  212.         if(i < 2)
  213.             *date++ = char(cDateSep);
  214.     }
  215.     *date = 0;
  216.  
  217.     return 0;
  218. }
  219.  
  220. void make_short_name(char *name, char *buff)
  221. {
  222.     if(strrchr(name, '\\'))
  223.         name = strrchr(name, '\\') + 1;
  224.  
  225.     while(*name)
  226.         *buff++ = __to_upper(*name++);
  227.  
  228.     *buff = 0;
  229. }
  230.  
  231. char * _ld_file(char *name)
  232. {
  233.     int in = _lopen(name, OP_READ);
  234.  
  235.     char *orig_file = 0;
  236.  
  237.     unsigned flen = 0;
  238.  
  239.     if(in >= 0)
  240.         flen = _lsize(in);
  241.  
  242.     //orig_file = new char[flen + 1];
  243.  
  244.     APIRET rc = DosAllocMem((PPVOID)&orig_file,
  245.                             flen + 1, PAG_READ | PAG_WRITE | PAG_COMMIT);
  246.  
  247.     if(rc)
  248.         return 0;
  249.  
  250.     orig_file[flen] = 0;
  251.  
  252.     if(in >= 0)
  253.     {
  254.         _lread(in, orig_file, flen);
  255.         _lclose(in);
  256.     }
  257.  
  258.     return orig_file;
  259. }
  260.  
  261. void _fr_file(char *data)
  262. {
  263.     DosFreeMem(data);
  264. }
  265.  
  266. int get_ea(char *FileName, char *ea_name, char **ea_value)
  267. {
  268.     LONG rc;                    /* Ret code                   */
  269.     UCHAR geabuff[300];         /* buffer for GEA             */
  270.     PVOID fealist;              /* fealist buffer             */
  271.     EAOP2 eaop;                 /* eaop structure             */
  272.     PGEA2 pgea;                 /* pgea structure             */
  273.     PFEA2 pfea;                 /* pfea structure             */
  274.     HFILE handle;               /* file handle                */
  275.     ULONG act;                  /* open action                */
  276.     char  *content;
  277.  
  278.     content = 0;
  279.  
  280.     rc = DosOpen((PCH)FileName, &handle, &act,
  281.                  0L, 0, OPEN_ACTION_OPEN_IF_EXISTS,
  282.                  OPEN_ACCESS_READONLY |
  283.                  OPEN_SHARE_DENYREADWRITE |
  284.                  OPEN_FLAGS_FAIL_ON_ERROR |
  285.                  OPEN_FLAGS_WRITE_THROUGH, NULL);
  286.     if(rc)
  287.     {
  288.         return -1;
  289.     }                           /* get the file status info   */
  290.  
  291.     if (DosAllocMem((PPVOID) & fealist, 0x00010000L, PAG_COMMIT | PAG_WRITE))
  292.     {
  293.         return -2;
  294.     }
  295.  
  296.     /* FEA and GEA lists          */
  297.     eaop.fpGEA2List = (PGEA2LIST) geabuff;
  298.     eaop.fpFEA2List = (PFEA2LIST) fealist;
  299.     eaop.oError = 0;
  300.     pgea = &eaop.fpGEA2List->list[0];
  301.     eaop.fpGEA2List->cbList = sizeof(ULONG) + sizeof(GEA2) + strlen(ea_name);
  302.     eaop.fpFEA2List->cbList = (ULONG) 0xffff;
  303.  
  304.     /* fill in the EA name length */
  305.  
  306.     pgea->cbName = (BYTE) strlen(ea_name);
  307.     strcpy(pgea->szName, ea_name);  /* fill in the name           */
  308.     pgea->oNextEntryOffset = 0;     /* fill in the next offset    */
  309.  
  310.     /* read the extended attribute */
  311.  
  312.     rc = DosQueryFileInfo(handle, 3, (PSZ) & eaop, sizeof(EAOP2));
  313.     DosClose(handle);           /* close the file             */
  314.  
  315.     if (eaop.fpFEA2List->cbList <= sizeof(ULONG))
  316.         rc = -5;                /* this is error also         */
  317.  
  318.     if (rc)
  319.     {
  320.         DosFreeMem(fealist);    /* error, get out             */
  321.         return -6;
  322.     }
  323.  
  324.     pfea = &(eaop.fpFEA2List->list[0]);     /* point to the first FEA     */
  325.     content = new char[pfea->cbValue + 1];
  326.  
  327.     content[pfea->cbValue] = 0;
  328.  
  329.     memcpy(content, ((PSZ) pfea->szName + (pfea->cbName + 1)), pfea->cbValue);
  330.     *ea_value = content;
  331.  
  332.     DosFreeMem(fealist);        /* free our buffer            */
  333.     return 0;
  334. }
  335.  
  336. int put_ea(char *FileName, char *ea_name, char *ea_value)
  337. {
  338.     LONG rc;                    /* Ret code                   */
  339.     PVOID fealist;              /* fealist buffer             */
  340.     EAOP2 eaop;                 /* eaop structure             */
  341.     PFEA2 pfea;                 /* pfea structure             */
  342.     HFILE handle;               /* file handle                */
  343.     ULONG act;                  /* open action                */
  344.  
  345.     rc = DosOpen((PCH)FileName, &handle, &act,
  346.                   0L, 0, OPEN_ACTION_OPEN_IF_EXISTS,
  347. //                  OPEN_ACCESS_READWRITE |
  348.                   OPEN_ACCESS_WRITEONLY    |
  349.                   OPEN_SHARE_DENYWRITE     |
  350.                   OPEN_FLAGS_FAIL_ON_ERROR |
  351.                   OPEN_FLAGS_WRITE_THROUGH, NULL);
  352.     if (rc)
  353.     {
  354.         return -1;
  355.     }
  356.  
  357.     if (DosAllocMem((PPVOID) & fealist, 0x00010000L, PAG_COMMIT | PAG_WRITE))
  358.     {
  359.         return -2;
  360.     }
  361.  
  362.     eaop.fpFEA2List = (PFEA2LIST) fealist;  /* Set memory for the FEA     */
  363.     eaop.fpGEA2List = NULL;                 /* GEA is unused              */
  364.     pfea = &eaop.fpFEA2List->list[0];       /* point to first FEA         */
  365.     pfea->fEA = '\0';                       /* set the flags              */
  366.  
  367.     /* Size of FEA name field     */
  368.     pfea->cbName = (BYTE) strlen(ea_name);
  369.  
  370.     /* Size of Value for this one */
  371.     pfea->cbValue = (SHORT) strlen(ea_value);
  372.  
  373.     /* Set the name of this FEA   */
  374.     strcpy(pfea->szName, ea_name);
  375.  
  376.     /* Set the EA value           */
  377.     memcpy(pfea->szName + (pfea->cbName + 1), ea_value, strlen(ea_value));
  378.  
  379.     pfea->oNextEntryOffset = 0; /* no next entry              */
  380.  
  381.     /* Set the total size var     */
  382.     eaop.fpFEA2List->cbList = sizeof(ULONG) + sizeof(FEA2) +
  383.                               pfea->cbName  + pfea->cbValue;
  384.  
  385.     /* set the file info          */
  386.  
  387.     rc = DosSetFileInfo(handle, 2, (PSZ)&eaop, sizeof(EAOP2));
  388.  
  389.     DosClose(handle);           /* Close the File             */
  390.     DosFreeMem(fealist);        /* Free the memory            */
  391.  
  392.     return 0;
  393. }
  394.  
  395. char* parse_pos(char *ptr, int *x, int *y)
  396. {
  397.     if(!ptr || !x || !y)
  398.         return 0;
  399.  
  400.     int cX = 0;
  401.     int cY = 0;
  402.  
  403.     while(*ptr && __isdd(*ptr))
  404.     {
  405.         cX *= 10;
  406.         cX += *ptr - '0';
  407.         ptr++;
  408.     }
  409.  
  410.     if(!*ptr || *ptr != 'x')
  411.         return 0;
  412.  
  413.     ptr++;
  414.  
  415.     while(*ptr && __isdd(*ptr))
  416.     {
  417.         cY *= 10;
  418.         cY += *ptr - '0';
  419.         ptr++;
  420.     }
  421.     *x = cX;
  422.     *y = cY;
  423.  
  424.     return ptr;
  425. }
  426.  
  427. char* mk_pos(char *ptr, int x, int y)
  428. {
  429.     if(!ptr)
  430.         return 0;
  431.  
  432.     strcpy(ptr, cvt_num(x,0));
  433.     strcat(ptr,"x");
  434.     strcat(ptr, cvt_num(y,0));
  435.  
  436.     return ptr + strlen(ptr);
  437. }
  438.  
  439. void get_cur_dir(char *ptr)
  440. {
  441.     DosQueryPathInfo((PCH)".", FIL_QUERYFULLNAME, ptr, FED_MAXPATH);
  442. }
  443.  
  444. char *get_full_name(char *fname)
  445. {
  446.     char *name = new char[FED_MAXPATH];
  447.  
  448.     name[0] = 0;
  449.  
  450.     DosQueryPathInfo((PCH)fname, FIL_QUERYFULLNAME, name, FED_MAXPATH);
  451.  
  452.     return name;
  453. }
  454.  
  455. //-----------------------------------------------
  456. // Buffered writer
  457. //-----------------------------------------------
  458.  
  459. BlockWrite::BlockWrite(int size)
  460. {
  461.  
  462.     buff = new char[size];
  463.     handle = used_len = 0;
  464.     len = size;
  465. }
  466.  
  467. BlockWrite::~BlockWrite()
  468. {
  469.     if(handle)
  470.         flush();
  471.  
  472.     delete buff;
  473. }
  474.  
  475. void BlockWrite::Add(char *data, int size)
  476. {
  477.     if(!handle || !size)
  478.         return;
  479.  
  480.     if((size+used_len) > len)
  481.         flush();
  482.  
  483.     if(size > len) //buffer still too small to fit pice
  484.     {
  485.         _lwrite(handle, data, size);
  486.         return;
  487.     }
  488.  
  489.     memcpy(&buff[used_len], data, size);
  490.     used_len += size;
  491. }
  492.  
  493. void BlockWrite::flush()
  494. {
  495.     if(!handle || !used_len)
  496.         return;
  497.  
  498.     _lwrite(handle, buff, used_len);
  499.     used_len = 0;
  500. }
  501.  
  502. void BlockWrite::Open(int hnd)
  503. {
  504.     handle = hnd;
  505. }
  506.  
  507. void BlockWrite::Close()
  508. {
  509.     if(used_len)
  510.         flush();
  511. }
  512.  
  513.