On Tue, 9 Mar 2004, Larry wrote:
>
> Now, I really need the files to be created in UTF-8 (ISO-10646-1). But I
> seem to have trouble here. I can create the files with CCSID 1208, but I
> can't seem to write to them. I assumed that I'd need to use iconv() to
> translate the data, but if open() can do the translation from CCSID 37 to
> CCSID 1208 then I'd much rather do that. Is there some trick I'm missing?
> How can I perform 37-819 conversion but not 37-1208? The 1208 CCSID
> specification at IBM does not help - the box for CCSID is empty:
> http://www-1.ibm.com/servers/eserver/iseries/software/globalization/ccsid/01
> 208.htm

Hmmm...  I think maybe you have to explicitly tell it both the file's
CCSID and the job's CCSID...  Though, you can specify 0 for the job's
CCSID.  At any rate, I'm pretty sure that open() calls iconv_open() under
the covers, and write() calls iconv() under the covers.

So, anything you can do with iconv() should also work with open/write.
Here's some sample code that writes a UTF-8 file.  If you're running V5R2,
you can use the single open method with the O_TEXT_CREAT flag as well,
which makes the code a little simpler (but requires some minor chnages to
the IFSIO_H member)

     H DFTACTGRP(*NO) BNDDIR('QC2LE')

      /include IFSIO_H
      /include ERRNO_H

     D CCSID_UTF8      c                   1208
     D CCSID_CURJOB    c                   0
     D M_RDWR          C                   438

     D CRLF            c                   const(x'0d25')
     D fd              s             10I 0
     D data            s           1000A   varying
     D mydir           s           1000A   varying
     D msg             s             50A

      /free

         *inlr = *on;
         mydir = '/tmp';
         unlink(mydir + '/test_text_file.txt');

      /if defined(OPENTWICE)

         // **********************************************************
         //  in pre V5R2 releases we have to pre-create the text
         //  file for translation to work.
         // **********************************************************

         fd = open( mydir + '/test_text_file.txt'
                  : O_CREAT + O_WRONLY + O_CCSID
                  : M_RDWR
                  : CCSID_UTF8                    );
         if (fd = -1);
            msg = 'open(): ' + %str(strerror(errno));
            dsply ' ' ' ' msg;
            return;
         endif;

         callp close(fd);

         fd = open( mydir + '/test_text_file.txt'
                  : O_WRONLY + O_TEXTDATA + O_CCSID
                  : M_RDWR
                  : CCSID_CURJOB                   );

         if (fd = -1);
            msg = 'open(): ' + %str(strerror(errno));
            dsply ' ' ' ' msg;
            return;
         endif;

      /else

         // **********************************************************
         //  starting in V5R2, we can use the O_TEXT_CREAT flag to
         //   both create & translate in one swell foop.
         // **********************************************************

         fd = open( mydir + '/test_text_file.txt'
                  : O_CREAT+O_WRONLY+O_CCSID+O_TEXTDATA+O_TEXT_CREAT
                  : M_RDWR
                  : CCSID_UTF8
                  : CCSID_CURJOB                  );


         if (fd = -1);
            msg = 'open(): ' + %str(strerror(errno));
            dsply ' ' ' ' msg;
            return;
         endif;

      /endif

         // **********************************************************
         // write some interesting data to our text file
         // **********************************************************

         data =
         'Dear Santa,'                                        +CRLF+
         '  Christmas is coming soon and I know that I''ve'   +CRLF+
         'been a very good boy. Therefore, you are bound by'  +CRLF+
         'your nature to provide me with gifts. Here are the' +CRLF+
         'ones I want:'                                       +CRLF+
                                                               CRLF+
         'a) 15 bottles of shampoo. It doesnt matter what'    +CRLF+
         'kind, as long as it comes in green bottle.'         +CRLF+
                                                               CRLF+
         'b) A map of Birmingham, Alabama.'                   +CRLF+
                                                               CRLF+
         'c) A new sofa, this one hurts when you sit on it.'  +CRLF+
                                                               CRLF+
         'd) My own iSeries/400.'                             +CRLF+
                                                               CRLF+
         'Sincerely,'                                         +CRLF+
         '  Harry S. Truman.'                                 +CRLF;

         if (write(fd: %addr(data)+2: %len(data)) < 1);
            msg = 'write(): ' + %str(strerror(errno));
            dsply ' ' ' ' msg;
         endif;

         if (close(fd) < 0);
            msg = 'close(): ' + %str(strerror(errno));
            dsply ' ' ' ' msg;
         endif;

         *inlr = *on;

      /end-free

      /define ERRNO_LOAD_PROCEDURE
      /include ERRNO_H

In my IFSIO_H I've added the following to support O_TEXT_CREAT in V5R2:

      /if defined(*V5R2M0)
     D*                                            Allow text translation
     D*                                            on newly created file.
     D* Note: O_TEXT_CREAT requires all of the following flags to work:
     D*           O_CREAT+O_TEXTDATA+(O_CODEPAGE or O_CCSID)
     D O_TEXT_CREAT    C                   33554432
      /endif

and:

     D*---------------------------------------------------------------
     D* Open a File
     D*
     D* int open(const char *path, int oflag, . . .);
     D*---------------------------------------------------------------
     D open            PR            10I 0 ExtProc('open')
     D  filename                       *   value options(*string)
     D  openflags                    10I 0 value
     D  mode                         10U 0 value options(*nopass)
     D  codepage                     10U 0 value options(*nopass)
     D/if defined(*V5R2M0)
     D  txtcreatid                   10U 0 value options(*nopass)
     D/endif


Aside from those changes, the IFSIO_H should be the same as the one in my
IFS tutorial.

>
> I'm going to work on your iconv() example even if I can get open() to do the
> job I want - a little more knowledge to add to the pot. :-)
>

For the iconv() example, to use UTF-8, you need to define a larger output
buffer, and also a separate variable for the output buffer's length.  The
reason is that UTF-8 can vary between 1-4 bytes per character.

So, to be safe, make the output buffer for iconv be 4 times the size of
the input buffer, and set it's buffer length variable to the larger size.

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2022 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.