ibm-midrangerpgleibm-ifs

How do I programmatically determine the CCSID of a file in the IFS?


I have many files in the IFS with a wide variety of CCSIDs (0, 37, 819, 1200, 1252, etc.) and I've been tasked with writing a simple RPGLE [1] program that will allow a user to view the contents of any of these files.

Our system (V6R1) CCSID (DSPSYSVAL QCCSID) is 65535 [2] and our jobs have a default CCSID of 37 which means when I try to display CCSID 819 (ASCII) for example, I see garbage – symbols, random colors, etc.

I am familiar with converting data from one CCSID to another via the QtqIconvOpen API so if I know a file has a CCSID that differs from our default CCSID, I can convert it. I wrote code years ago to do that and it works fine.

My problem is finding out the CCSID of the IFS file programmatically. After Googling the only relevant information I found was here on Stackoverflow from JamesA (How to determine the CCSID used in CPYFRMIMPF command?) who points out that you can execute “ls -S” in a QSHELL to get a listing of files showing their CCSID and filename.

So theoretically I could execute ls -S filename, parse the output to get the CCSID and then use that CCSID to perform a conversion of the data if necessary. That's an ugly hack though and I'd much prefer to use an IBM API if possible.

Does anyone know of such an API or a more elegant hack than parsing a listing from ls -S filename?

[1] I use the C API's open(), read() and close() to do the actual reading of data.

[2] I know 65535 is a poor choice but there's nothing I can do about it.


Solution

  • It looks like the Qp0lGetAttr() "Get Attributes" API will let you specify a set of attributes to retrieve for a particular file, including one called QP0L_ATTR_CCSID.

    Here's an example of this API in use from RPG. The example including the GetAttr call is in a ZIP file linked from that page.