home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games / INFESPGAMES.mdf / os2 / spl / src / ea.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  4.7 KB  |  196 lines

  1. /* ea.cpp:  some EA manipulation routines 
  2.  
  3.     Copyright (C) 1993, 1994 John-Marc Chandonia
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include "ea.hpp"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <malloc.h>
  25.  
  26. // return a number relating to last file write time
  27. long query_last_write(char *filename) {
  28.     HFILE infile;
  29.     ULONG result=0;
  30.     FILESTATUS3 fi;
  31.     long t;
  32.     APIRET rc;
  33.  
  34.     rc=DosOpen(filename,
  35.            &infile,
  36.            &result,
  37.            0,
  38.            0,
  39.            OPEN_ACTION_FAIL_IF_NEW|
  40.            OPEN_ACTION_OPEN_IF_EXISTS,
  41.            OPEN_SHARE_DENYNONE,
  42.            0);
  43.         
  44.     if ((rc) && (result)) return(0);
  45.  
  46.     DosQueryFileInfo(infile,
  47.              FIL_STANDARD,
  48.              &fi,
  49.              sizeof(fi));
  50.  
  51.     DosClose(infile);
  52.  
  53.     t=fi.ftimeLastWrite.twosecs +
  54.     (30 * fi.ftimeLastWrite.minutes) +
  55.     (30 * 60 * fi.ftimeLastWrite.hours) +
  56.     (30 * 60 * 24 * fi.fdateLastWrite.day) +
  57.     (30 * 60 * 24 * 31 * fi.fdateLastWrite.month) +
  58.     (30 * 60 * 24 * 31 * 12 * fi.fdateLastWrite.year);
  59.  
  60.     return(t);
  61. }
  62.  
  63. // get binary ea data from file
  64. int getEA(char *filename, char *eaname, PVOID ea, long *maxeasize) {
  65.     HFILE infile;
  66.     ULONG result=0;
  67.     FILESTATUS4 fi;
  68.     APIRET rc;
  69.     FEA2LIST *flist;
  70.     GEA2LIST *glist;
  71.     EAOP2 eaop2;
  72.     int i;
  73.  
  74.     // get a file handle
  75.     rc=DosOpen(filename,
  76.            &infile,
  77.            &result,
  78.            0,
  79.            0,
  80.            OPEN_ACTION_FAIL_IF_NEW|
  81.            OPEN_ACTION_OPEN_IF_EXISTS,
  82.            OPEN_SHARE_DENYNONE,
  83.            0);
  84.     if (rc) return(1);
  85.  
  86.     // get total size of ea list from fileinfo.
  87.     rc = DosQueryFileInfo(infile,
  88.               FIL_QUERYEASIZE,
  89.               &fi,
  90.               sizeof(fi));
  91.     if (rc) {
  92.     DosClose(infile);
  93.     return(1);
  94.     }
  95.  
  96.     // allocate maximum memory that could be required.
  97.     flist = (FEA2LIST *)malloc(2*fi.cbList);
  98.     glist = (GEA2LIST *)malloc(2*fi.cbList);
  99.  
  100.     glist->cbList = 1;   
  101.     glist->list[0].oNextEntryOffset=0;
  102.     glist->list[0].cbName = strlen(eaname);
  103.     strcpy(glist->list[0].szName,eaname);
  104.  
  105.     flist->cbList = 2*fi.cbList;
  106.  
  107.     eaop2.fpGEA2List = glist;
  108.     eaop2.fpFEA2List = flist;
  109.     eaop2.oError = 0;
  110.     
  111.     // load ea info into buffer
  112.     rc = DosQueryFileInfo(infile,
  113.               FIL_QUERYEASFROMLIST,
  114.               &eaop2,
  115.               sizeof(eaop2));
  116.     if (rc) {
  117.     free(flist);
  118.     free(glist);
  119.     DosClose(infile);
  120.     return(1);
  121.     }
  122.  
  123.     // make sure ea isn't larger than buffer
  124.     *maxeasize = min(*maxeasize,
  125.              flist->list[0].cbValue);
  126.     // and copy it.
  127.     memcpy(ea,
  128.        flist->list[0].szName+flist->list[0].cbName+1,
  129.        *maxeasize);
  130.        
  131.     free(flist);
  132.     free(glist);
  133.     DosClose(infile);
  134.     if (*maxeasize) return(0);
  135.     else return(1);
  136. }
  137.  
  138. // put binary ea data into file
  139. int putEA(char *filename, char *eaname, PVOID ea, long easize) {
  140.     HFILE infile;
  141.     ULONG result=0;
  142.     FILESTATUS4 fi;
  143.     APIRET rc;
  144.     EAOP2 eaop2;
  145.     int i;
  146.     size_t blocks;
  147.     FEA2LIST *flist=NULL;
  148.  
  149.     blocks = sizeof(FEA2LIST) + strlen(eaname) + easize;
  150.     flist = (FEA2LIST *)malloc(blocks);
  151.     flist->cbList = sizeof(FEA2LIST) + strlen(eaname) + easize;
  152.     flist->list[0].oNextEntryOffset = 0;
  153.     flist->list[0].fEA = 0;
  154.     flist->list[0].cbName = strlen(eaname);
  155.     flist->list[0].cbValue = easize;
  156.     memcpy(flist->list[0].szName,
  157.        eaname,
  158.        flist->list[0].cbName);
  159.     memcpy(flist->list[0].szName+flist->list[0].cbName+1,
  160.        ea,
  161.        easize);
  162.  
  163.     eaop2.fpGEA2List = NULL;
  164.     eaop2.fpFEA2List = flist;
  165.     eaop2.oError = 0;
  166.     
  167.     // get the file handle
  168.     rc=DosOpen(filename,
  169.            &infile,
  170.            &result,
  171.            0,
  172.            0,
  173.            OPEN_ACTION_FAIL_IF_NEW|
  174.            OPEN_ACTION_OPEN_IF_EXISTS,
  175.            OPEN_SHARE_DENYNONE|
  176.            OPEN_ACCESS_READWRITE,
  177.            &eaop2);
  178.     if (rc) {
  179.     free(flist);
  180.     return(1);
  181.     }
  182.  
  183.     // set file info using eaop2 structure
  184.     rc = DosSetFileInfo(infile,
  185.             FIL_QUERYEASIZE,
  186.             &eaop2,
  187.             sizeof(eaop2));
  188.     free(flist);
  189.     DosClose(infile);
  190.     if (rc) return(1);
  191.  
  192.     return(0);
  193. }
  194.  
  195.  
  196.