-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIFSRDR.SQLRPGLE
More file actions
237 lines (183 loc) · 5.88 KB
/
Copy pathIFSRDR.SQLRPGLE
File metadata and controls
237 lines (183 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
**FREE
// ============================================================
// Program : IFSRDR
// Purpose : IFS reader service program exposes procedures
// to open, read lines from, close, and check
// existence of IFS stream files using C runtime
// APIs bound via QC2LE
// Author : Developer
// Date : 2026-04-15
// Changes :
// 2026-04-15 Developer INC#0001 Initial creation
// ============================================================
Ctl-Opt Option(*SrcStmt : *NoDebugIO)
BndDir('QC2LE')
NoMain
ExtBinInt(*Yes)
AlwNull(*UsrCtl)
Debug(*Yes);
Dcl-Pr COpen Pointer ExtProc('fopen');
path Pointer Value Options(*String);
mode Pointer Value Options(*String);
End-Pr;
Dcl-Pr CFgets Pointer ExtProc('fgets');
buffer Pointer Value;
size Int(10) Value;
stream Pointer Value;
End-Pr;
Dcl-Pr CFclose Int(10) ExtProc('fclose');
stream Pointer Value;
End-Pr;
Dcl-Pr CFputs Int(10) ExtProc('fputs');
buffer Pointer Value Options(*String);
stream Pointer Value;
End-Pr;
Dcl-Pr CStat Int(10) ExtProc('stat');
path Pointer Value Options(*String);
statDs Pointer Value;
End-Pr;
Dcl-S fileHandle Pointer Inz(*Null);
//COPY QRPGLESRC,APPPROTS
// ============================================================
// Procedure : OpenIfsFile
// Purpose : Open an IFS stream file for reading
// Parameters: filePath absolute IFS path
// Returns : *On if file opened successfully, *Off otherwise
// ============================================================
Dcl-Proc OpenIfsFile Export;
Dcl-Pi *N Ind;
filePath Varchar(512) Const;
End-Pi;
fileHandle = COpen(%Trim(filePath) : 'r');
If fileHandle = *Null;
Return *Off;
EndIf;
Return *On;
End-Proc;
// ============================================================
// Procedure : ReadIfsLine
// Purpose : Read a single line from a previously opened IFS
// file; wraps fopen+fgets+fclose for single-shot use
// Parameters: filePath absolute IFS path
// lineOut receives the line content (trimmed)
// Returns : *On if a line was read, *Off on EOF or error
// ============================================================
Dcl-Proc ReadIfsLine Export;
Dcl-Pi *N Ind;
filePath Varchar(512) Const;
lineOut Varchar(1024);
End-Pi;
Dcl-S localHandle Pointer Inz(*Null);
Dcl-S rawBuffer Char(1025) Inz(*Blanks);
Dcl-S fetchResult Pointer Inz(*Null);
localHandle = COpen(%Trim(filePath) : 'r');
If localHandle = *Null;
Return *Off;
EndIf;
fetchResult = CFgets(%Addr(rawBuffer) : 1025 : localHandle);
CFclose(localHandle);
If fetchResult = *Null;
Return *Off;
EndIf;
// Strip trailing newline/CR before returning
lineOut = %TrimR(%Str(%Addr(rawBuffer)));
Return *On;
End-Proc;
// ============================================================
// Procedure : ReadAllLines
// Purpose : Read all lines from an IFS file into a pre-opened
// handle; caller must call OpenIfsFile first and
// CloseIfsFile when done
// Parameters: lineOut receives each line in turn
// Returns : *On while lines remain, *Off at EOF or error
// ============================================================
Dcl-Proc ReadAllLines Export;
Dcl-Pi *N Ind;
lineOut Varchar(1024);
End-Pi;
Dcl-S rawBuffer Char(1025) Inz(*Blanks);
Dcl-S fetchResult Pointer Inz(*Null);
If fileHandle = *Null;
Return *Off;
EndIf;
rawBuffer = *Blanks;
fetchResult = CFgets(%Addr(rawBuffer) : 1025 : fileHandle);
If fetchResult = *Null;
Return *Off;
EndIf;
lineOut = %TrimR(%Str(%Addr(rawBuffer)));
Return *On;
End-Proc;
// ============================================================
// Procedure : WriteIfsLine
// Purpose : Append a single text line to an IFS stream file,
// creating the file if it does not exist
// Parameters: filePath absolute IFS path
// lineIn content to write (newline appended)
// Returns : *On if written successfully, *Off on error
// ============================================================
Dcl-Proc WriteIfsLine Export;
Dcl-Pi *N Ind;
filePath Varchar(512) Const;
lineIn Varchar(1024) Const;
End-Pi;
Dcl-S writeHandle Pointer Inz(*Null);
Dcl-S writeBuffer Char(1026);
Dcl-S writeResult Int(10) Inz(0);
writeHandle = COpen(%Trim(filePath) : 'a');
If writeHandle = *Null;
Return *Off;
EndIf;
writeBuffer = %Trim(lineIn) + X'0A'; // append Unix newline
// Use fputs to write the buffer to the file
writeResult = CFputs(%Trim(writeBuffer) : writeHandle);
CFclose(writeHandle);
Return (writeResult >= 0);
End-Proc;
// ============================================================
// Procedure : CloseIfsFile
// Purpose : Close the module-level file handle opened by
// OpenIfsFile
// Parameters: None
// Returns : None
// ============================================================
Dcl-Proc CloseIfsFile Export;
Dcl-Pi *N;
End-Pi;
If fileHandle <> *Null;
CFclose(fileHandle);
fileHandle = *Null;
EndIf;
Return;
End-Proc;
// ============================================================
// Procedure : IfsFileExists
// Purpose : Check whether an IFS path exists using the C
// stat() system call; does not open the file
// Parameters: filePath absolute IFS path to test
// Returns : *On if file exists, *Off if not found or error
// ============================================================
Dcl-Proc IfsFileExists Export;
Dcl-Pi *N Ind;
filePath Varchar(512) Const;
End-Pi;
// stat() requires a 128-byte output buffer (struct stat)
Dcl-Ds StatBuf Len(128);
st_dev Int(10);
st_ino Int(10);
st_mode Int(10);
st_nlink Int(10);
st_uid Int(10);
st_gid Int(10);
st_rdev Int(10);
st_size Int(10);
st_atime Int(10);
st_mtime Int(10);
st_ctime Int(10);
st_blksize Int(10);
st_blocks Int(10);
End-Ds;
Dcl-S rc Int(10) Inz(0);
rc = CStat(%Trim(filePath) : %Addr(StatBuf));
Return (rc = 0);
End-Proc;