Making Incremental Improvements

The reason why the initial version of our program, though functional, was not acceptable is that the output was not formatted in a way the user may expect or desire. Part of the reason lies in the fact that to start with, we used the SIMPOL intrinsic function .tostr(). Although this function is quite useful, it is a fairly low-level function and does not provide a wide degree of flexibility when formatting the result. For that reason, early in the development cycle of SIMPOL, additional functions were written using SIMPOL itself to provide that sort of functionality.

There is currently a large and ever-growing library of pre-designed functionality supplied with SIMPOL and in most cases the fully commented source code of the library is also provided. Pre-compiled libraries are located by default in the lib subdirectory of the place where Superbase Next Generation was installed. Projects are normally located in the Projects directory also located directly below the root directory of the installation. The source code for the various supplied libraries can be found in the Libs directory located directly below the Projects directory.

In order to improve the output of the program, we can use the STR() function found in the STR.sml library file. This function includes the ability to format strings in exactly the same ways as those supported by the previous Superbase product, except currently for a lack of support for scientific notation, which will eventually also be supported. To access the functionality in this library, we first need to add it to the project. Select the Settings item from the Project menu.

Opening the Project Settings window via the menu

Opening the Project Settings window via the menu

This will display the Project Settings window.

The Project Settings window

The Project Settings window

This window is extremely important for creating powerful and successful applications using SIMPOL. The initial tab allows the setting of the source code file preference, assigning of command line parameters when running and debugging in the IDE, and also provides a mthod of selecting the SIMPOL components required by the project. If a component is required but not selected, then it will not be available at runtime nor will the inline help assistence work for the associated types and functions.

The second tab provides a place to define two important areas, on the left is the place that the include directories are added (where the compiler will look for included source code files during compilation) and on the right is the list of pre-compiled SIMPOL modules (*.sml's) that are to be added to the project.

The Project Settings Includes and libraries tab

The Project Settings Includes and libraries tab

Click on the Add button on the right side of the window and from the resulting file selection window, go into the lib directory and select the file STR.sml.

The file selection window for STR.sml

The file selection window for STR.sml

Once the library has been added to the project, we can add a declaration for the type SBLNumSettings, which is necessary in the SIMPOL version of this function because unlike in SBL, there are no pre-defined global entities such as Superbase.NumericFormat. As can be seen from the following picture, the inline help also supports user-defined objects and functions. In this case the new() has been implemented in such a way as to allow default values for the object which the user can override by passing in other values.

The inline help for the SBLNumSettings type

The inline help for the SBLNumSettings type

Continue modifying the code until it looks like the source code in the image that follows. We will replace nearly all instances of the function .tostr() with the function STR(). This will give greater flexibility when formatting our numbers.

The reworked source code using the STR() function

The reworked source code using the STR() function

Now we can rebuild the project by pressing Ctrl-B. Assuming that no typing mistakes were made and that it builds successfully, pressing Ctrl-E should successfully run the program and show the results in the output window, which should look something like the following (obviously the actual date and time will differ).

The output from the modified program

The output from the modified program

This time around the result looks much more reasonable than the earlier version. This solution still leaves some open issues, such as dealing with date formats that use the name or the abbreviation of the month and the am/pm style of time format common in English-speaking countries (and possibly elsewhere). The solution to this is to use more appropriate functions for the formatting of the date and time. As it turns out, just as there is a STR.sml there is also a library called SBLDateLib.sml and another called SBLTimeLib.sml, both of which were written in SIMPOL and for which the source code is provided. These libraries are intended to be directly compatible with the older SBL functionality and they contain functions that are in all capital letters, such as DATESTR(), MONTHSTR(), TIMESTR(), and others. As the development of SIMPOL progresses we will create numerous libraries that reproduce the functionality from SBL as well as producing more modern versions of some functions. For example, one of the functions included is the LTRIM() function. This function is a drop-in replacement for the SBL function of the same name. There is also a function supplied called ltrim(). This function is a bit more sophisticated than the SBL version, in that it not only trims spaces, it also trims tab characters and can be passed a string parameter to optionally trim any character contained within that string so that the user can choose which characters should be trimmable.

Let's make some final improvements to the program. Reopen the Project Settings window via the menu and in the Includes and libraries tab remove the STR.sml and select instead the SBLDateLib.sml and SBLTimeLib.sml from the lib directory. Modify the source code to look that shown in the program listing that follows:

 
function main()
  datetime dt
  SBLlocaledateinfo ldiLocale
  integer iMicrosecondsinaday

  ldiLocale =@ SBLlocaledateinfo.new()
  iMicrosecondsinaday = 60 * 60 * 24 * 1000000

  dt =@ datetime.new()
  dt.setnow()

  string s

  s = DATESTR(date.new(dt/iMicrosecondsinaday), "mmmm dd, yyyy", ldiLocale) + "  " + \
      TIMESTR(time.new(dt mod iMicrosecondsinaday), "hh:mm:ss.s am")
end function s

Again, just as with the STR() function, the date functionality requires some formatting information that was globally present in SBL but which must be provided explicitly in SIMPOL. This time after building and executing the program we can see that we are now able to finely control the formatting of the output.

The output of the new program using date and time functions

The output of the new program using date and time functions