• Welcome to PlanetSquires Forums.
 

José, if you're bored...

Started by Paul Squires, August 02, 2018, 09:19:19 PM

Previous topic - Next topic

José Roca

#15
CTextStream can't know in advance if the file is ansi or unicode. However, if the file is unicode with a BOM and you open it as unicode using the OpenUnicode method, it will skip the BOM automatically, i.e. the first line will be returned without the BOM marker.

Maybe we can write a function that checks if the file has an UTF-16 BOM and, if not, call the IsTextUnicode API function, although this function isn't foolproof.


José Roca

What about this function?


' ========================================================================================
' Determines if a file is likely to contain a form of unicode text.
' ========================================================================================
PRIVATE FUNCTION AfxIsFileUnicode (BYREF wszFileName AS WSTRING) AS BOOLEAN
   DIM hFile AS HANDLE = CreateFileW(@wszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, _
                         OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL)
   IF hFile = INVALID_HANDLE_VALUE THEN RETURN FALSE
   DIM dwFileSize AS DWORD, dwHighSize AS DWORD
   dwFileSize = GetFileSize(hFile, @dwHighSize)
   DIM dwBytesToRead AS DWORD = 1024
   IF dwFileSize < dwBytesToRead THEN dwBytesToRead = dwFileSize
   DIM pBuffer AS UBYTE PTR = CAllocate(1, dwBytesToRead)
   DIM bRes AS BOOLEAN, dwBytesRead AS DWORD
   bRes = ReadFile(hFile, pBuffer, dwBytesToRead, @dwBytesRead, NULL)
   IF bRes THEN bRes = IsTextUnicode(pBuffer, dwBytesRead, NULL)
   IF pBuffer THEN DeAllocate(pBuffer)
   IF hFile THEN CloseHandle(hFile)
   RETURN bRes
END FUNCTION
' ========================================================================================


José Roca

#17
I'm going to add a file called AfxFile.inc that will give access to all the file related classes and functions:


' ########################################################################################
' Microsoft Windows
' File: AfxFile.inc
' Contents: Includes all the WinFBX procedures and functions that deal with files and folders.
' Compiler: Free Basic 32 & 64 bit
' Copyright (c) 2018 José Roca. Freeware. Use at your own risk.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#pragma once

#include once "Afx/AfxWin.inc"
#include once "Afx/CStream.inc"
#include once "Afx/CTextStream.inc"
#include once "Afx/CFindFile.inc"
#include once "Afx/CFileSys.inc"
#include once "Afx/CFileTime.inc"


Paul Squires

Excellent idea to have an AfxFile.inc file to encapsulate all of the file routines. I will use it a lot.
The AfxIsFileUnicode  is a great addition as well.

If you ever get bored again, just ask, I have many more ideas!  LOL  :)
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

José Roca

#19
The attached file contains the updated AfxWin.inc and CStream.inc files, and the new AfxFile.inc. If somebody doesn't need to work with unicode, he can use the FB native functions instead.

> If you ever get bored again, just ask, I have many more ideas!  LOL

August is a good month because I don't have many things to do. I stopped adding code because of the lack of testers. I don't want to write code that nobody will use.

The WinFBX repository has been updated with these changes.

Paul Squires

Thanks Jose, I will ensure that the next WinFBE Suite upload includes the latest WinFBX code.
I will be replacing all internal WinFBE file handling code with this new AfxFile code.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Paul Squires

A question I get a lot from ex-PBers is a replacement for XPRINT commands. Early in my FB learning I started to build on a previous XPRINT type of code (FBWinPrint) - I called mine WinXPrint. It is horrible looking code by today's standards.
https://www.freebasic.net/forum/viewtopic.php?f=3&t=25321&p=227827
If you're feeling bored and looking for a nice challenge - XPRINT code would certainly fill a gap in the FB library universe.

Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Paul Squires

....and an all encompassing tutorial on how to build and use DLL's in Windows using FreeBASIC. Especially figuring out a good way to handle import libraries *.a which is a real pain compared to PowerBASIC. I tried to learn it many months ago and built a rudimentary utility "MakeLIB" that attempted to automate the process. That needs to be revisited.  http://www.planetsquires.com/protect/forum/index.php?topic=3675.0
I know that you use dynamic loading of the DLL functions but it would be cool if it was easy like PB.  :)
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

José Roca

Quote from: Paul Squires on August 06, 2018, 11:41:08 AM
A question I get a lot from ex-PBers is a replacement for XPRINT commands. Early in my FB learning I started to build on a previous XPRINT type of code (FBWinPrint) - I called mine WinXPrint. It is horrible looking code by today's standards.
https://www.freebasic.net/forum/viewtopic.php?f=3&t=25321&p=227827
If you're feeling bored and looking for a nice challenge - XPRINT code would certainly fill a gap in the FB library universe.

I don't even have a printer... :)

Maybe this code could be a starting point: http://www.planetsquires.com/protect/forum/index.php?topic=4059.msg30559#msg30559

Johan Klassen

actually you can use a dll without an import lib, you can #inclib "mydll" where "mydll" is mydll.dll
the dll probably needs to reside where the program using it resides, beware that if you have an import lib of the dll then perhaps FB may link agains the import lib, have not tested this case, only made sure that there was no import lib, but the dll only.

José Roca

#25
Quote from: Paul Squires on August 06, 2018, 11:48:35 AM
I know that you use dynamic loading of the DLL functions but it would be cool if it was easy like PB.  :)

The only thing that comes to my mind is to prepare the .bi file as:


TYPE PFNSQLITE3LIBVERSIONNUMBERPROC AS FUNCTION CDECL () AS LONG
DIM SHARED sqlite3_libversion_number AS PFNSQLITE3LIBVERSIONNUMBERPROC
' ... more procedures

FUNCTION InitSqLite3Lib (BYREF wszDllPath AS WSTRING = "sqlite3.dll") AS HMODULE
   DIM hLib AS HMODULE
   hLib = LoadLibraryW(wszDllPath)
   IF hLib = NULL THEN RETURN NULL
   sqlite3_libversion_number = CAST(PFNSQLITE3LIBVERSIONNUMBERPROC, GetProcAddress(hLib, "sqlite3_libversion_number"))
   ' ... more procedures
   RETURN hLib
END FUNCTION


Then, to use it:


InitSqLite3Lib("sqlite3_32.dll")
print sqlite3_libversion_number()


If it has no parameters, the use of () is mandatory; otherwise, it will print the value of the sqlite3_libversion_number pointer instead of calling the sqlite3_libversion_number function.

Another possibility is to build the .dll as a low-level COM server and use a class instead of functions.

Of course, the best solution would be to modify the compiler to accept a LIB or IMPORT clause in the declare.

José Roca

#26
Some ideas for a printing class:

1) Methods to attach a printer: AttachPrinter and ChoosePrinter.

2) Properties to set printer values, such paper size, orientation, etc.

3) Pages can be constructed using a memory bitmap and drawn using GDI and/or GDI+ and passed to a method of the class that will send them to the printer.

Criticism?

Paul Squires

Quote from: José Roca on August 07, 2018, 11:15:04 AM
Some ideas for a printing class:

1) Methods to attach a printer: AttachPrinter and ChoosePrinter.

2) Properties to set printer values, such paper size, orientation, etc.

3) Pages can be constructed using a memory bitmap and drawn using GDI and/or GDI+ and passed to a method of the class that will send them to the printer.

Criticism?


Sorry for my lateness. Yes, that is the kind of approach to a printing class that was in my mind also. Some basic properties to set elements of what is being printed, and then constructing the document in memory and output to the printer via api methods. Also being able to handle different fonts, colors, and graphics. It would be a great addition. Not sure that a Print Preview would be an absolute necessity but it would be cool also.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Paul Squires

I just finished reviewing WinFBE code. There are 10 source code files that contain 22 references to FB related file handling routines. I will post a new WinFBE package tonight and then start with replacing the FB file handling routines with Jose's classes.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

José Roca

A first try.

The idea is to create an instance of the CPrint class and attach a printer to it with AttachPrinter or ChoosePrinter.


'#CONSOLE ON
#define UNICODE
#INCLUDE ONCE "Afx/CPrint.inc"
USING Afx

DIM pPrint AS CPrint
pPrint.AttachPrinter("Bullzip PDF Printer")


Then we can use the CMemBmp class to create a memory bitmap (with the dimensions returned by the GetHorizontalResolution and GetVerticalResolution methods of the CPrint class), draw its contents using GDI or GDI+ and send it to the PrintBitmap method.

For documents with more than one page, we could build an array of memory bitpamps and send it to a future new method of the CPrint class that will print them.