This blog is devoted primarily to doing interesting and fun things using the Ada language. I hope that my commentary about the code is sufficient for those unfamiliar with the language, but please let me know, if not. I am not trying to write a book on Ada. Most of the time I will be cherry-picking things of general interest from the on-going R&D work that I conduct in my spare time. Gentle and Constructive comments are always welcome.
I hope you brought your appetite!
……………
Introduction to Comma-Separated Values
A comma-separated values file is a text file in which the values — whatever they are — are separated by commas. Sometimes other characters are used to separate the values, and I wrote this program to allow the specification of one or more separating characters. One may, for instance, desire to also use a space to make viewing the text file more readable. The sample program here allows both a comma and a space to be used as separators.
Since I am new to blogs and Blogger in particular, the format may lack ease-of-readability. I hope to rectify this in the future.
Intro to the Program
But let's get started! Here is the main program Demo_CSVs. The file name is demo_csvs.adb.
![]() |
| procedure Demo_CSVs |
I use the free Gnat compiler and GPS developer environment from the good folk at AdaCore.
Ada requires us to specify which library units and other program units that this unit — procedure in this case — uses. This is what the "with"s are for. Comments begin with a double hyphen. Ada keywords are generally shown smaller and bolded.
The portion of a unit's body before the begin is called the specification of the body. This is separate from the procedure specification as mentioned below. The use just says that the symbols, operations, and subprograms in the nested unit (package Employee_CSV) are to be made directly visible within this procedure.
The declaration of Process_CSV_Record is two-fold — a specification (the first line) and a body (the second line). In this case, I have told the compiler that the actual body of this procedure appears in a separate file.
The package CSVs_Package is an instantiation of a generic package declared in another program unit. This construct will be described elsewhere; however, suffice it to say that a generic is a compile-time, optionally parameterized unit.
Next we have a small procedure to wrap things up and display a nice little good-bye message.
The Business End of Demo_CSVs
The begin following the Good_Bye procedure signals the point at which the main executable portion of the Demo_CSVs procedure starts. I say "main executable," since types and data in the procedure specification (or "spec") can be dynamic in both size and value. Other than a little initialization (Reserve) and tidying up (Good-Bye) the procedure does one thing: It calls another procedure named Process_CSV_File.
There are 3 arguments to Process_CSV_File:
- The first argument is a special file variable, declared in the Ada library package Ada.Text_IO. In this case, a function is called to retrieve the current input file, typically the "standard input," which normally would be the console device but can be changed to be any input text file.
- The second argument is a string containing the characters that are to be used as break characters between the values or fields of the CSV file records.
- The third argument is an access (not a pointer!) to a procedure to process the CSV records. It will be called from within the Process_CSV_File procedure.
Finally, there is an exception handler. Only a single exception is anticipated, and that is one defined within the IO library unit Ada.IO_Exceptions. It is End_Error and occurs when an end-of-file exception is raised. In our case, the only time that this exception should be raised is if end-of-file is detected from the input console device (since the program is designed to be operated manually for demonstration purposes). End-of-file from the data file is detected and handled separately in the Process_CSV_File procedure.
Wrapping Up
Within this blog entry we have examined the main program that will process a comma-separated-values file. This file is one of the smallest in the program, and that was another good reason to start with it. I will post the rest of the program with commentary as time allows.
-- em

No comments:
Post a Comment