Sunteți pe pagina 1din 23

H Option( *SrcStmt: *NoDebugIo ) BndDir( 'QC2LE' ) DFTACTGRP(*No)

**********************************************************************
* Project ID

Date Pgmr ID Rev Description

*
*

11/16/11 JJF

00 program written

** pulls .csv tables from directory:

/home/Reserve_Adjustments/

*
**********************************************************************
*
* Directory Entry Structure (dirent)
*
d p_dirent

d dirent

ds

*
based(p_dirent)

d_reserv1

16A

d_reserv2

10U 0

d_fileno

10U 0

d_reclen

10U 0

d_reserv3

10I 0

d_reserv4

8A

d_nlsinfo

12A

nls_ccsid

nls_cntry

2A

OVERLAY(d_nlsinfo:5)

nls_lang

3A

OVERLAY(d_nlsinfo:7)

nls_reserv

3A

OVERLAY(d_nlsinfo:10)

d_namelen

d_name

10I 0 OVERLAY(d_nlsinfo:1)

10U 0
640A

*-----------------------------------------------------------* Open a Directory

*-----------------------------------------------------------d opendir

pr

d dirname

EXTPROC('opendir')

VALUE

*-----------------------------------------------------------* Read Directory Entry


*-----------------------------------------------------------d readdir

pr

d dirname

EXTPROC('readdir')

VALUE

*-----------------------------------------------------------* Close Directory


*-----------------------------------------------------------d closedir

pr

d dirname

EXTPROC('closedir')

VALUE

*-----------------------------------------------------------* Open IFs table


*-----------------------------------------------------------d open

pr

10i 0

ExtProc('open')

filename

value

openflags

10i 0

value

mode

10u 0

value options(*nopass)

codepage

10u 0

value options(*nopass)

*-----------------------------------------------------------* Read IFS table


*-----------------------------------------------------------d read
d

filehandle

datareceived

nbytes

pr

10i 0

ExtProc('read')

10i 0

value

value

10u 0

value

*-----------------------------------------------------------* Close IFs table

*-----------------------------------------------------------d close
d

pr

filehandle

10i 0

ExtProc('close')

10i 0

value

*-----------------------------------------------------------* read ifs table - internal procedure


*-----------------------------------------------------------d RdIfsFil

pr

256a

d FileName

256a

const

*-----------------------------------------------------------* Delay - sleep function


*-----------------------------------------------------------d sleep

pr

10i 0 ExtProc( 'sleep' )

d seconds

10u 0 Value

*-----------------------------------------------------------* Command - run a command


*-----------------------------------------------------------d $command
d

command

length

pr

extpgm('QCMDEXC')
256
15 5

*-----------------------------------------------------------* Grab the date in LongFormat


*-----------------------------------------------------------d CEEDATE

pr

opdesc

Lilian

10i 0

picture

65535A

const options(*varsize)

OutputDate

65535A

const options(*varsize)

Feedback

12a

options(*omit)

*-----------------------------------------------------------* a few local variables...

d BaseDate

inz(D'1582-10-14')

d cmdlength

15 5

d cmdstring

d count

d cr

d data

65535A

d Data_Rec

65535A

d datasize

5 0

d dh

d Eol

d Error_Flag

d File

256

d FileName

256

varying

d FolderNames

256

dim(50)

d Fp

10i 0

d KeepLooping

d lf

d MyNewName

d N

5 0

d nDays

10i 0

256
3 0
Const(x'0D')

Const(x'0D25')
1A

INZ('0')

inz('1')
Const(x'25')

265

varying

* values for oflag parameter, used by open()


d O_RDONLY

10i 0

inz(1)

d O_TEXTDATA

10i 0

inz(16777216)

d Oflag

10i 0

d Omode

10u 0

d PathName

26

d Q

d R

5 0

d Rc

10i 0

inz('''')

d ReturnData

12

d SleepSeconds

10i 0 inz(1)

d ta

3 0

d Today

inz(*SYS)

// entire document stored in here


d MyData

ds

d bighunkdata
d

qualified inz
65535

OneSlice

60

dim(1000) overlay(bighunkdata:*next)

*-----------------------------------------------------* MAIN LINE


*------------------------------------------------------

/free

// program will loop until outside force


// stops it.
dow KeepLooping;

exsr $GetFileName;
if ta > *zeros;
// read the tables one at a time
for count = 1 to ta;
filename = foldernames(count);
Error_flag = rdifsfil(Filename);
//exsr $MoveToHistory;
endfor;
endif;

// Delay job for a number of seconds then start

// the process all over again.


sleep(SleepSeconds);

enddo;

*inlr = *on;

//------------------------------------------// $GetFileName - get the next csv table


//------------------------------------------begsr $GetFileName;

clear filename;
// tables will hold all the names of the tables
clear TA;
clear folderNames;

// loop on the directory


// Step1: Open up the directory.
PathName = '/home/Reserve_Adjustments/';
dh = opendir(%addr(PathName));
if dh <> *NULL;

// Step2: Read each entry from the directory (in a loop)


p_dirent = readdir(dh);

dow p_dirent <> *NULL;


if d_namelen < 256;
FileName = %subst(d_name:1:d_namelen);
// process only csv files

// even MT directory contains folders:


// o .
// o ..
if %scan('.csv':Filename) > *zeros;
ta+=1;
foldernames(ta) = %trim(pathname) + %trim(filename);
endif;
endif;
p_dirent = readdir(dh);
enddo;
endif;

// Step3: Close the directory to reprocess


closedir(dh);

endsr;

//------------------------------------------// $MoveToHistory - move processed table to


//

history

//-------------------------------------------

begsr $MoveToHistory;

// ***** Rename the file *****


// RNM OBJ('/home/Reserve_Adjustments/May Claim Fees.csv')
// NEWOBJ('SomeNewName.csv')
// *like: November16_2011_114547.csv

nDays = %diff(today : baseDate : *days);


ceedate(nDays:'Mmmmmmmmmm':ReturnData:*OMIT);

MyNewName = %trim(ReturnData) +
%char(%subdt(Today:*days)) + '_' +
%char(%subdt(Today:*years)) + '_' +
%ScanRpl('.' : '' :
%char(%time())) + '.csv';

cmdstring = 'RNM OBJ(' + Q + %trim(PathName) +


%trim(filename) + Q + ')' +
' NEWOBJ(' + Q + %trim(MyNewName) +
Q + ')';

cmdlength = %len(%trim(cmdstring));
monitor;
$command(cmdstring:cmdlength);
on-error;
// process for errors
endmon;

// ***** Move file to history *****


// MOV OBJ('/home/Reserve_Adjustments/May Claim Fees.csv')
// TODIR('/home/Reserve_Adjustments/history/')

cmdstring = 'MOV OBJ(' + Q + %trim(PathName) +


%trim(MyNewName) + Q + ')' +
' TODIR(' + Q + %trim(PathName) + 'History/' +
Q + ')';

cmdlength = %len(%trim(cmdstring));
monitor;
$command(cmdstring:cmdlength);
on-error;
// process for errors
endmon;

endsr;

//-------------------------------------------

/end-free

*------------------------------------------------------------*

RdIfsFil - Subprocedure To Read The IFS File

*------------------------------------------------------------p RdIfsFil

d RdIfsFil

PI

Export

d FileName

256A
256A

d CharsRead

d CurChar

d Eof

Const

10i 0
1
const(x'00')

/free
Oflag = O_Rdonly + O_Textdata;
// need whole path here
File = %trim(FileName) + x'00';
Fp = open(%addr(File): Oflag);

if Fp < 0;

Error_Flag = *On;
Return Error_Flag;
Endif;

R = 0;
N = 0;
dou CurChar = Eof;
exsr getChar;
R+=1;
%Subst(Data: R: 1) = CurChar;

if CurChar = X'25';
%Subst(Data: R: 1) = *blanks;
endif;

select;
when R = 256 or CurChar = X'25';

// if you find the code then we still have more data in


// memory and we need to process that remaining data.

if CurChar = X'25';
%Subst(Data: R: 1) = *blanks;
endif;

// one record is here


clear R;
clear Data;

endsl;

enddo;

Return

Error_Flag;

//--------------------------------------------------------// GetChar - Process IFS Record, One Character At A Time


//--------------------------------------------------------begsr GetChar;

// If input buffer is empty, or all characters have been


//

processed, refill the input buffer.

if N = CharsRead;
CharsRead = Read(Fp:%Addr(Data_Rec): 2560);
N = *Zero;
endif;

// Get the next character in the input buffer.


if CharsRead <= 0;
CurChar = Eof;
else;
N+=1;
CurChar = %Subst(Data_Rec: N: 1);
select;
when CurChar = *blanks or CurChar = cr or CurChar = lf;
mydata.bighunkdata = %trim(mydata.bighunkdata) + '|';
other;
mydata.Bighunkdata = %trim(mydata.bighunkdata) +
%trim(Curchar);
endsl;
endif;

endsr;

//--------------------------------------------------------/end-free

p RdIfsFil

H Option( *SrcStmt: *NoDebugIo ) BndDir( 'QC2LE' ) DFTACTGRP(*No)


**********************************************************************
* Project ID

Date Pgmr ID Rev Description

*
*

11/16/11 JJF

00 program written

** pulls .csv tables from directory:

/home/Reserve_Adjustments/

*
**********************************************************************
*
* Directory Entry Structure (dirent)
*
d p_dirent

d dirent

ds

*
based(p_dirent)

d_reserv1

16A

d_reserv2

10U 0

d_fileno

10U 0

d_reclen

10U 0

d_reserv3

10I 0

d_reserv4

8A

d_nlsinfo

12A

nls_ccsid

nls_cntry

10I 0 OVERLAY(d_nlsinfo:1)
2A

OVERLAY(d_nlsinfo:5)

nls_lang

3A

OVERLAY(d_nlsinfo:7)

nls_reserv

3A

OVERLAY(d_nlsinfo:10)

d_namelen

d_name

10U 0
640A

*-----------------------------------------------------------* Open a Directory


*-----------------------------------------------------------d opendir

pr

d dirname

EXTPROC('opendir')

VALUE

*-----------------------------------------------------------* Read Directory Entry


*-----------------------------------------------------------d readdir

pr

d dirname

EXTPROC('readdir')

VALUE

*-----------------------------------------------------------* Close Directory


*-----------------------------------------------------------d closedir

pr

d dirname

EXTPROC('closedir')

VALUE

*-----------------------------------------------------------* Open IFs table


*-----------------------------------------------------------d open

pr

10i 0

ExtProc('open')

filename

value

openflags

10i 0

value

mode

10u 0

value options(*nopass)

codepage

10u 0

value options(*nopass)

*-----------------------------------------------------------* Read IFS table

*-----------------------------------------------------------d read
d

filehandle

datareceived

nbytes

pr

10i 0

ExtProc('read')

10i 0

value

value

10u 0

value

*-----------------------------------------------------------* Close IFs table


*-----------------------------------------------------------d close
d

pr

filehandle

10i 0

ExtProc('close')

10i 0

value

*-----------------------------------------------------------* read ifs table - internal procedure


*-----------------------------------------------------------d RdIfsFil

pr

d FileName

256a
256a

const

*-----------------------------------------------------------* Delay - sleep function


*-----------------------------------------------------------d sleep

pr

d seconds

10i 0 ExtProc( 'sleep' )


10u 0 Value

*-----------------------------------------------------------* Command - run a command


*-----------------------------------------------------------d $command
d

command

length

pr

extpgm('QCMDEXC')
256
15 5

*-----------------------------------------------------------* Grab the date in LongFormat


*-----------------------------------------------------------d CEEDATE

pr

opdesc

Lilian

10i 0

picture

65535A

const options(*varsize)

OutputDate

65535A

const options(*varsize)

Feedback

12a

options(*omit)

*-----------------------------------------------------------* a few local variables...

d BaseDate

inz(D'1582-10-14')

d cmdlength

15 5

d cmdstring

d count

d cr

d data

65535A

d Data_Rec

65535A

d datasize

5 0

d dh

d Eol

d Error_Flag

d File

256

d FileName

256

varying

d FolderNames

256

dim(50)

d Fp

10i 0

d KeepLooping

d lf

d MyNewName

d N

5 0

d nDays

10i 0

256
3 0
Const(x'0D')

Const(x'0D25')
1A

INZ('0')

inz('1')
Const(x'25')

265

varying

* values for oflag parameter, used by open()


d O_RDONLY

10i 0

inz(1)

d O_TEXTDATA

10i 0

inz(16777216)

d Oflag

10i 0

d Omode

10u 0

d PathName

26

d Q

d R

5 0

d Rc

10i 0

d ReturnData

12

d SleepSeconds

10i 0 inz(1)

d ta

3 0

d Today

inz('''')

inz(*SYS)

// entire document stored in here


d MyData

ds

d bighunkdata
d

qualified inz
65535

OneSlice

60

dim(1000) overlay(bighunkdata:*next)

*-----------------------------------------------------* MAIN LINE


*------------------------------------------------------

/free

// program will loop until outside force


// stops it.
dow KeepLooping;

exsr $GetFileName;
if ta > *zeros;
// read the tables one at a time
for count = 1 to ta;

filename = foldernames(count);
Error_flag = rdifsfil(Filename);
//exsr $MoveToHistory;
endfor;
endif;

// Delay job for a number of seconds then start


// the process all over again.
sleep(SleepSeconds);

enddo;

*inlr = *on;

//------------------------------------------// $GetFileName - get the next csv table


//------------------------------------------begsr $GetFileName;

clear filename;
// tables will hold all the names of the tables
clear TA;
clear folderNames;

// loop on the directory


// Step1: Open up the directory.
PathName = '/home/Reserve_Adjustments/';
dh = opendir(%addr(PathName));
if dh <> *NULL;

// Step2: Read each entry from the directory (in a loop)


p_dirent = readdir(dh);

dow p_dirent <> *NULL;


if d_namelen < 256;
FileName = %subst(d_name:1:d_namelen);
// process only csv files
// even MT directory contains folders:
// o .
// o ..
if %scan('.csv':Filename) > *zeros;
ta+=1;
foldernames(ta) = %trim(pathname) + %trim(filename);
endif;
endif;
p_dirent = readdir(dh);
enddo;
endif;

// Step3: Close the directory to reprocess


closedir(dh);

endsr;

//------------------------------------------// $MoveToHistory - move processed table to


//

history

//-------------------------------------------

begsr $MoveToHistory;

// ***** Rename the file *****


// RNM OBJ('/home/Reserve_Adjustments/May Claim Fees.csv')
// NEWOBJ('SomeNewName.csv')
// *like: November16_2011_114547.csv

nDays = %diff(today : baseDate : *days);


ceedate(nDays:'Mmmmmmmmmm':ReturnData:*OMIT);

MyNewName = %trim(ReturnData) +
%char(%subdt(Today:*days)) + '_' +
%char(%subdt(Today:*years)) + '_' +
%ScanRpl('.' : '' :
%char(%time())) + '.csv';

cmdstring = 'RNM OBJ(' + Q + %trim(PathName) +


%trim(filename) + Q + ')' +
' NEWOBJ(' + Q + %trim(MyNewName) +
Q + ')';

cmdlength = %len(%trim(cmdstring));
monitor;
$command(cmdstring:cmdlength);
on-error;
// process for errors
endmon;

// ***** Move file to history *****

// MOV OBJ('/home/Reserve_Adjustments/May Claim Fees.csv')


// TODIR('/home/Reserve_Adjustments/history/')

cmdstring = 'MOV OBJ(' + Q + %trim(PathName) +


%trim(MyNewName) + Q + ')' +
' TODIR(' + Q + %trim(PathName) + 'History/' +
Q + ')';
cmdlength = %len(%trim(cmdstring));
monitor;
$command(cmdstring:cmdlength);
on-error;
// process for errors
endmon;

endsr;

//-------------------------------------------

/end-free

*------------------------------------------------------------*

RdIfsFil - Subprocedure To Read The IFS File

*------------------------------------------------------------p RdIfsFil

d RdIfsFil

PI

d FileName

Export
256A
256A

d CharsRead

d CurChar

d Eof

Const

10i 0
1
const(x'00')

/free
Oflag = O_Rdonly + O_Textdata;
// need whole path here
File = %trim(FileName) + x'00';
Fp = open(%addr(File): Oflag);

if Fp < 0;
Error_Flag = *On;
Return Error_Flag;
Endif;

R = 0;
N = 0;
dou CurChar = Eof;
exsr getChar;
R+=1;
%Subst(Data: R: 1) = CurChar;

if CurChar = X'25';
%Subst(Data: R: 1) = *blanks;
endif;

select;
when R = 256 or CurChar = X'25';

// if you find the code then we still have more data in


// memory and we need to process that remaining data.

if CurChar = X'25';
%Subst(Data: R: 1) = *blanks;

endif;

// one record is here


clear R;
clear Data;

endsl;
enddo;

Return

Error_Flag;

//--------------------------------------------------------// GetChar - Process IFS Record, One Character At A Time


//--------------------------------------------------------begsr GetChar;

// If input buffer is empty, or all characters have been


//

processed, refill the input buffer.

if N = CharsRead;
CharsRead = Read(Fp:%Addr(Data_Rec): 2560);
N = *Zero;
endif;

// Get the next character in the input buffer.


if CharsRead <= 0;
CurChar = Eof;
else;
N+=1;
CurChar = %Subst(Data_Rec: N: 1);
select;

when CurChar = *blanks or CurChar = cr or CurChar = lf;


mydata.bighunkdata = %trim(mydata.bighunkdata) + '|';
other;
mydata.Bighunkdata = %trim(mydata.bighunkdata) +
%trim(Curchar);
endsl;
endif;

endsr;

//--------------------------------------------------------/end-free

p RdIfsFil

S-ar putea să vă placă și