It is currently Wed Jun 28, 2017 3:49 pm



Welcome
Welcome to rfobasic

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today. **You are not required to provide truthful information to any registration questions. Be whomever you wish to be.!


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Deleting Unused Functions in GW LIB
Unread postPosted: Thu Dec 15, 2016 1:06 pm 
Offline
User avatar

Joined: Wed Jul 10, 2013 8:11 am
Posts: 364
Hi,

Normally you need only few functions from the GW lib.

This demo reduce the size from 3731 to 729 lines of code.

This program could be used for other libs, too.

Code:
! This file: Minimize_gw_lib_main.bas
! Using GW_demo_small.bas as a small demo
! Using GW.bas as lib
! At the end creating
! GW_demo_small_min.bas as a small modified demo
! GW_min.bas as a smaller lib

mainBasicSource$ = "GW_demo_small.bas"

CONSOLE.TITLE "Minimize_gw_lib_main.bas"
PRINT "MAIN"

mainBasicSource$ = LEFT$(mainBasicSource$, LEN(mainBasicSource$)-4)
REM *** Modify the lib GW.bas to GW_max.bas
REM Create a new file to store the line numbers of the original code, but in conjunction with an expanded GWlib.
TEXT.OPEN W, FN4, "GW_maxOrg.dat"
REM Create a new file to store an expanded GWlib.
TEXT.OPEN W, FN3, "../source/GW_max.bas"
lineCount = 1
REM Create a new file in a view from the GW_max lib to store the used line numbers.
TEXT.WRITELN FN3, "TEXT.OPEN W, GW_FN1, "+ CHR$(34)+ "GW_max.dat" + CHR$(34)
lineCount = lineCount + 1
TEXT.WRITELN FN3, "TEXT.CLOSE GW_FN1"
TEXT.OPEN R, FN2, "../source/GW.bas"
DO
TEXT.READLN FN2, a_line$
IF LEFT$( a_line$,3) = "EOF" | LEFT$( a_line$,1) = "!"
ELSE
  a_line$ = REPLACE$(a_line$,"GW.bas", "GW_max.bas")
  TEXT.WRITELN FN3,a_line$
  lineCount = lineCount + 1
ENDIF
REM Look for the first line of a function
IF is_in ("FN.DEF", a_line$,1)
  sep$ = "[(]"
  SPLIT result_array$[], MID$(a_line$, 8), sep$
  REM Overlook the function GW_LOAD_PG(...
  IF result_array$[1] = "GW_LOAD_PG"
  ELSE
   relLine = lineCount
   lineCount = lineCount + 1
   REM Add in GW.bas in (all-1) functions a command, that stores the used line number in GW_max.dat
   TEXT.WRITELN FN3, "TEXT.OPEN A, GW_FN1, "+ CHR$(34)+ "GW_max.dat" + CHR$(34)
   lineCount = lineCount + 1
   TEXT.WRITELN FN3, "TEXT.WRITELN GW_FN1, " + CHR$(34) + STR$(relLine)+CHR$(34)
   lineCount = lineCount + 1
   TEXT.WRITELN FN3, "TEXT.CLOSE GW_FN1"
   TEXT.WRITELN FN4,  STR$(relLine)
  ENDIF
ENDIF
TEXT.EOF FN2, isEof
UNTIL isEof
TEXT.CLOSE FN2
TEXT.CLOSE FN3
TEXT.CLOSE FN4
! ************************
REM Modify the main Basic Source Code to a version which uses the expanded GW_max lib
REM Please clear all END statements in the main Basic Source Code manually for testing
REM before using this program!
TEXT.OPEN W, FN3, "../source/" + mainBasicSource$ + "_max.bas" %GW_demo_small_max.bas"
TEXT.OPEN R, FN2, "../source/" + mainBasicSource$ + ".bas" %GW_demo_small.bas"
DO
TEXT.READLN FN2, a_line$
IF is_in ("INCLUDE", UPPER$(a_line$))
  REM Change from GW.bas to GW_max.bas
  TEXT.WRITELN FN3,"INCLUDE" + CHR$(34) + "GW_max.bas" + CHR$(34)
ELSE
  IF LEFT$( a_line$,3) = "EOF"
   REM Call the SUB program after testing
   TEXT.WRITELN FN3,"RUN" + CHR$(34) + "Minimize_gw_lib_sub.bas" + CHR$(34)+ ","+ CHR$(34) + mainBasicSource$ + CHR$(34)
  ELSE
   TEXT.WRITELN FN3,a_line$
  ENDIF
ENDIF
TEXT.EOF FN2, isEof
UNTIL isEof
TEXT.CLOSE FN2
TEXT.CLOSE FN3
! ************************
REM Execute the modified app
RUN mainBasicSource$ + "_max.bas" %"GW_demo_small_max.bas"
REM On runtime test all GUI functions to trigger all usable lib functions!
END


After calling the Source Program you should use it, because some functions used only at activity.

Test all touchable functions :!:

Code:
! This file: Minimize_gw_lib_sub.bas

CONSOLE.TITLE "Minimize_gw_lib_sub.bas"
PRINT "SUB"

mainBasicSource$ = ##$ %##$ is a System Value with data from Minimize_gw_lib_main.bas

REM Open the file with stored line numbers of the original code, but in conjunction with an expanded GWlib.
TEXT.OPEN R, FN2, "GW_maxOrg.dat"
DO
TEXT.READLN FN2, a_line$
IF LEFT$( a_line$,3) = "EOF"
ELSE
  REM Put the modified original line numbers as bundle keys in a bundle.
  REM Ignor the 1!
  BUNDLE.PUT bp, a_line$, 1 % Using a bundle like a list with unique items
ENDIF
TEXT.EOF FN2, isEof
UNTIL isEof
TEXT.CLOSE FN2
BUNDLE.KEYS bp, lp
LIST.SIZE lp, allWithoutOneFunctions
! ************************
REM Open the file the stored and used line numbers.
TEXT.OPEN R, FN2, "GW_max.dat"
DO
TEXT.READLN FN2, a_line$
IF LEFT$( a_line$,3) = "EOF"
ELSE
  REM Remove the used line numbers from the bundles key list.
  REM It doesn't matter, if the key does not exists!
  BUNDLE.REMOVE bp, a_line$ % Remove line numbers of used functions
ENDIF
TEXT.EOF FN2, isEof
UNTIL isEof
TEXT.CLOSE FN2
BUNDLE.KEYS bp, lp
LIST.SIZE lp, removedFunctions
! ************************
REM Create a new file to store a minimized GWlib.
TEXT.OPEN W, FN3, "../source/GW_min.bas"
lineCount = 0
REM Open the expanded GWlib.
TEXT.OPEN R, FN2, "../source/GW_max.bas"
switch = 0
DO
TEXT.READLN FN2, a_line$
lineCount = lineCount + 1
REM Ignor EndOfFile, all TEXT. commands and empty lines
IF LEFT$( a_line$,3) = "EOF"|is_in ("TEXT.", a_line$)|LTRIM$(a_line$)=""
ELSE
  IF switch =2
   switch =0
  ENDIF
  IF is_in ("FN.DEF", a_line$) %REM First line of a function block
   BUNDLE.CONTAIN bp, STR$(lineCount), isDef % Is this the first line number of an unused function?
   IF isDef = 1
    switch = 1 %REM If switch = 1 or 2 ignor the line
   ENDIF
  ENDIF
  IF switch =1 &  is_in ("IF !GW_SILENT_LOAD", a_line$) %REM Last line of a function block
   switch=2
  ENDIF
  IF switch =0 %REM IF only switch = 0 take the lines.
   a_line$ = REPLACE$(a_line$,"GW_max.bas", "GW_min.bas")
   TEXT.WRITELN FN3,a_line$
  ENDIF
ENDIF

TEXT.EOF FN2, isEof
UNTIL isEof
TEXT.CLOSE FN2
TEXT.CLOSE FN3
! ************************
REM Modify the main Basic Source Code to a version which uses the minimized GW_min lib
TEXT.OPEN W, FN3, "../source/" + mainBasicSource$ + "_min.bas" %GW_demo_small_min.bas
TEXT.OPEN R, FN2, "../source/" + mainBasicSource$ + ".bas" %GW_demo_small.bas
DO
TEXT.READLN FN2, a_line$
IF is_in ("INCLUDE", UPPER$(a_line$))
  TEXT.WRITELN FN3,"INCLUDE" + CHR$(34) + "GW_min.bas" + CHR$(34)
ELSE
  IF LEFT$( a_line$,3) = "EOF"
  ELSE
   TEXT.WRITELN FN3,a_line$
  ENDIF
ENDIF
TEXT.EOF FN2, isEof
UNTIL isEof
TEXT.CLOSE FN2
TEXT.CLOSE FN3
DIALOG.MESSAGE ,STR$(removedFunctions) + " functions from " + STR$(allWithoutOneFunctions + 1) + " removed!",go, "GO"
RUN mainBasicSource$ + "_min.bas" %GW_demo_small_min.bas"
END


Tested with GW lib 4.4 and 4.6.

I do not know, if it has consequences for the speed, but you have a better overview.

What need the code above in the future? More elegance, deleting temporary files and an UI.
Luca, can you help?

Happy coding
Gregor

EDIT 2016-12-16: Have added some needed code remarks. See the REMs!


Last edited by aFox on Fri Dec 16, 2016 9:17 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Deleting Unused Functions in GW LIB
Unread postPosted: Thu Dec 15, 2016 3:44 pm 
Offline

Joined: Wed Feb 20, 2013 3:25 pm
Posts: 423
Location: Italy
Hi Gregor,

What logic are you using to remove functions?
Some functions are not part of the public API but are called by other functions.

Luca


Top
 Profile  
 
 Post subject: Re: Deleting Unused Functions in GW LIB
Unread postPosted: Fri Dec 16, 2016 9:24 am 
Offline
User avatar

Joined: Wed Jul 10, 2013 8:11 am
Posts: 364
Hi Luca,
luca_italy wrote:
What logic are you using to remove functions?

Have added some needed code remarks. See the REMs!
luca_italy wrote:
Some functions are not part of the public API but are called by other functions.

You have to test all your GUI functions to trigger all needed lib functions.

Gregor


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
suspicion-preferred