home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 May / Freesoft_1997-05_cd.bin / recenz / PROGRAM / JAVADRAW / iavadraw301_inst.exe / data.z / InputFileImpl.c < prev    next >
C/C++ Source or Header  |  1997-05-20  |  2KB  |  86 lines

  1. /*
  2.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. #include <StubPreamble.h>
  18. #include <javaString.h>
  19.  
  20. #include "InputFile.h"
  21. #include "OutputFile.h"
  22.  
  23. #include <sys/types.h>
  24. #include <sys/param.h>
  25. #include <stdio.h>
  26. #include <fcntl.h>
  27. #include <errno.h>
  28.  
  29. #define        LOCAL_PATH_SEPARATOR        '/'
  30.  
  31. static void
  32. convertPath(char *path)
  33. {
  34.     while (*path != '\0') {
  35.         if ((*path == InputFile_separatorChar) ||
  36.             (*path == OutputFile_separatorChar)) {
  37.             *path = LOCAL_PATH_SEPARATOR;
  38.         }
  39.         path++;
  40.     }
  41.     return;
  42. }
  43.  
  44. long InputFile_open(struct HInputFile *this)
  45. {
  46.     int                fd;
  47.     char        buf[MAXPATHLEN];
  48.  
  49.     javaString2CString(unhand(this)->path, buf, sizeof(buf));
  50.     convertPath(buf);
  51.  
  52.     fd = open(buf, O_RDONLY);
  53.     if (fd < 0)
  54.         return(FALSE);
  55.  
  56.     unhand(this)->fd = fd;
  57.     return(TRUE);
  58. }
  59.  
  60. void InputFile_close(struct HInputFile *this)
  61. {
  62.      close(unhand(this)->fd);
  63.      unhand(this)->fd = -1;
  64.      return;
  65. }
  66.  
  67. long InputFile_read(struct HInputFile *this, 
  68.                     HArrayOfByte *b, 
  69.                     long len)
  70. {
  71.     char *data        = unhand(b)->body;
  72.     int  count        = obj_length(b);
  73.     int  actual;
  74.  
  75.     if (count < len) {
  76.         actual = count;
  77.     }
  78.     else {
  79.         actual = len;
  80.     }
  81.     actual = read(unhand(this)->fd, data, actual);
  82.     if (actual == 0)
  83.         return(-1);
  84.     return(actual);
  85. }
  86.