Allow deleting of unreferenced account groups.
[capital-apms-progress.git] / inc / method / m-base64.i
blob8d5b2538c58a43978b6a44cfd31afe5b1e149f0e
1 &ANALYZE-SUSPEND _VERSION-NUMBER UIB_v9r12
2 &ANALYZE-RESUME
3 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _DEFINITIONS Method-Library
4 /*------------------------------------------------------------------------
5 Library :
6 Purpose :
8 Syntax :
10 Description :
12 Author(s) :
13 Created :
14 Notes :
15 ----------------------------------------------------------------------*/
16 /* This .W file was created with the Progress AppBuilder. */
17 /*----------------------------------------------------------------------*/
19 /* Definitions */
20 DEF STREAM base64stream.
21 DEF STREAM incomingfile.
23 /* _UIB-CODE-BLOCK-END */
24 &ANALYZE-RESUME
27 &ANALYZE-SUSPEND _UIB-PREPROCESSOR-BLOCK
29 /* ******************** Preprocessor Definitions ******************** */
33 /* _UIB-PREPROCESSOR-BLOCK-END */
34 &ANALYZE-RESUME
37 /* ************************ Function Prototypes ********************** */
39 &IF DEFINED(EXCLUDE-base64-encode) = 0 &THEN
41 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD base64-encode Method-Library
42 FUNCTION base64-encode RETURNS CHARACTER
44 input-stream AS MEMPTR,
45 is-a-string AS LOGICAL,
46 to-file AS LOGICAL
47 ) FORWARD.
49 /* _UIB-CODE-BLOCK-END */
50 &ANALYZE-RESUME
52 &ENDIF
54 &IF DEFINED(EXCLUDE-base64-encode-file) = 0 &THEN
56 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD base64-encode-file Method-Library
57 FUNCTION base64-encode-file RETURNS CHARACTER
58 ( file-param AS CHARACTER ) FORWARD.
60 /* _UIB-CODE-BLOCK-END */
61 &ANALYZE-RESUME
63 &ENDIF
65 &IF DEFINED(EXCLUDE-base64-encode-string) = 0 &THEN
67 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD base64-encode-string Method-Library
68 FUNCTION base64-encode-string RETURNS CHARACTER
69 ( input-string AS CHARACTER ) FORWARD.
71 /* _UIB-CODE-BLOCK-END */
72 &ANALYZE-RESUME
74 &ENDIF
77 /* *********************** Procedure Settings ************************ */
79 &ANALYZE-SUSPEND _PROCEDURE-SETTINGS
80 /* Settings for THIS-PROCEDURE
81 Type: Method-Library
82 Allow:
83 Frames: 0
84 Add Fields to: Neither
85 Other Settings: INCLUDE-ONLY
87 &ANALYZE-RESUME _END-PROCEDURE-SETTINGS
89 /* ************************* Create Window ************************** */
91 &ANALYZE-SUSPEND _CREATE-WINDOW
92 /* DESIGN Window definition (used by the UIB)
93 CREATE WINDOW Method-Library ASSIGN
94 HEIGHT = 15
95 WIDTH = 60.
96 /* END WINDOW DEFINITION */
98 &ANALYZE-RESUME
99 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _XFTR "MethodLibraryCues" Method-Library _INLINE
100 /* Actions: adecomm/_so-cue.w ? adecomm/_so-cued.p ? adecomm/_so-cuew.p */
101 /* Method Library,uib,70080
102 Destroy on next read */
103 /* _UIB-CODE-BLOCK-END */
104 &ANALYZE-RESUME
107 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _INCLUDED-LIB Method-Library
108 /* ************************* Included-Libraries *********************** */
110 /* _UIB-CODE-BLOCK-END */
111 &ANALYZE-RESUME
117 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _MAIN-BLOCK Method-Library
120 /* *************************** Main Block *************************** */
122 /* _UIB-CODE-BLOCK-END */
123 &ANALYZE-RESUME
126 /* ************************ Function Implementations ***************** */
128 &IF DEFINED(EXCLUDE-base64-encode) = 0 &THEN
130 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION base64-encode Method-Library
131 FUNCTION base64-encode RETURNS CHARACTER
133 input-stream AS MEMPTR,
134 is-a-string AS LOGICAL,
135 to-file AS LOGICAL
137 /*------------------------------------------------------------------------------
138 Purpose: Encode a mem pointer binary stream into a base64 encoded string
140 Notes: The following description of the base64 encoding process..
142 * Divide the input bytes stream into blocks of 3 bytes.
143 * Divide the 24 bits of a 3-byte block into 4 groups of 6 bits.
144 * Map each group of 6 bits to 1 printable character, based on the 6-bit value.
145 * If the last 3-byte block has only 1 byte of input data, pad 2 bytes of zero
146 (\x0000). After encoding it as a normal block, override the last 2 characters
147 with 2 equal signs (==), so the decoding process knows 2 bytes of zero were
148 padded.
149 * If the last 3-byte block has only 2 bytes of input data, pad 1 byte of zero
150 (\x00). After encoding it as a normal block, override the last 1 character
151 with 1 equal signs (=), so the decoding process knows 1 byte of zero was
152 padded.
153 * Carriage return (\r) and new line (\n) are inserted into the output character
154 stream. They will be ignored by the decoding process.
156 ------------------------------------------------------------------------------*/
157 DEF VAR Codes64 AS CHARACTER NO-UNDO.
158 DEF VAR output-string AS CHARACTER NO-UNDO.
159 DEF VAR v-count AS INTEGER NO-UNDO.
160 DEF VAR v-index AS INTEGER NO-UNDO.
161 DEF VAR grand-count AS INTEGER INIT 0 NO-UNDO.
163 DEF VAR stream-length AS INTEGER NO-UNDO.
165 DEF VAR first-int AS INTEGER NO-UNDO.
166 DEF VAR second-int AS INTEGER NO-UNDO.
167 DEF VAR third-int AS INTEGER NO-UNDO.
168 DEF VAR last-block AS LOGICAL INIT NO NO-UNDO.
170 DEF VAR integer-array AS INTEGER EXTENT 4 NO-UNDO.
172 /* Temp file vars */
173 DEF VAR temp-file-name AS CHARACTER NO-UNDO.
175 /* Encoding string */
176 Codes64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.
178 /* If the output is set to go to a file */
179 IF to-file THEN DO:
180 temp-file-name = SESSION:TEMP-DIRECTORY + 'BASE64' +
181 STRING( TODAY, "99999999" ) +
182 STRING( TIME ) +
183 STRING( RANDOM( 1, 200 ) ) + '.tmp'.
184 OUTPUT STREAM base64stream TO VALUE( temp-file-name ).
185 END.
187 /* If this is a CHAR stream, then ignore the last byte as it will be a NULL */
188 stream-length = GET-SIZE( input-stream ).
189 IF is-a-string THEN
190 stream-length = stream-length - 1.
192 DO v-count = 1 TO stream-length BY 3:
194 IF v-count + 3 >= stream-length THEN
195 last-block = YES.
197 integer-array[1] = 0.
198 integer-array[2] = 0.
199 integer-array[3] = 0.
200 integer-array[4] = 0.
202 /* Fetch the next three chars */
203 first-int = GET-BYTE( input-stream, v-count ).
204 second-int = GET-BYTE( input-stream, v-count + 1 ).
205 third-int = GET-BYTE( input-stream, v-count + 2 ).
207 /* Pack the three 8 bit integers into 4 "6 bit" integers */
208 PUT-BITS( integer-array[1], 1, 6 ) = GET-BITS( first-int, 3, 6 ).
209 PUT-BITS( integer-array[2], 5, 2 ) = GET-BITS( first-int, 1, 2 ).
210 PUT-BITS( integer-array[2], 1, 4 ) = GET-BITS( second-int, 5, 4 ).
211 PUT-BITS( integer-array[3], 3, 4 ) = GET-BITS( second-int, 1, 4 ).
212 PUT-BITS( integer-array[3], 1, 2 ) = GET-BITS( third-int, 7, 2 ).
213 PUT-BITS( integer-array[4], 1, 6 ) = GET-BITS( third-int, 1, 6 ).
215 DO v-index = 1 TO 4:
216 /* Wrap the string every 76 characters */
217 IF grand-count <> 0 AND grand-count MODULO 72 = 0 THEN
218 output-string = output-string + CHR(13) + CHR(10).
220 /* Check if the last two characters exist */
221 IF v-index = 3 OR v-index = 4 THEN DO:
222 IF last-block AND integer-array[v-index] = 0 THEN
223 output-string = output-string + '='.
224 ELSE
225 output-string = output-string + SUBSTRING( Codes64, integer-array[v-index] + 1, 1 ).
226 END.
227 ELSE
228 output-string = output-string + SUBSTRING( Codes64, integer-array[v-index] + 1, 1 ).
230 /* Count the chars for use in the MODULO */
231 grand-count = grand-count + 1.
232 END.
234 /* If this data is going to a file then write the data and clear the string buffer */
235 IF to-file THEN DO:
236 PUT STREAM base64stream UNFORMATTED output-string.
237 output-string = ''.
238 END.
239 END.
241 /* Return the temp-file name or the string */
242 IF to-file THEN DO:
243 PUT STREAM base64stream UNFORMATTED CHR(10).
244 OUTPUT STREAM base64stream CLOSE.
245 RETURN temp-file-name.
246 END.
247 ELSE
248 RETURN output-string.
250 END FUNCTION.
252 /* _UIB-CODE-BLOCK-END */
253 &ANALYZE-RESUME
255 &ENDIF
257 &IF DEFINED(EXCLUDE-base64-encode-file) = 0 &THEN
259 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION base64-encode-file Method-Library
260 FUNCTION base64-encode-file RETURNS CHARACTER
261 ( file-param AS CHARACTER ) :
262 /*------------------------------------------------------------------------------
263 Purpose: Base64 encode a file
264 Notes:
265 ------------------------------------------------------------------------------*/
266 DEF VAR fileptr AS MEMPTR.
267 DEF VAR encoded-file-name AS CHARACTER NO-UNDO.
269 FILE-INFO:FILE-NAME = file-param.
270 IF FILE-INFO:FILE-MOD-DATE = ? THEN DO:
271 RETURN "".
272 END.
274 /* Slurp the file in */
275 SET-SIZE( fileptr ) = FILE-INFO:FILE-SIZE.
276 INPUT STREAM incomingfile FROM VALUE( file-param ) BINARY.
277 IMPORT STREAM incomingfile fileptr.
278 INPUT STREAM incomingfile CLOSE.
281 Encode the file in base64. NO because not a string, YES because into a temp file.
282 The function returns the temp file location of the encoded data
284 encoded-file-name = base64-encode( fileptr, NO, YES ).
286 RETURN encoded-file-name.
288 END FUNCTION.
290 /* _UIB-CODE-BLOCK-END */
291 &ANALYZE-RESUME
293 &ENDIF
295 &IF DEFINED(EXCLUDE-base64-encode-string) = 0 &THEN
297 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION base64-encode-string Method-Library
298 FUNCTION base64-encode-string RETURNS CHARACTER
299 ( input-string AS CHARACTER ) :
300 /*------------------------------------------------------------------------------
301 Purpose: Base64 encode an arbritrary string
302 Notes:
303 ------------------------------------------------------------------------------*/
304 DEF VAR string-pointer AS MEMPTR.
305 SET-SIZE( string-pointer ) = LENGTH( input-string ) + 1.
307 PUT-STRING( string-pointer, 1 ) = input-string.
310 YES because this function should ignore the terminating NULL, and NO
311 because the output should not go to a file.
313 RETURN base64-encode( string-pointer, YES, NO ).
315 END FUNCTION.
317 /* _UIB-CODE-BLOCK-END */
318 &ANALYZE-RESUME
320 &ENDIF