[SWEXA.WRK]SWEXA_ADD_NUMBER_OF_PAGES.C

/*
**++
**
**  Program:
**      SWEXA_ADD_NUMBER_OF_PAGES
**
**  Purpose:
**      Add the specified number of pages to each page of a report.
**
**  Copyright:
**      Copyright © 1999 Corpita Pty Ltd
**      15 Bedford Street, Collingwood 3066, Australia
**
**  History:
**      20-Mar-1999 by Simon L. Jackson
**          Initial version
**
**--
*/

#pragma module swexa_add_number_of_pages

#include <rms.h>

#include "swrk_sft_dir:swrk_base.h"
#include "swrk_sft_dir:swrk_messages.h"

#include "swexa_sft_dir:swexa_messages.h"

SWRK_MODULE;

#define FAB_M_OPS ( FAB$M_GET | FAB$M_PUT | FAB$M_UPD )

static unsigned short int previous_rfa[3] = { 1, 0, 0 };

#define lcl_procedure_name "SWEXA_ADD_NUMBER_OF_PAGES"

STATUS swexa_add_number_of_pages (
    unsigned int arg_number_of_pages )
 {
    unsigned short int current_rfa[3];
    struct FAB *lcl_fab;
    char lcl_line_buf[137];
    FLAG lcl_loop;
    struct RAB *lcl_rab;
    char *save_rab_ubf;
    unsigned short int save_rab_usz;
    trace_procedure;

#ifdef __ALPHA
    lcl_rab = dcob$rms_current_rab ();
#endif

/* Save current RAB details for later */
    current_rfa[0] = lcl_rab->rab$w_rfa[0];
    current_rfa[1] = lcl_rab->rab$w_rfa[1];
    current_rfa[2] = lcl_rab->rab$w_rfa[2];
    save_rab_ubf = lcl_rab->rab$l_ubf;
    save_rab_usz = lcl_rab->rab$w_usz;

/* Check for capabilities - reopen the file if necessary */
    lcl_fab = lcl_rab->rab$l_fab;
    if ((( lcl_fab->fab$b_fac & FAB_M_OPS ) != FAB_M_OPS )
     || (( lcl_fab->fab$l_fop & FAB$M_SQO ) != 0 ))
     {
      chk_sts ( sys$disconnect ( lcl_rab ));
      chk_sts ( sys$close ( lcl_fab ));
      lcl_fab->fab$b_fac |= FAB_M_OPS;
      lcl_fab->fab$l_fop &= ~ FAB$M_SQO;
      chk_sts ( sys$open ( lcl_fab ));
      chk_sts ( sys$connect ( lcl_rab ));
     }

/* Restore previous RFA */
    lcl_rab->rab$w_rfa[0] = previous_rfa[0];
    lcl_rab->rab$w_rfa[1] = previous_rfa[1];
    lcl_rab->rab$w_rfa[2] = previous_rfa[2];

/* Set values of our own */
    lcl_rab->rab$b_rac = RAB$C_RFA;
    lcl_rab->rab$l_ubf = lcl_line_buf;
    lcl_rab->rab$w_usz = sizeof(lcl_line_buf) - 1;

/* Loop through the earlier part of the file */
    lcl_loop = TRUE;
    while ( lcl_loop )
     {
      ret_sts = sys$get ( lcl_rab );
      switch ( ret_sts )
       {
        case RMS$_EOF:
          lcl_loop = FALSE;
          break;
        case RMS$_NORMAL:
          if ( lcl_rab->rab$w_rsz >= 11 ) /* Minimum for "Page 9 of 9" */
           {
            char *tmp_ptr = lcl_line_buf;
            char *tmp_end = tmp_ptr + lcl_rab->rab$w_rsz;
            char *tmp_end_1;
            *tmp_end = NULL;

/* Determine the point at which we couldn't have the word "Page" */
            tmp_end_1 = tmp_end - 10;
            while ( tmp_ptr != tmp_end_1 )
             {

/* If we have the word "Page" */
              if (( tmp_ptr[0] == 'P' || tmp_ptr[0] == 'p' )
               && ( tmp_ptr[1] == 'A' || tmp_ptr[1] == 'a' )
               && ( tmp_ptr[2] == 'G' || tmp_ptr[2] == 'g' )
               && ( tmp_ptr[3] == 'E' || tmp_ptr[3] == 'e' )
               && ( tmp_ptr[4] == ' ' || tmp_ptr[4] == '\t' ))
               {
                char *tmp_ptr_2 = tmp_ptr + 5;
                char *tmp_end_2;

/* Skip any remaining white space after "Page" */
                tmp_end_2 = tmp_end - 6;
                while ( tmp_ptr_2 != tmp_end_2
                  && ( *tmp_ptr_2 == ' ' || *tmp_ptr_2 == '\t' ))
                    tmp_ptr_2++;

/* Skip any page number */
                tmp_end_2 = tmp_end - 5;
                while ( tmp_ptr_2 != tmp_end_2
                  && ( *tmp_ptr_2 >= '0' && *tmp_ptr_2 <= '9' ))
                    tmp_ptr_2++;

/* Skip any remaining white space after the page number */
                tmp_end_2 = tmp_end - 4;
                while ( tmp_ptr_2 != tmp_end_2
                  && ( *tmp_ptr_2 == ' ' || *tmp_ptr_2 == '\t' ))
                    tmp_ptr_2++;

/* If we hit the point at which we couldn't have the word "of", break */
                if ( tmp_ptr_2 == tmp_end_2 ) break;

/* See if we have the word "of" */
                if (( tmp_ptr_2[0] == 'O' || tmp_ptr_2[0] == 'o' )
                 && ( tmp_ptr_2[1] == 'F' || tmp_ptr_2[1] == 'f' )
                 && ( tmp_ptr_2[2] == ' ' || tmp_ptr_2[2] == '\t' ))
                 {

/* Skip any remaining white space after "of" */
                  tmp_ptr_2 += 3;
                  tmp_end_2 = tmp_end - 1;
                  while ( tmp_ptr_2 != tmp_end_2
                    && ( *tmp_ptr_2 == ' ' || *tmp_ptr_2 == '\t' ))
                      tmp_ptr_2++;
                  if ( tmp_ptr_2 == tmp_end_2 ) break;
                  if ( *tmp_ptr_2 == '9' )
                   {

/* Find the length of the number of pages */
                    STRING_S ( num_pag_dsc );
                    STRING_S_C ( num_pag_fao, "!#ZL" );
                    num_pag_dsc.str_ptr = tmp_ptr_2;
                    while ( tmp_ptr_2 != tmp_end && *tmp_ptr_2 == '9' )
                      tmp_ptr_2++;
                    num_pag_dsc.str_len = diff32 ( tmp_ptr_2, num_pag_dsc.str_ptr );
                    chk_sts ( lib$sys_fao ( &num_pag_fao, 0, &num_pag_dsc,
                      num_pag_dsc.str_len, arg_number_of_pages ));
                    chk_sts ( sys$update ( lcl_rab ));
                   }
                 }
               }
              tmp_ptr++;
             }
           }
          lcl_rab->rab$b_rac = RAB$C_SEQ;
          break;
        default:
          swrk_log_rms_rab ( log_crash_args, SWRK__READERR, lcl_rab );
          break;
       }
     }

/* Restore previous settings */
    lcl_rab->rab$b_rac = RAB$C_SEQ;
    lcl_rab->rab$l_ubf = save_rab_ubf;
    lcl_rab->rab$w_usz = save_rab_usz;

/* Only go back as far as where we are now next time */
    previous_rfa[0] = lcl_rab->rab$w_rfa[0];
    previous_rfa[1] = lcl_rab->rab$w_rfa[1];
    previous_rfa[2] = lcl_rab->rab$w_rfa[2];

    return ( 1 );
 }