Read text file no allow edits

Moderator: Rathinagiri

User avatar
RussBaker
Posts: 51
Joined: Wed Jul 22, 2015 9:44 am

Re: Read text file no allow edits

Post by RussBaker »

Thank you all for suggestions.
I took Dragan's code and made some changes. I don't need to search the file nor use line numbers.

Added printing function, which allows me to use any Windows or network printer. This is important as line printers are hard to find.
My old Clip code was inserting page breaks into the report, so this allows me to detect them and break the page in HMG.

I also built in some logic to detect page width. My reports come in 80 and 120 column formats, so I want to use the correct print width for selecting fonts size.

It accepts command line arguments so I can do
PREVIEW.EXE Mytextfile.txt

What I found important was selecting a font that is monospaced and exists in most Windows installs.

Again, that's to a fantastic HMG support community.
Attached is his modified code.

--Russ
Attachments
Main.zip
(1.58 KiB) Downloaded 352 times
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Read text file no allow edits

Post by Rathinagiri »

Wonderful!
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
bpd2000
Posts: 1207
Joined: Sat Sep 10, 2011 4:07 am
Location: India

Re: Read text file no allow edits

Post by bpd2000 »

+1
BPD
Convert Dream into Reality through HMG
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Read text file no allow edits

Post by Rathinagiri »

Hi,

I have slightly modified to reduce the width of the grid, center the window and allow to print in lpt1 if it is available. Hope you don't mind altering.

My humble suggestions:

1. dbcloseall is not required as no dbf activity is done here.
2. Virtual grid can be used for large text files.
3. Instead of using len() and substr() functions, we can use hmg_len() and hb_usubstr() functions as they are unicode compatible.

Code: Select all

/*
 * This program is generated by HMGCASE
 * developed by Dragan Cizmarevic < dragancesu(at)gmail.com > 
 */
#include <hmg.ch>

#define CRLF            Chr(13)+Chr(10)
#define LINEBUFF        1024

STATIC aGrid := {}, _find := '', _readmefile := ''


PROCEDURE MAIN 

	

     PUBL Maxline:=0
    _readmefile:="test.txt"
    Procargin() 

      set navigation extended   

   DEFINE WINDOW Readme_Form_1 ;
      AT 10,10 ;
      WIDTH 1024 ;
      HEIGHT 768 ;
      TITLE "Report Preview" ;
      MAIN ; // for test, usually MODAL
      ON INIT Readme_Load_file(_readmefile) ;
      ON RELEASE dbcloseall()

      ON KEY ESCAPE ACTION Readme_Form_1.Release

	  	  
	  @ 15, 50 BUTTON cmd4Print CAPTION " Print " ACTION printtext() WIDTH 90 HEIGHT 25
	  @ 15, 150 BUTTON cmd5Print CAPTION " LPT1 Print " ACTION printtextlpt1( _readmefile ) WIDTH 90 HEIGHT 25
  
  
      @ 50,1 GRID Readme_Grid_1 ;
         WIDTH 1010 ;
         HEIGHT 675 ;
         HEADERS { "","" } ;
         WIDTHS { 1, 980 } ;
         FONT "Lucida Console" ;
         SIZE 9.7 ;
         JUSTIFY { 1,0 } ; 
         NOLINES 
         

   END WINDOW
   Readme_Form_1.center
   ACTIVATE WINDOW Readme_Form_1

RETURN

*:---------------------------------------------*
Function Readme_Load_file(cfile)
Local i, cLine := "", aLine := {}

Private lEof:= .f.
nArquivo:=FOpen(cFile,0)
If nArquivo # -1
   Do While !lEof
      cLine := CharRem( Chr(26), ReadLine(nArquivo,LINEBUFF) )
      Aadd(aGrid, { "" , cline }  )
      if len(alltrim(cline)) > maxline
         maxline= len(alltrim(cline))
      endif         
 
   Enddo
Endif
FClose(nArquivo)

   Readme_Form_1.Readme_Grid_1.DeleteAllItems()
   FOR i = 1 to LEN(aGrid)
        Readme_Form_1.Readme_Grid_1.AddItem(aGrid[i])
   NEXT

Return aLine


*:---------------------------------------------*
Static Function ReadLine(nHandle,nBuffSize)
Local cRet:= cBuff:='', nPos:= nEol:= nRead:=0

cBuff := Space(nBuffSize)
nPos := FSeek(nHandle,0,1)
If ( nRead:=FRead(nHandle,@cBuff,nBuffSize) ) > 0
   If ( nEol:=At(CRLF,SubStr(cBuff,1,nRead)) ) == 0
      cRet:=SubStr(cBuff,1)+Chr(26)
      lEof:=.T.
   Else
      cRet:=SubStr(cBuff,1,nEol-1)
      FSeek(nHandle,nPos+nEol+1,0)
   Endif
Else
   lEof:=.T.
Endif
Return cRet



*------------------------------------------------------------------------------*
Procedure PrintText()
*------------------------------------------------------------------------------*
Local i 
Local lSuccess


	SELECT PRINTER DIALOG TO lSuccess 

	If lSuccess == .T.
		PrintDoc()
		MsgInfo('Print Finished')
	EndIf

Return

*------------------------------------------------------------------------------*
Procedure PrintDoc
*------------------------------------------------------------------------------*
Local i
Local x
local l:=1
local ej:=chr(12)
local f:=7  // font
local h:=4  // height
If maxline > 80
  f=7
  h=4
 //msgbox("small font") 
Else
//msgbox("large font")
  f=12
  h=4.5
Endif

	// Measure Units Are Millimeters

	START PRINTDOC
			START PRINTPAGE
            for i = 1 to len(aGrid)
              x=aGrid[i,2]     
              
              @ l*h, 7 Print x  FONT "Courier New" Size f
              l++
              if l> 60 .or. ej $ x 
                  END PRINTPAGE
                  l=1
                  START PRINTPAGE
              endif
				Next i
			END PRINTPAGE
	END PRINTDOC

Return



// **************************************
func procargin()  // process argument in
// **************************************  
for n=1 to HB_Argc()
  
  argin=upper(HB_Argv(n))
  _readmefile:=argin
  
next n

return 

function printtextlpt1( cFileName )
   if PRINTREADY( 1 ) 
      copy file (cFileName) to lpt1
   else
      msgstop( 'Printer is not ready at LPT1 Port!','Dot Matrix Printer Error' )
   endif   
return nil
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
RussBaker
Posts: 51
Joined: Wed Jul 22, 2015 9:44 am

Re: Read text file no allow edits

Post by RussBaker »

Rathinagiri wrote:Hi,

I have slightly modified to reduce the width of the grid, center the window and allow to print in lpt1 if it is available. Hope you don't mind altering.

My humble suggestions:

1. dbcloseall is not required as no dbf activity is done here.
2. Virtual grid can be used for large text files.
3. Instead of using len() and substr() functions, we can use hmg_len() and hb_usubstr() functions as they are unicode compatible.
Oh please, make suggestions and comments.

1) I also thought the dbcloseall was out of place as no dbf files are used. It probably doesn't do anything other than waste code.

2) Virtual grid? I will need to learn more about grids.

3) I am in my infancy with HMG. I've been a Clip5x programmer for decades, but the HB_ functions are new to me. I will learn more of them going forward.

Dragan might appreciate the LPT support. Most of my installations no longer use LPT printers.
The clients that do, use them for printing barcode labels on pin feed forms. That's on my todo list. Learn how to produce 3 of 9 barcode labels. Although sheet feed laser labels are superior to ribbon printed labels. So much to learn, however I'm really enjoying HMG.
User avatar
dragancesu
Posts: 921
Joined: Mon Jun 24, 2013 11:53 am
DBs Used: DBF, MySQL, Oracle
Location: Subotica, Serbia

Re: Read text file no allow edits

Post by dragancesu »

I'm glad I could help, HMG use for nearly two years and since then has improved a lot, harbour and HMG

Clipper and dot-matrix printer is fine combination
Clipper and laser problematic
HMG and laser ok
but there are users who still have satisfied the Epson dot matrix printers and there is Clipper is slightly ahead, and there are plenty of such programs in the past, it took me only view and print, it works

And I think should be seeking in text and sending the mail, but that's what I have already written
User avatar
RussBaker
Posts: 51
Joined: Wed Jul 22, 2015 9:44 am

Re: Read text file no allow edits

Post by RussBaker »

In retrospect. I think the search function is probably very valuable and may put it back in.

I have clip code that prints to simple laser printers and barcodes. Modern lasers have more dependency on windows drivers and plain text doesn't work.
I do have one client with an Epson dot matrix. They print bar code labels with a 3 of 9 print function. I need to make HMG do this in native windows.

Thanks again Dragan. Very helpful


--rRuss
Post Reply