home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / packery / xpk_source / xfh / misc.c < prev    next >
C/C++ Source or Header  |  1996-10-19  |  3KB  |  137 lines

  1. /* misc.c - misc support functions.
  2.    Copyright (C) 1991, 1992, 1993 Kristian Nielsen.
  3.  
  4.    This file is part of XFH, the compressing file system handler.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            */
  19.  
  20. #include "CFS.h"
  21.  
  22. #include <string.h>
  23. #include "dossupport.h"
  24.  
  25. /* Allocate (CSTR) copy of BSTR. Result is modifyable. */
  26. /* (NOTE: 'bstr' is a BPTR, not UBYTE *). */
  27. char *copybstr(BSTR bstr){
  28.    UBYTE buf[256];
  29.    
  30.    if(!bstr) return NULL;
  31.    bstr2c(bstr,buf);
  32.    return copystr(buf);
  33. }
  34.  
  35. /* Allocate copy of C string. */
  36. char *copystr(char *str){
  37.    char *p;
  38.    
  39.   if(!str) return NULL;
  40.   if(!(p=dosalloc(strlen(str)+1))) return NULL;
  41.   strcpy(p,str);
  42.   return p;
  43. }
  44.  
  45. /* Free allocated string. */
  46. void freestr(char *str){
  47.    dosfree(str);
  48. }
  49.  
  50.  
  51. /* "Safe" (bounded-buffer) versions of bstr2cinplace() / cstr2binplace(). */
  52. /* Only does their stuff if string seems a valid BSTR. */
  53. char *safebstr2cinplace(UBYTE *pp, int buflen){
  54.    register int len;
  55.    register UBYTE *p;
  56.    
  57.    len = *pp;
  58.    if(len < buflen){
  59.       for( p=pp; len--; p++ ) p[0] = p[1];
  60.       *p = '\0';
  61.    }
  62.    return (char *)pp;
  63. }
  64.  
  65.  
  66. BSTR safecstr2binplace(char *pp, int buflen){
  67.    register int len;
  68.    register char c,d;
  69.    register char *p;
  70.    
  71.    for(len=0,p=pp; len<buflen && *p; len++,p++);
  72.    if(len<buflen){
  73.       for( c=len,p=pp; len--; p++ ){
  74.          d=*p;
  75.          *p=c;
  76.          c=d;
  77.       }
  78.       *p=c;
  79.    }
  80.    return (BSTR)((ULONG)pp>>2);
  81. }
  82.  
  83.  
  84. /* ank - give the error ERROR_ACTION_NOT_KNOWN. */
  85. LONG ank( glb glob, ... ){
  86.    glob->ioerr = ERROR_ACTION_NOT_KNOWN;
  87.    return 0L;
  88. }
  89.  
  90. /* ank_fail - give the error ERROR_ACTION_NOT_KNOWN, but fail with -1L. */
  91. LONG ank_fail( glb glob, ... ){
  92.    glob->ioerr = ERROR_ACTION_NOT_KNOWN;
  93.    return -1L;
  94. }
  95.  
  96. /* own - give the error ERROR_OBJECT_WRONG_TYPE. */
  97. LONG owt( glb glob, ... ){
  98.    glob->ioerr = ERROR_OBJECT_WRONG_TYPE;
  99.    return 0L;
  100. }
  101.  
  102. /* abs_seek_pos - get absolute seek pos from pos/offset pair. */
  103. LONG abs_seek_pos( LONG currentpos, LONG filelen, LONG pos, LONG offset ){
  104.    
  105.    switch(offset){
  106.       case OFFSET_BEGINNING:
  107.          return pos;
  108.       case OFFSET_CURRENT:
  109.          return currentpos + pos;
  110.       case OFFSET_END:
  111.          return filelen + pos;
  112.       default:
  113.          debug(("Error: abs_seek_pos: Bad offset value for Seek(): %ld\n",offset));
  114.          return -1L;
  115.    }
  116. }
  117.  
  118.  
  119. /* Get size of file from UFS filehandle. Return -1 if error. */
  120. LONG xFileSizeXfh( glb glob, struct FileHandle *xfh ){
  121.    LONG oldpos;
  122.    LONG size;
  123.    
  124.    oldpos = xSeek( glob, xfh, 0, OFFSET_END );
  125.    if( oldpos == -1L ){
  126.       debug(("Error: xFileSizeXfh(): Cannot obtain size.\n"));
  127.       return -1L;
  128.    }
  129.    size = xSeek( glob, xfh, oldpos, OFFSET_BEGINNING );
  130.    if( size == -1L ){
  131.       debug(("Error: xFileSizeXfh(): Cannot obtain size.\n"));
  132.    }
  133.    return size;
  134. }
  135.  
  136. /* End of misc.c */
  137.