home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-26  |  6.6 KB  |  250 lines

  1. /*
  2.  * Copyright (c) 1994 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. /*
  18.  * This file includes the native code used to implement methods for
  19.  * the File, InputFile and OutputFile classes. Specifically, classes
  20.  * InputFile and OutputFile have native methods that interact with
  21.  * the underlying system.
  22.  *
  23.  * The prototype for the non-static functions are found in the JAVAH
  24.  * generated header file for the class. Also, the JAVAH generated
  25.  * stubfile takes care of marshalling the function parameters from
  26.  * the JAVA world into the C world.
  27.  *
  28.  * First off, we need to include the JAVA internal header files
  29.  * for macros and function prototypes required to manipulate JAVA
  30.  * data structures and functions.
  31.  *
  32.  * StubPreamble.h includes the structure and macro definitions needed
  33.  * to convert JAVA data structures into C data structures. For
  34.  * example, macros such as "unhand" are defined in StubPreamble.h.
  35.  *
  36.  * javaString.h defines the JAVA string maniuplation macros and
  37.  * routines needed to convert between JAVA and C strings.
  38.  */
  39. #include <StubPreamble.h>
  40. #include <javaString.h>
  41.  
  42.  
  43. /*
  44.  * These headers are special in that they are generated by JAVAH. These
  45.  * include the C structure definitions for the JAVA classes.
  46.  */
  47. #include "InputFile.h"
  48. #include "OutputFile.h"
  49.  
  50.  
  51. /*
  52.  * These are some standard Unix headers we use to implement our system
  53.  * dependent native methods.
  54.  */
  55. #include <sys/types.h>
  56. #include <sys/param.h>
  57. #include <stdio.h>
  58. #include <fcntl.h>
  59. #include <errno.h>
  60.  
  61.  
  62. /*
  63.  * Here we define constants used by this module. In our case we need
  64.  * to define our local path separator, because it is different from the
  65.  * one used in JAVA. Note that JAVA's version path separator is
  66.  * defined in the JAVAH generated header files.
  67.  */
  68. #define    LOCAL_PATH_SEPARATOR    '/'
  69.  
  70.  
  71. /*
  72.  * static void
  73.  * convertPath(char *)
  74.  *
  75.  * This function provides C string path conversions from those
  76.  * separated by the File separator character to the system defined
  77.  * one.
  78.  */
  79. static void
  80. convertPath(char *str)
  81. {
  82.     while (*str != '\0') {
  83.     if ((*str == InputFile_separatorChar) ||
  84.         (*str == OutputFile_separatorChar)) {
  85.         *str = LOCAL_PATH_SEPARATOR;
  86.     }
  87.     str++;
  88.     }
  89.     return;
  90. }
  91.  
  92.  
  93. /*
  94.  * long
  95.  * InputFile_open(struct HInputFile *)
  96.  *
  97.  * This function implements the native method open in class InputFile
  98.  * This fact is evidenced by the name. The function name is derived by
  99.  * concatenating the package name with the class name with the method name.
  100.  * The semantics for this method are defined in the class file.
  101.  *
  102.  * The parameter to this function can be thought of as a "handle to
  103.  * a InputFile object." The actual makeup of this is the data portion
  104.  * of the class followed by the method table for the class.
  105.  */
  106. long 
  107. InputFile_open(struct HInputFile *this)
  108. {
  109.     int        fd;
  110.     char    buf[MAXPATHLEN];
  111.  
  112.     /*
  113.      * We need to convert JAVA path string into a C string and
  114.      * then change it to a system dependent path string.
  115.      * Note that "unhand" gets the data portion of the handle to
  116.      * the C representation of the JAVA class InputFile.
  117.      */
  118.     javaString2CString(unhand(this)->path, buf, sizeof(buf));
  119.     convertPath(buf);
  120.  
  121.     /*
  122.      * Now we actually call the system dependent routines to implement
  123.      * the method. Note that we could throw an exception via
  124.      * SignalError() instead of returning a boolean.
  125.      */
  126.     fd = open(buf, O_RDONLY);
  127.     if (fd < 0)
  128.         return(FALSE);
  129.  
  130.     /*
  131.      * On success we store the system fd into the class variable for
  132.      * future calls to this library.
  133.      */
  134.     unhand(this)->fd = fd;
  135.     return(TRUE);
  136. }
  137.  
  138.  
  139. /*
  140.  * void
  141.  * InputFile_close(struct HInputFile *)
  142.  *
  143.  * Implements the close method for class InputFile.
  144.  */
  145. void 
  146. InputFile_close(struct HInputFile *this)
  147. {
  148.      close(unhand(this)->fd);
  149.      unhand(this)->fd = -1;
  150.      return;
  151. }
  152.  
  153.  
  154. /*
  155.  * long 
  156.  * InputFile_read(struct HInputFile *, HArrayOfByte *, long )
  157.  *
  158.  * Implements the read method for class InputFile.
  159.  */
  160. long 
  161. InputFile_read(struct HInputFile *this, 
  162.             HArrayOfByte *buffer, 
  163.             long count)
  164. {
  165.     /*
  166.      * We use another macro here called obj_length which returns the
  167.      * length of an JAVA array object. Again, unhand of the Array
  168.      * object gets us the data structure portion of the object.
  169.      */
  170.     char *data    = unhand(buffer)->body;
  171.     int  len    = obj_length(buffer);
  172.     int  actual;
  173.  
  174.     if (len < count) {
  175.     actual = len;
  176.     }
  177.     else {
  178.     actual = count;
  179.     }
  180.     actual = read(unhand(this)->fd, data, actual);
  181.     if (actual == 0)
  182.     return(-1);
  183.     return(actual);
  184. }
  185.  
  186.  
  187. /*
  188.  * long 
  189.  * OutputFile_open(struct HOutputFile *)
  190.  *
  191.  * Implements the open method for class OutputFile.
  192.  * It is virually identical to InputFile_open. Please, refer to the
  193.  * comments for that function for more information.
  194.  */
  195. long 
  196. OutputFile_open(struct HOutputFile *this)
  197. {
  198.     int        fd;
  199.     char    buf[MAXPATHLEN];
  200.  
  201.     javaString2CString(unhand(this)->path, buf, sizeof(buf));
  202.     convertPath(buf);
  203.     fd = open(buf, O_RDWR|O_CREAT|O_TRUNC, 0644);
  204.     if (fd < 0)
  205.         return(FALSE);
  206.     unhand(this)->fd = fd;
  207.     return(TRUE);
  208. }
  209.  
  210.  
  211. /*
  212.  * void 
  213.  * OutputFile_close(struct HOutputFile *)
  214.  *
  215.  * Implements the close method for class OutputFile.
  216.  */
  217. void 
  218. OutputFile_close(struct HOutputFile *this)
  219. {
  220.     close(unhand(this)->fd);
  221.     unhand(this)->fd = -1;
  222.     return;
  223. }
  224.  
  225.  
  226. /*
  227.  * long 
  228.  * OutputFile_write(struct HOutputFile *, HArrayOfByte *, long )
  229.  *
  230.  * Implements the write method for class OutputFile.
  231.  */
  232. long 
  233. OutputFile_write(struct HOutputFile *this, 
  234.               HArrayOfByte *buffer, 
  235.               long count)
  236. {
  237.     char *data    = unhand(buffer)->body;
  238.     int  len    = obj_length(buffer);
  239.     int  actual;
  240.  
  241.     if (len < count) {
  242.     actual = len;
  243.     }
  244.     else {
  245.     actual = count;
  246.     }
  247.     actual = write(unhand(this)->fd, data, actual);
  248.     return(actual);
  249. }
  250.