/* sortvars.p Sort variable definitions by variable name Chad Brackmann */ /* It will look for all the variable definitions and attempt to sort them by the name of the variable. The sorted variables will be written to a file "so.txt" in your current default directory. The idea is then to insert "so.txt" into your program and delete the old variable definitions. A temporary file called "so.tmp" is overwritten, so don't have one of these in your home directory either. This is a beta version because it doesn't yet handle variable definitions that are broken into two or more lines. UNIX only. Also, it won't distinguish between variables defined in the main procedure and variables defined in triggers or internal procedures. */ DEF VAR add-p AS LOGI NO-UNDO. DEF NEW GLOBAL SHARED VAR f1 AS CHAR FORMAT "x(16)" LABEL "File-name". DEF VAR f2 AS CHAR FORMAT "x(60)" LABEL "Full-path" NO-UNDO. DEF VAR f3 AS CHAR NO-UNDO. DEF VAR ii AS INTE NO-UNDO. DEF VAR jj AS INTE NO-UNDO. DEF VAR kk AS CHAR NO-UNDO. DEF VAR mx AS INTE NO-UNDO. DEF VAR n AS CHAR FORMAT "x(60)" NO-UNDO. DEF VAR t AS CHAR FORMAT "x(16)" EXTENT 60 NO-UNDO. DEF TEMP-TABLE tt FIELD KEY AS CHAR FIELD kk AS CHAR EXTENT 60 INDEX idx1 KEY. REPEAT: INPUT CLOSE. DISP SKIP(1) " Files named 'so.txt' and 'so.tmp' will be overwritten. " WITH FRAME fr1. UPDATE SKIP(1) SPACE(1) f1 HELP "Enter the program name (on your propath)" WITH FRAME fr1 SIDE-LABELS TITLE " Sort Variables " CENTERED ROW 2. f2 = SEARCH(f1). IF f2 = ? THEN NEXT. IF f2 BEGINS "./" THEN DO: INPUT THRU pwd. IMPORT f3. INPUT CLOSE. f2 = f3 + SUBSTRING(f2,2). END. DISP SKIP SPACE(1) f2 WITH FRAME fr1. DEF STREAM istream. INPUT STREAM istream FROM VALUE(f2) NO-ECHO. STATUS DEFAULT "Reading program file...". REPEAT: ASSIGN t = "" n = "". IMPORT STREAM istream UNFORMATTED n. n = TRIM(n). IF NOT n BEGINS "DEF" THEN NEXT. DO ii = 1 TO NUM-ENTRIES(n," "). t[ii] = TRIM(ENTRY(ii,n," ")). END. /* disp t. */ IF t[1] BEGINS "DEF" AND (t[2] BEGINS "VAR" OR t[3] BEGINS "VAR" OR t[4] BEGINS "VAR" OR t[5] BEGINS "VAR" ) THEN DO: DO jj = 1 TO 10: DO ii = 1 TO 60 - 1: IF t[ii] = "" THEN ASSIGN t[ii] = t[ii + 1] t[ii + 1] = "". END. END. kk = "". IF t[2] BEGINS "VAR" THEN kk = t[3]. ELSE IF t[3] BEGINS "VAR" THEN kk = t[4]. ELSE IF t[4] BEGINS "VAR" THEN kk = t[5]. ELSE IF t[5] BEGINS "VAR" THEN kk = t[6]. IF kk = "" THEN NEXT. CREATE tt. ASSIGN tt.key = kk. DO ii = 1 TO 60: IF t[ii] BEGINS "DEF" THEN t[ii] = "DEF". IF t[ii] BEGINS "INT" THEN t[ii] = "INTE". IF t[ii] BEGINS "CHAR" THEN t[ii] = "CHAR". IF t[ii] BEGINS "DEC" THEN t[ii] = "DECI". IF t[ii] BEGINS "LOGI" THEN t[ii] = "LOGI". t[ii] = RIGHT-TRIM(t[ii],"."). ASSIGN t[ii] = CAPS(t[ii]) WHEN KEYWORD-ALL(t[ii]) <> ?. IF LENGTH(t[ii]) > 2 THEN IF t[ii] MATCHES "*~." THEN ASSIGN t[ii] = CAPS(t[ii]) WHEN KEYWORD-ALL(TRIM(SUBSTRING(t[ii],1,LENGTH(t[ii]) - 1))) <> ?. tt.kk[ii] = t[ii]. END. END. END. mx = 1. STATUS DEFAULT "Appending '.' character...". FOR EACH tt: add-p = NO. DO ii = 60 TO 1 BY -1 WHILE add-p = NO: IF tt.kk[ii] = "" THEN NEXT. IF add-p = NO AND tt.kk[ii] <> "" THEN DO: add-p = YES. tt.kk[ii] = tt.kk[ii] + ".". END. END. END. STATUS DEFAULT "Finding maximum length of variable name...". tt-2: FOR EACH tt: DO ii = 1 TO 60: IF KEYWORD-ALL(tt.kk[ii]) = ? THEN DO: mx = MAX(mx,LENGTH(tt.kk[ii])). LEAVE tt-2. END. END. END. mx = mx + 4. STATUS DEFAULT "Creating temp file...". OUTPUT TO so.tmp UNBUFFERED. FOR EACH tt: DO ii = 1 TO 60: IF tt.kk[ii] = "" THEN NEXT. PUT UNFORMATTED tt.kk[ii] " ". IF KEYWORD-ALL(tt.kk[ii]) = ? AND ii > 1 AND tt.kk[ii - 1] <> "FORMAT" AND tt.kk[ii - 1] <> "LABEL" AND tt.kk[ii - 1] <> "EXTENT" AND tt.kk[ii - 1] <> "LIKE" AND NOT tt.kk[ii - 1] BEGINS "INIT" AND tt.kk[ii ] <> "LOGI" AND tt.kk[ii ] <> "INTE" AND NOT tt.kk[ii] BEGINS "NO-UNDO" THEN DO: DO jj = LENGTH(tt.kk[ii]) TO mx - 1: PUT UNFORMATTED " ". END. END. END. PUT SKIP. END. OUTPUT CLOSE. INPUT STREAM istream FROM so.tmp . OUTPUT TO so.txt UNBUFFERED. STATUS DEFAULT "Aligning NO-UNDO keywords...". REPEAT: IMPORT STREAM istream UNFORMATTED n. jj = 0. DO WHILE INDEX(n,"no-undo") < 52: jj = jj + 1. IF jj > 60 THEN LEAVE. n = REPLACE(n,"no-undo"," NO-UNDO"). END. PUT UNFORMATTED n SKIP. END. OUTPUT CLOSE. STATUS DEFAULT "". DISP SKIP(1) " Done! Check file called 'so.txt' in your current directory. " SKIP(1) WITH FRAME lframe CENTERED TITLE " Information " ROW 9. END.