File: w_main.srw
Size: 8465
Date: Wed, 17 Mar 2021 01:38:34 +0100
$PBExportHeader$w_main.srw
forward
global type w_main from window
end type
type mdi_1 from mdiclient within w_main
end type
type cbx_objects from checkbox within w_main
end type
type cbx_datawindow from checkbox within w_main
end type
type dw_report from datawindow within w_main
end type
type dw_libraries from datawindow within w_main
end type
end forward

global type w_main from window
integer width = 2135
integer height = 2432
boolean titlebar = true
string title = "Line Counter"
string menuname = "m_main"
boolean controlmenu = true
windowtype windowtype = mdihelp!
long backcolor = 67108864
string icon = "AppIcon!"
boolean center = true
event m_opentarget ( )
event m_count ( )
event m_print ( )
mdi_1 mdi_1
cbx_objects cbx_objects
cbx_datawindow cbx_datawindow
dw_report dw_report
dw_libraries dw_libraries
end type
global w_main w_main

forward prototypes
public function string wf_replaceall (string as_oldstring, string as_findstr, string as_replace)
public function string wf_fullpath (string as_filepath, string as_filename)
end prototypes

event m_opentarget();// select a target

String ls_pathname, ls_filename, ls_location, ls_data
String ls_liblist, ls_libname, ls_empty[]
Integer li_rc, li_fnum, li_bytes, li_next
Long ll_len, ll_pos1, ll_pos2, ll_newrow

li_rc = GetFileOpenName("Select Target", &
            ls_pathname, ls_filename, &
            "pbt", "PowerBuilder Targets (*.pbt), *.pbt")

If li_rc = 1 Then
   gs_libnames = ls_empty
   dw_libraries.Reset()
   // get directory the target file is in
   ll_len = ( Len(ls_pathname) - Len(ls_filename) ) - 1
   ls_location = Left(ls_pathname, ll_len)
   // open target file
   li_fnum = FileOpen(ls_pathname)
   // read the target file
   li_bytes = FileRead(li_fnum, ls_data)
   DO WHILE li_bytes > 0
      If Lower(Left(ls_data, 9)) = "liblist ~"" Then
         ls_liblist = Mid(ls_data, 10, Len(ls_data) - 11)
         If Right(ls_liblist, 1) <> ";" Then
            ls_liblist = ls_liblist + ";"
         End If
      End If
      If Lower(Left(ls_data, 9)) = "appname ~"" Then
         gs_appname = Mid(ls_data, 10, Len(ls_data) - 11)
      End If
      // read next line
      li_bytes = FileRead(li_fnum, ls_data)
   LOOP
   // close target file
   FileClose(li_fnum)
   // get first library name
   ll_pos1 = Pos(ls_liblist, ";", 1)
   ll_pos2 = Pos(ls_liblist, ";", ll_pos1 + 1)
   If ll_pos1 = 0 Then
      // only library
      ls_libname = wf_fullpath(ls_location, ls_liblist)
      If FileExists(ls_libname) Then
         ll_newrow = dw_libraries.InsertRow(0)
         dw_libraries.SetItem(ll_newrow, "library_name", ls_libname)
         li_next = UpperBound(gs_libnames) + 1
         gs_libnames[li_next] = ls_libname
      Else
         MessageBox(this.title, "Target contains missing file: " + ls_libname)
      End If
   Else
      // first of many
      ls_libname = Mid(ls_liblist, 1, ll_pos1 - 1)
      ls_libname = wf_fullpath(ls_location, ls_libname)
      If FileExists(ls_libname) Then
         ll_newrow = dw_libraries.InsertRow(0)
         dw_libraries.SetItem(ll_newrow, "library_name", ls_libname)
         li_next = UpperBound(gs_libnames) + 1
         gs_libnames[li_next] = ls_libname
      Else
         MessageBox(this.title, "Target contains missing file: " + ls_libname)
      End If
      // get the others
      DO WHILE ll_pos2 > 0
         // get library name
         ll_len = ll_pos2 - ll_pos1
         ls_libname = Mid(ls_liblist, ll_pos1 + 1, ll_len - 1)
         // insert library
         ls_libname = wf_fullpath(ls_location, ls_libname)
         If FileExists(ls_libname) Then
            ll_newrow = dw_libraries.InsertRow(0)
            dw_libraries.SetItem(ll_newrow, "library_name", ls_libname)
            li_next = UpperBound(gs_libnames) + 1
            gs_libnames[li_next] = ls_libname
         Else
            MessageBox(this.title, "Target contains missing file: " + ls_libname)
         End If
         // find next semi-colon
         ll_pos1 = ll_pos2
         ll_pos2 = Pos(ls_liblist, ";", ll_pos1 + 1)
      LOOP
   End If
   If dw_libraries.RowCount() > 0 Then
      dw_libraries.ScrollToRow(1)
   End If
End If

end event

event m_count();// start counting

If UpperBound(gs_libnames) = 0 Then
   Beep(1)
   MessageBox(this.title, "Please select a Target file!", StopSign!)
   Return
End If

SetPointer(HourGlass!)

// load directory
gn_dir.of_load()

// count the lines
gn_ctr.of_count(cbx_datawindow.Checked, cbx_objects.Checked)

// copy results
dw_report.Reset()
gn_ctr.RowsCopy(1, gn_ctr.RowCount(), Primary!, dw_report, 1, Primary!)

// sort the report
dw_report.Sort()
dw_report.GroupCalc()

// set the application name
dw_report.Object.appname.Text = gs_appname

Beep(1)

MessageBox(this.title, "Line Count Complete!")

end event

event m_print();// print the report

dw_report.Print()

end event

public function string wf_replaceall (string as_oldstring, string as_findstr, string as_replace);String ls_newstring
Long ll_findstr, ll_replace, ll_pos

// get length of strings
ll_findstr = Len(as_findstr)
ll_replace = Len(as_replace)

// find first occurrence
ls_newstring = as_oldstring
ll_pos = Pos(ls_newstring, as_findstr)

Do While ll_pos > 0
   // replace old with new
   ls_newstring = Replace(ls_newstring, ll_pos, ll_findstr, as_replace)
   // find next occurrence
   ll_pos = Pos(ls_newstring, as_findstr, (ll_pos + ll_replace))
Loop

Return ls_newstring

end function

public function string wf_fullpath (string as_filepath, string as_filename);// this function takes any ..\\ at the start of as_filename
// and replaces with directory from as_filepath

String ls_filename
Long ll_pos, ll_cnt, ll_max

// check for drive letters
If Mid(as_filename, 2, 3) = ":\\" Then
   ls_filename = wf_replaceall(as_filename, "\\", "\")
Else
   // get first occurrence
   ll_pos = Pos(as_filename, "..\\")
   DO WHILE ll_pos > 0
      // count and remove occurrence
      ll_max = ll_max + 1
      as_filename = Mid(as_filename, 5)
      // get next occurrence
      ll_pos = Pos(as_filename, "..\\")
   LOOP
   // remove directory levels
   FOR ll_cnt = 1 TO ll_max
      ll_pos = LastPos(as_filepath, "\")
      as_filepath = Left(as_filepath, ll_pos - 1)
   NEXT
   ls_filename = wf_replaceall(as_filename, "\\", "\")
   ls_filename = as_filepath + "\" + ls_filename
End If

Return ls_filename

end function

on w_main.create
if this.MenuName = "m_main" then this.MenuID = create m_main
this.mdi_1=create mdi_1
this.cbx_objects=create cbx_objects
this.cbx_datawindow=create cbx_datawindow
this.dw_report=create dw_report
this.dw_libraries=create dw_libraries
this.Control[]={this.mdi_1,&
this.cbx_objects,&
this.cbx_datawindow,&
this.dw_report,&
this.dw_libraries}
end on

on w_main.destroy
if IsValid(MenuID) then destroy(MenuID)
destroy(this.mdi_1)
destroy(this.cbx_objects)
destroy(this.cbx_datawindow)
destroy(this.dw_report)
destroy(this.dw_libraries)
end on

event open;gw_frame = This

end event

type mdi_1 from mdiclient within w_main
long BackColor=268435456
end type

type cbx_objects from checkbox within w_main
integer x = 987
integer y = 1120
integer width = 1029
integer height = 68
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Arial"
long textcolor = 33554432
long backcolor = 67108864
string text = "Include structural code in objects"
end type

type cbx_datawindow from checkbox within w_main
integer x = 37
integer y = 1120
integer width = 882
integer height = 68
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Arial"
long textcolor = 33554432
long backcolor = 67108864
string text = "SQL Only for DataWindow Objects"
boolean checked = true
end type

type dw_report from datawindow within w_main
integer x = 37
integer y = 1248
integer width = 2053
integer height = 964
integer taborder = 30
string title = "none"
string dataobject = "d_linectr_summary"
boolean vscrollbar = true
boolean livescroll = true
borderstyle borderstyle = stylelowered!
end type

type dw_libraries from datawindow within w_main
integer x = 37
integer y = 128
integer width = 2053
integer height = 932
integer taborder = 20
string title = "none"
string dataobject = "d_library_list"
boolean vscrollbar = true
boolean livescroll = true
borderstyle borderstyle = stylelowered!
end type

event rowfocuschanged;this.SelectRow(0, False)
this.SelectRow(currentrow, True)

end event