home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / native / implementing / example / OutputFileImpl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-13  |  2.1 KB  |  82 lines

  1. /*
  2.  * Copyright (c) 1995-1997 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 "OutputFile.h"
  21. #include "InputFile.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 OutputFile_open(struct HOutputFile *this)
  45. {
  46.     int                fd;
  47.     char        buf[MAXPATHLEN];
  48.  
  49.     javaString2CString(unhand(this)->path, buf, sizeof(buf));
  50.     convertPath(buf);
  51.     fd = open(buf, O_RDWR|O_CREAT|O_TRUNC, 0644);
  52.     if (fd < 0)
  53.         return(FALSE);
  54.     unhand(this)->fd = fd;
  55.     return(TRUE);
  56. }
  57.  
  58. void OutputFile_close(struct HOutputFile *this)
  59. {
  60.     close(unhand(this)->fd);
  61.     unhand(this)->fd = -1;
  62.     return;
  63. }
  64.  
  65. long OutputFile_write(struct HOutputFile *this, 
  66.                       HArrayOfByte *b, 
  67.                       long len)
  68. {
  69.     char *data        = unhand(b)->body;
  70.     int  count        = obj_length(b);
  71.     int  actual;
  72.  
  73.     if (count < len) {
  74.         actual = count;
  75.     }
  76.     else {
  77.         actual = len;
  78.     }
  79.     actual = write(unhand(this)->fd, data, actual);
  80.     return(actual);
  81. }
  82.