'Date Created: 04-Jan-2004 02:49:05 PM 'Last Updated: 14-Jan-2004 11:02:53 AM 'Created By : Cal 'Updated By : Cal 'NOTE - This is NOT the latest version anymore. FUNCTION AIMS_Code_size as C(scp_name="" as C,password="" as C) 'Description:Returns the number of lines, words, and characters for any or all scripts or functions. Works in the code tab of the control panel and in the code editor itself. 'NOTE: This started out as a function that required arguments then morfed into one that basically never needs arguments. DIM p as P DIM cur_scp as C DIM decimals as N DIM first_time as L DIM is_function as L DIM letters as N DIM lines as N DIM mresp as N DIM n2char as C DIM pswd_attempts as N DIM words as N DIM ascp_cnt as N '--------------- Very_beginning: '--------------- First_time = .T. pswd_attempts = 0 is_function = .F. password = "" '--------------- Get_script_text: '--------------- IF scp_name = "" 'THIS IS THE NORMAL STARTING POINT. 'If scp_name is blank AND we are IN the editor, this section will return the code TEXT. 'If scp_name is blank and we are NOT in the editor, the error routine drops us to 'the Choose_script_name() function call which is at the end of this main script. p = a5_get_ptr_to_code_ed(p) ON ERROR goto Get_script_name orig_cursor = p.get_cursor() ON ERROR goto 0 p.select(1,200000) cur_scp = p.get_select() IF cur_scp = "" ui_msg_box("*** ERROR***","Nothing in the editor.",16) DELETE ctype EXIT FUNCTION END IF ELSE IF scp_name = "ALL Scripts" password = "" scp_list = a5.Script_Enum(2) 'NOTE: String is not sorted. scp_cnt = w_count(scp_list) IF scp_cnt = 0 ui_msg_box("NO SCRIPTS TO COUNT","Action Aborted.",16) GOTO Very_beginning END IF words = 0 lines = 0 letters = 0 ascp_cnt = 0 FOR qx = 1 to scp_cnt ascp_cnt = ascp_cnt + 1 'If this was a saved script, find out if a password is needed and get it. scp_name = word(scp_list,qx,crlf()) '--------------- Get_script: '--------------- cur_scp = script_load(scp_name,password) IF left(cur_scp,32) = "'Password provided doesn't match" IF pswd_attempts = 3 ui_msg_box("SIZE CHECK ABORTED","Sorry, you only get 3 tries. Moving to next script.",16) pswd_attempts = 0 GOTO Next_script END IF pswd_attempts = pswd_attempts + 1 password = ui_get_password("SCRIPT " + cur_scp + " PASSWORD PROTECTED","Enter the password or press Cancel.","******","******") IF password = "" pswd_attempts = 0 GOTO Next_script END IF GOTO Get_script END IF 'Count words. words = words + w_count(cur_scp) 'Count lines. lines = lines + line_count(cur_scp) 'Count letters. letters = letters + len(trim(cur_scp)) '--------------- Next_script: '--------------- NEXT mresp = ui_msg_box("SCRIPT SIZE","Scripts Counted: " + N2char(ascp_cnt,0) + crlf(2) + "Lines: " + N2char(lines,0) + crlf(2) + "Words: " + N2char(words,0) + crlf(2) + "Characters: " + N2char(letters,0) + crlf(2) + "SAVE TO CLIPBOARD?",32 + ui_yes_no) IF mresp = ui_yes_selected ClipBoard.Set_Data("Scripts Counted: " + N2char(ascp_cnt,0) + crlf(2) + "Lines: " + N2char(lines,0) + crlf(2) + "Words: " + N2char(words,0) + crlf(2) + "Characters: " + N2char(letters,0)) END IF 'Don't reset lines, words, letters because we might add them to functions to 'get totals. Allow total only with functions so it can't just keep adding up. 'Reset vars and get next selection. scp_name = "" First_time = .T. pswd_attempts = 0 is_function = .F. password = "" GOTO Get_script_name ELSE IF scp_name = "ALL Functions" password = "" scp_list = a5.udf_Enum(2) 'NOTE: String is not sorted. scp_cnt = w_count(scp_list) IF scp_cnt = 0 ui_msg_box("NO FUNCTIONS TO COUNT","Action Aborted.",16) GOTO Very_beginning END IF mtitle = "FUNCTION SIZE" text1 = "Functions Counted: " IF words > 0 mresp = ui_msg_box("ADD TO PREVIOUS?","Add count to previous count?",32 + ui_yes_no) IF mresp = ui_no_selected words = 0 lines = 0 letters = 0 ascp_cnt = 0 ELSE mtitle = "TOTAL SIZE" text1 = "Total Counted: " END IF ELSE ascp_cnt = 0 END IF FOR qx = 1 to scp_cnt ascp_cnt = ascp_cnt + 1 'If this was a saved script, find out if a password is needed and get it. scp_name = word(scp_list,qx,crlf()) '--------------- Get_func: '--------------- cur_scp = udf_load(scp_name,password) IF left(cur_scp,32) = "'Password provided doesn't match" IF pswd_attempts = 3 ui_msg_box("SIZE CHECK ABORTED","Sorry, you only get 3 tries. Moving to next script.",16) pswd_attempts = 0 GOTO Next_func END IF pswd_attempts = pswd_attempts + 1 password = ui_get_password("SCRIPT " + cur_scp + " PASSWORD PROTECTED","Enter the password or press Cancel.","******","******") IF password = "" pswd_attempts = 0 GOTO Next_func END IF GOTO Get_func END IF 'Count words. words = words + w_count(cur_scp) 'Count lines. lines = lines + line_count(cur_scp) 'Count letters. letters = letters + len(trim(cur_scp)) '--------------- Next_func: '--------------- NEXT mresp = ui_msg_box(mtitle,text1 + N2char(ascp_cnt,0) + crlf(2) + "Lines: " + N2char(lines,0) + crlf(2) + "Words: " + N2char(words,0) + crlf(2) + "Characters: " + N2char(letters,0) + crlf(2) + "SAVE TO CLIPBOARD?",32 + ui_yes_no) IF mresp = ui_yes_selected ClipBoard.Set_Data(text1 + N2char(ascp_cnt,0) + crlf(2) + "Lines: " + N2char(lines,0) + crlf(2) + "Words: " + N2char(words,0) + crlf(2) + "Characters: " + N2char(letters,0)) END IF scp_name = "" lines = 0 words = 0 letters = 0 'Reset vars and get next selection. scp_name = "" First_time = .T. pswd_attempts = 0 is_function = .F. password = "" GOTO Get_script_name ELSE 'A SPECIFIC script or function has been selected, get the text. IF is_function cur_scp = udf_load(scp_name,password) is_function = .T. ELSE cur_scp = script_load(scp_name,password) END IF 'Find out if a password is needed, get it, then get the correct text. 'Of course, this won't happen when run from within the code editor. WHILE left(cur_scp,32) = "'Password provided doesn't match" IF pswd_attempts = 3 ui_msg_box("SIZE CHECK ABORTED","Sorry, you only get 3 tries.",16) DELETE ctype EXIT FUNCTION END IF pswd_attempts = pswd_attempts + 1 password = ui_get_password("SCRIPT PASSWORD PROTECTED","Enter the password or press Cancel.","******","******") IF password = "" DELETE ctype EXIT FUNCTION END IF IF is_function cur_scp = udf_load(scp_name,password) ELSE cur_scp = script_load(scp_name,password) END IF END WHILE END IF 'Count words in current code or individual script/function requested. '(ALL Scripts or ALL Functions are handled within the IF statement because of the looping reqd.) words = w_count(cur_scp) 'The first time thru we assume it is a script. If no text is returned, try it as a function. IF words = 0 .and. first_time is_function = .T. ' scp_name = scp_name + "()" first_time = .F. GOTO Get_script_text END IF 'Count lines. lines = line_count(cur_scp) 'Count letters. letters = len(trim(cur_scp)) 'Display results. IF scp_name = "" scp_name = "Current Script" 'For display purposes only. END IF IF is_function scp_name = scp_name + "()" mtitle = "FUNCTION SIZE" ELSE mtitle = "SCRIPT SIZE" END IF msg = "Name: " + scp_name + crlf(2) msg = msg + "Lines: " + N2char(lines,0) + crlf(2) msg = msg + "Words: " + N2char(words,0) + crlf(2) msg = msg + "Characters: " + N2char(letters,0) msg2 = crlf(2) + "SAVE TO CLIPBOARD?" mresp = ui_msg_box(mtitle,msg + msg2,32 + ui_yes_no) IF mresp = ui_yes_selected ClipBoard.Set_Data(msg) END IF IF scp_name = "Current Script" 'Quit. DELETE ctype EXIT FUNCTION ELSE 'NOT in the code editor so allow another selection. lines = 0 words = 0 letters = 0 scp_name = "" is_function = .F. END IF '--------------- Get_script_name: '--------------- scp_name = Choose_script_name() IF "@"$scp_name scp_name = "" ui_msg_box("ERROR","This function only works with code stored in the current .alb file.",16) END IF IF scp_name <> "" 'Keep going until user cancels. (scp_name is blank.) GOTO Very_beginning END IF DELETE ctype END FUNCTION FUNCTION N2char as C(input_val as N,decimals=2 as N) 'Description: Formats numbers as char string with commas for display. Decimal spaces are rounded to 'decimals'. Max numerals is 14. Default decimals is 2. DIM char_val as C DIM input_val as N DIM main_len as N DIM power as N DIM testval as N '---------------------------------------------------------------------------------- ' FORMATS A NUMBER WITH COMMAS - FOR DISPLAY IN MESSAGE BOXES. '---------------------------------------------------------------------------------- char_val = ltrim(str(input_val,20,decimals)) main_len = at(".",char_val) - 1 IF decimals = 0 power = 1 ELSE power = decimals + 2 END IF testval = 10000000000000000 / 10^power IF input_val >= testval - .5 ui_msg_box("TOO LARGE","The number input is too high to be converted to a string value." + crlf(2) + "The total of the main digits plus the decimal plus the decimal digits cannot exceed 15.",16) n2char = "0" EXIT FUNCTION END IF char_val = ltrim(str(input_val,25,decimals)) IF decimals = 0 main_len = len(trim(char_val)) ELSE main_len = at(".",char_val) - 1 END IF SELECT CASE main_len > 12 char_val = stuff(char_val,main_len - 11,0,",") char_val = stuff(char_val,main_len - 7,0,",") char_val = stuff(char_val,main_len - 3,0,",") char_val = stuff(char_val,main_len + 1,0,",") CASE main_len > 9 char_val = stuff(char_val,main_len - 8,0,",") char_val = stuff(char_val,main_len - 4,0,",") char_val = stuff(char_val,main_len - 0,0,",") CASE main_len > 6 char_val = stuff(char_val,main_len - 5,0,",") char_val = stuff(char_val,main_len - 1,0,",") CASE main_len > 3 char_val = stuff(char_val,main_len - 2,0,",") END SELECT n2char = char_val END FUNCTION FUNCTION Choose_script_name as C() DIM GLOBAL ctype as C 'Keep the same selection for each session until the user changes it. DIM ok_pressed as L DIM scp_list as C DIM func_list as C IF ctype = "" ctype = "Script" END IF ok_pressed = .F. scp_list = a5.Script_Enum(2) scp_list = sortsubstr(scp_list,crlf()) func_list = a5.Udf_Enum(2) func_list = sortsubstr(func_list,crlf()) DIM dlg_result as C dlg_result = ui_dlg_box("SELECT SCRIPT/FUNCTION NAME",<<%dlg% {ysize=.3}{sp}; Script or Function? (ctype:Script)(ctype:Function);{sp}; Choose a script/function name or Cancel to quit.;{sp}; {condition=Ctype="Script"} {region} [%v;h;c=40%.120,25scp_name^#scp_list!scp_*];{sp}; {endregion} {condition=ctype="Function"} {start_pos} {region} [%v;h;c=40%.120,25scp_name^#func_list!fnc_*];{sp}; {endregion}; {condition=.T.} {line=1,0}; <*10OK> <10Cancel> Run scripts then functions to get a total count. %dlg%,<<%code% IF left(a_dlg_button,4) = "scp_" IF a_dlg_button <> "scp_dblclick" a_dlg_button = "" ELSE ok_pressed = .T. END IF END IF IF left(a_dlg_button,4) = "fnc_" IF a_dlg_button <> "fnc_dblclick" a_dlg_button = "" ELSE ok_pressed = .T. END IF END IF IF a_dlg_button = "OK" ok_pressed = .T. END IF IF a_dlg_button = "Do 'em ALL" IF ctype = "Script" scp_name = "ALL Scripts" ELSE scp_name = "ALL Functions" END IF ok_pressed = .T. END IF %code%) IF .not. ok_pressed scp_name = "" END IF Choose_script_name = scp_name END FUNCTION