Top
PowerBSORT V7.0 User's Guide
FUJITSU Software

A.4 Examples of using C language program

This section explains the description examples of inputting records from text files and outputting sort result to the output file. In the examples, the library function of C language and the system call have not been tested, therefore the operation of this program is not ensured if an error occurred in these functions.
This use example is attached as SAMPLES\c\sample7.c of the installation folder of the product.

/* Declaration of the header file */
#include "sample.h"
#include "bsrt.h"                /* Declaration of the common definition file */
#include <memory.h>
#include <malloc.h>
#include <stdlib.h>

/* Declaration of the structural body variable used to call DLL */
BSRTFUNC        func;            /* Declaration of the function address storage area */
BSRTPRIM        prim;            /* Declaration of the main information table area */
BSRTREC         rec;             /* Declaration of the record information area */
BSRTFILE        file;            /* Declaration of the file information area */
BSPTR_BSRTKEY   key;             /* Declaration of the key information area */
BSRTOPT         opt;             /* Declaration of the option information area */

int sample()
{
  int      rcd;                    /* Function return value */
  HGLOBAL  hHdlFile;               /* Input file table area handle */
  HGLOBAL  hHdlKey;                /* Key table area handle */
  char     szStr[80];              /* Area for the error details code display */

  /* Setting of the argument information on bsrtopen function ...(Note 1) */

  /* Setting of the processing of BSRTPRIM ...(Note 2) */
  memset(&prim, 0x00, sizeof(BSRTPRIM));     /* Initialization of the BSRTPRIM area */
  prim.function   = BS_SORT;                 /* Sort option */
  prim.optionfunc = 0;                       /* No record processing specification */
  prim.msglevel   = BSMSG_LEVEL0;            /* Processing information is not output */
  prim.cdmode     = BSZD_AU;                 /* Code system of the input data is ASCII */
  prim.chklevel   = BSCHK_LEVEL0;            /* The BSORT function is not checked */
  prim.rec_len    = 15;                      /* The record length is 15 bytes */
  prim.fieldmode  = BS_FLTFLD;               /* Floating field specification */

  /* Setting processing of BSRTFILE */
  memset(&file, 0x00, sizeof(BSRTFILE));     /* Initialization of the BSRTFILE area */

  /* Securing of the input file table area...(Note 3) */
  hHdlFile = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, BS_FILESIZE(1));
  file.inpfile_tbl = (BSPTR_BSFILE)GlobalLock((HGLOBAL)hHdlFile);
  file.inpfile_tbl->entry_no = 1;            /* The number of input files is 1 */
  file.inpfile_tbl->file_addr[0] = (BSPTR_UCHAR)"sortin.txt";
                                             /* The file name is sortin.txt */

  /* Specifying output file name */
  file.outfile_addr = (BSPTR_UCHAR)"sortout.txt";
                                             /* The file name is sortout.txt */

  /* Securing of the key table area...(Note 4) */
  hHdlKey = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, BS_KEYTABSIZE(1));
  key = (BSPTR_BSRTKEY)GlobalLock((HGLOBAL)hHdlKey);
                                             /* Securing of the key table area */
  memset(key, 0x00, BS_KEYTABSIZE(1));       /* Initialization of the BSRTKEY area */

  /* Setting processing of BSRTKEY */
  key->entry_no                  = 1;          /* The number of keys is 1 */
  key->key_entry[0].key_position = 0;          /* Key position is first field */
  key->key_entry[0].key_length   = 4;          /* Key length is 4 bytes */
  key->key_entry[0].key_type     = BSKEY_ASC;  /* Data format is ASCII */
  key->key_entry[0].key_order    = BS_ASCND;   /* Sorting in ascending order */

  /* Setting processing of BSRTREC (initialization) */
  memset(&rec, 0x00, sizeof(BSRTREC));       /* Initialization of the BSRTREC area */

  /* Setting processing of BSRTOPT (initialization) */
  memset(&opt, 0x00, sizeof(BSRTOPT));       /* Initialization of the BSRTOPT area */

  /* Issuing the bsrtopen function ...(Note 5) */
  rcd = bsrtopen(BSRTVL, &func, &prim, &rec, key, &file, &opt);

  /* Return value of bsrtopen function ...(Note 6) */
  if(rcd != 0)                               /* Examples of the error display */
  {
    wsprintf((LPSTR)szStr, " Detailed code %d error occurred by the bsrtopen function. ",rec.errdetail);
    MessageBox(NULL, szStr, " PowerBSORT function call sample ", MB_OK | MB_ICONSTOP);
  }

  if(rcd == -1)
  {
    /* Error occurred by the bsrtopen after the execution environment is constructed */
    bsrtclse(BSRTVL, &func, &prim, &rec);
    GlobalUnlock((HGLOBAL)hHdlKey);
    GlobalFree((HGLOBAL)hHdlKey);
    GlobalUnlock((HGLOBAL)hHdlFile);
    GlobalFree((HGLOBAL)hHdlFile);
    return -1;
  }
  else if(rcd == -2)
  {
    /* Error occurred by the bsrtopen before the execution environment is constructed */
    GlobalUnlock((HGLOBAL)hHdlKey);
    GlobalFree((HGLOBAL)hHdlKey);
    GlobalUnlock((HGLOBAL)hHdlFile);
    GlobalFree((HGLOBAL)hHdlFile);
    return -1;
  }

  /* Issuing the bsrtclse function */
  rcd = bsrtclse(BSRTVL, &func, &prim, &rec);

  /* Return value of bsrtclse function ...(Note 7) */
  if(rcd == -1)
  {
    /* Error occurred by the bsrtclse function */
    GlobalUnlock((HGLOBAL)hHdlKey);
    GlobalFree((HGLOBAL)hHdlKey);
    GlobalUnlock((HGLOBAL)hHdlFile);
    GlobalFree((HGLOBAL)hHdlFile);
    return -2;
  }

  GlobalUnlock((HGLOBAL)hHdlKey);
  GlobalFree((HGLOBAL)hHdlKey);
  GlobalUnlock((HGLOBAL)hHdlFile);
  GlobalFree((HGLOBAL)hHdlFile);
  return 0;                        /* Normal termination */
}

NOTE

  1. Information to set in this use example is as follows. Never fail to initialize these areas before use.

    • BSRTPRIM : Main information

    • BSRTFILE : File information

    • BSRTOPT : Option information (Initialization only)

    • BSRTKEY : Key information

    • BSRTREC : Record information (Initialization only)

    • BSRTFUNC : Others (Initialization only)

  2. Notes for processing the text files are shown below.

    • Specify the maximum record length for the record length (prim.rec_len). The maximum record length for a text file is the maximum line length including line feed code.

    • Specify the field specification (prim.fieldmode) without fail.

    • When BS_FLTFLD (floating field specification) is specified for prim.fieldmode, the field separation character (prim.fldchar_addr) that is effective exclusively for the floating field specification can be specified. When the specification of the field separation character is omitted, the blank and the tab become the field separation characters.

  3. Since the number of files is 1, the parameter value of the BS_FILESIZE macro is 1. To specify two or more files, specify the number. PowerBSORT can specify two or more input files and 1 output file.

  4. Since the number of key fields is 1, the parameter value of the BS_KEYTABSIZE macro is 1. To specify two or more files, specify the number.

  5. In this example, PowerBSORT executes the I/O processing of the record because it specifies the I/O file. Therefore, the C language program issues only the bsrtopen function and the bsrtclse function.

  6. The return values of the bsrtopen function include 0, -1, and -2. The meaning of each value is as follows.

    • 0 : The bsrtopen function is a normal termination. The bsrtclse function needs to be issued.

    • -1 : Error occurred by the bsrtopen after the execution environment is constructed. The bsrtclse function needs to be issued.

    • -2 : Error occurred by the bsrtopen before the execution environment is constructed. The bsrtclse function does not need to be issued.

  7. The return values of the bsrtclse function include 0, 1, and -1. The meaning of each value is as follows.

    • 0 : The bsrtclse function is a normal termination.

    • 1 : The bsrtclse function made normal interruption.

    • -1 : Abnormal termination by the bsrtclse function.