The more you use APIs, the more you need copy files, and this article shows you how to manage them.
One of the greatest strengths of the IBM i is its ever-expanding collection of APIs. These APIs provide everything from system function access to database access to programming to utility functions used by other functions. Pretty much anything you can do from a command line or a menu you can do from an API, which allows you to embed those features into your ILE programs. However, in order to access all of those functions, you need to define the interface to each one, and that is best done through the use of copy files. The problem is that copy files can quickly get out of hand; this article presents a framework to keep them under control.
Reducing Maintenance
IBM's catalog of APIs is gigantic. There are literally thousands of APIs out there. Don't believe me? Go to the API finder for 7.2, type a blank into the name in the "Find by Name" section, and hit Enter. You'll find over 3,000 APIs.
To call these APIs from RPG, you'll need at minimum a prototype for the procedure and in many cases at least one (if not several) data structure definitions. Not to mention the fact that many of these APIs require other APIs. So using a single function may require several prototypes and a bunch of data structures. Not terrible, but what usually happens is that once you've used the function in one program, you'll need it somewhere else. After you've created those definitions and had to copy them manually into several programs, you'll get tired of it (especially if you need to make changes). So almost immediately you'll want a way to reduce the maintenance burden, and that's the copy file. For example, I use a lot of simple system APIs, including sleep and system. They don't really belong with anything else, so I include those prototypes in a simple utility copy file. Also, a lot of APIs use IBM's standard error data structure, so it makes sense to include that in that same utility file. I have a file like this in nearly every development environment I use. With minor tweaks, it usually ends up looking something like this:
// Standard data structures
dcl-ds api_ErrorDs template qualified;
BytesPrv int(10);
BytesAvl int(10);
MessageId char(07);
reserved char(01);
MessageDta char(99);
end-ds;
// C Utilities
dcl-pr strerror pointer extproc('strerror');
errnum int(10) value;
end-pr;
dcl-pr sleep int(10) extproc('sleep');
seconds uns(10) value;
end-pr;
dcl-pr system int(10) extproc('system');
command pointer value options(*string);
end-pr;
All I need to do is include that copy file, and now I have sleep and system, as well as the standard error data structure. Note that I used the qualified and template keywords on my included data structures; that means they don't take up compiler space and they also don't conflict with other variable names.
Next, I start grouping related APIs together, creating one copy file for each. And that way, I get something like this:
/copy QCPYLESRC,DEF_IFS
/copy QCPYLESRC,DEF_UTL
The above might be the /COPY statements for a program that monitors the IFS. It uses some IFS routines and also uses the strerror function to check for errors, so it includes two copy files. The name for your copy file members is entirely up to you, but I've found that using DEF_ as a prefix allows me to segregate out everything that's related to the copy file. It gives me only six characters for the rest of the member name, but we're RPG programmers; we can name anything with six characters! Seriously, that limitation isn't nearly as bad as it seems since you would typically need at most only a couple of dozen copy files.
But copy files by themselves can just as easily spiral out of control. Let me give you a simple example. There are many situations where I need a list of objects. One example is a list of output queues. Our IT staff is constantly adding or removing printer devices and output queues, and the easiest way for our users to select the right one is just to list all the objects of type *OUTQ. In the past, I've done that by doing DSPOBJD to a work file, and that's still a viable approach, but an alternative way is to use the object APIs (specifically QUSROBJD). This API in particular is a bit more complex than others because it also requires you to use the list APIs and the user space APIs, but that also makes it a really good one to learn because, once you've done so, all the other APIs begin to really fall into place (you'll find yourself using the user space and list APIs in many other situations).
API Dependence
And here's where it gets messy. Let's say I have a copy file for user spaces. And then I have another one for the list APIs. The list APIs require the use of a user space, so I have to also include the copy file for user spaces. So do I just assume that if someone is including the copy file for list APIs that they're also including the user space API definitions? Or do I make the list API copy file also include the user space copy file? That could work, but what if I need the user space APIs for another reason? Now I may attempt to include the same copy file twice, and that could be a problem. As you can see, this interdependence can get messy. But you can reduce that clutter that by using a copy file manager. This is a top-level copy file that controls all your other copy files.
So let's take a look at what we might have in that copy file. I'll stick with my naming convention of DEF_ for everything. In this case, the controlling copy file is called is called DEF_SYS. It's used to define all your system APIs.
// Cross includes
// These are used when one definition requires another
/IF DEFINED(DEF_IFS)
/DEFINE DEF_UTL
/ENDIF
/IF DEFINED(DEF_OBJ)
/DEFINE DEF_LST
/ENDIF
/IF DEFINED(DEF_LST)
/DEFINE DEF_USR
/ENDIF
These first lines identify cross-functional dependencies. If you use the IFS routines, you need the utility functions. So if DEF_IFS is defined, then you will also define DEF_UTL. You may be a little confused by the concept of /DEFINE, but all that does is tell the compiler that some condition has been set to true. We'll see how that works in a moment.
// Now bring in everything required
// IFS Routines
/IF DEFINED(DEF_IFS)
/COPY QCPYLESRC,DEF_IFS
/ENDIF
// List processing
/IF DEFINED(DEF_LST)
/COPY QCPYLESRC,DEF_LST
/ENDIF
// Object processing
/IF DEFINED(DEF_OBJ)
/COPY QCPYLESRC,DEF_OBJ
/ENDIF
// User space processing
/IF DEFINED(DEF_USR)
/COPY QCPYLESRC,DEF_USR
/ENDIF
// Utilities
/IF DEFINED(DEF_UTL)
/COPY QCPYLESRC,DEF_UTL
/ENDIF
This section of the copy file then includes all of the individual copy files. Each condition that is set includes another copy file. The copy files are included only once, so there is no concern about collisions or multiple inclusions. And the programs themselves need only worry about including the files that they actually use; required files are included automatically.
Using This in RPG Programs
Now that you have DEF_SYS set up, it's very easy to use in your application programs. Here's an example of the only lines of code required to add IFS support to an RPG program:
/define DEF_IFS
/copy QCPYLESRC,DEF_SYS
By adding these two lines to your RPG source, you will automatically include two copy files: DEF_IFS and the required file DEF_UTL. You define DEF_IFS and then incldue DEF_SYS, which does the rest of the work. In the cross include section, DEF_IFS causes DEF_UTL to be set. Then in the main include section, the copy files DEF_IFS and DEF_UTL are both included. I'l show you the two lines of code we need for object list support and leave it as an exercise for you to figure out which files get included:
/define DEF_OBJ
/copy QCPYLESRC,DEF_SYS
Encapsulation
Of course, even if you're managing your copy files properly, having all of these API calls in your application programs can add unnecessary technical complexity to your business logic. Someone who just wants a list of output queues doesn't need to know anything about user spaces; that's programming clutter that can confuse the issue. We can get around that by using encapsulation, in which we take all the complexities of the API calls and hide them within some application-friendly procedures. These procedures are typically placed into service programs and end up acting like your own business logic APIs. So instead of using the object APIs (and consequently the list and user space APIs) to get a list of printers, we might instead create our own simple API that returns a list of printers called, say, GetPrinters (we'll talk another day about naming conventions, but this is good enough for now). The prototype is very simple:
dcl-pr GetPrinters;
aPrinters char(10) dim(C_MAX_PRINTERS);
end-pr;
The call is simply GetPrinters(aPrinters). That's it. Under the covers, GetPrinters calls the system APIs. There's no real need for error handling; GetPrinters can send an escape message if something goes really wrong. As I said above, you'll put this function into a service program, and you'll probably want a copy file for the definition of this function. Note the constant C_MAX_PRINTERS; that allows us to set an arbitrary limit to the number of printers and then simply recompile the affected programs if the number needs to be increased. That value would be in the copy file along with the prototype. All of our earlier discussions about copy file management still apply, but for the application programmer, the APIs are much less technical and much more application-oriented. Instead, our application program looks like this:
/define DEF_PRINTERS
/copy QCPYLESRC,DEF_APPL
dcl-s aPrinters char(10) dim(C_MAXPRINTERS);
(...)
GetPrinters(aPrinters);
Now all the complexity of the system APIs is hidden behind a simple intuitive call. So the code for GetPrinter uses the system APIs, and the application programs use our own application APIs. Both can be implemented via our copy file approach, and in the end your programming becomes much more manageable.
LATEST COMMENTS
MC Press Online