1
1
Fork 0
mirror of https://github.com/FellippeHeitor/InForm.git synced 2024-05-12 06:50:12 +00:00

Update TextFetch example to use QB64-PE v3.11.0 features

This commit is contained in:
Samuel Gomes 2024-01-05 09:41:10 +05:30
parent b62e99935b
commit a76449ad62
2 changed files with 30 additions and 115 deletions

View file

@ -1,5 +1,5 @@
' Text Fetch.bas started b+ 2019-11-12 from other work with Dirs and Files loading
' Changes and updates by a740g (1-Dec-2023). Also simplified direntry.h
' Updated to work with QB64-PE 3.11.0 by a740g (5-Jan-2024)
'
': This program uses
': InForm - GUI library for QB64 - v1.0
@ -9,6 +9,10 @@
OPTION _EXPLICIT
$IF VERSION < 3.11.0 THEN
$ERROR This requires the latest version of QB64-PE from https://github.com/QB64-Phoenix-Edition/QB64pe/releases
$END IF
': Controls' IDs: ------------------------------------------------------------------
DIM SHARED frmTextFetch AS LONG
DIM SHARED lbCWD AS LONG
@ -31,30 +35,6 @@ DIM SHARED lbEnd AS LONG
'$INCLUDE:'../../InForm/xp.uitheme'
'$INCLUDE:'../../InForm/InForm.ui'
' --- DIRENTRY STUFF ---
DECLARE LIBRARY "direntry"
' This opens a directly for reading it's contents
' IMPORTANT: Call the open_dir() wrapper instead of calling this directly. open_dir() properly null-terminates the directory string
FUNCTION __open_dir%% (dirName AS STRING)
' This reads a single directory entry. You can call this repeatedly
' It will return an empty string once all entries have been read
' "flags" and "fileSize" are output parameters (i.e. use a variable)
' If "flag" is 1 then it is a directory and 2 if it is a file
FUNCTION read_dir$ (flags AS LONG, fileSize AS LONG)
' Close the directory. This must be called before open_dir() or read_dir$() can be used again
SUB close_dir
END DECLARE
' This properly null-terminates the directory name before passing it to __load_dir()
FUNCTION open_dir%% (dirName AS STRING)
open_dir = __open_dir(dirName + CHR$(0))
END FUNCTION
' --- DIRENTRY STUFF ---
SUB loadText
DIM i AS INTEGER, b$, clip$
ResetList ListTxt
@ -93,33 +73,30 @@ FUNCTION GetCurDirLists& (DirList() AS STRING, FileList() AS STRING)
REDIM AS STRING DirList(0 TO RESIZE_BLOCK_SIZE), FileList(0 TO RESIZE_BLOCK_SIZE) ' resize the file and dir list arrays (and toss contents)
DIM dirName AS STRING: dirName = _CWD$ ' we'll enumerate the current directory contents
DIM AS LONG dirCount, fileCount, flags, fileSize
DIM dirName AS STRING: dirName = _CWD$ + "/" ' we'll enumerate the current directory contents
DIM AS LONG dirCount, fileCount
DIM entryName AS STRING, ext AS STRING
IF open_dir(dirName) THEN
DO
entryName = read_dir(flags, fileSize)
IF LEN(entryName) <> 0 THEN
SELECT CASE flags
CASE 1
IF dirCount > UBOUND(DirList) THEN REDIM _PRESERVE DirList(0 TO UBOUND(DirList) + RESIZE_BLOCK_SIZE) AS STRING
DirList(dirCount) = entryName
dirCount = dirCount + 1
CASE 2
ext = LCASE$(MID$(entryName, 1 + _INSTRREV(entryName, ".")))
entryName = _FILES$(dirName)
SELECT CASE ext
CASE "", "txt", "log", "md", "lst", "bas", "bi", "bm", "frm", "vb", "cls", "c", "cpp", "h", "cc", "cxx", "c++", "hh", "hpp", "hxx", "h++", "cppm", "ixx"
IF fileCount > UBOUND(FileList) THEN REDIM _PRESERVE FileList(0 TO UBOUND(FileList) + RESIZE_BLOCK_SIZE) AS STRING
FileList(fileCount) = entryName
fileCount = fileCount + 1
END SELECT
END SELECT
END IF
LOOP UNTIL LEN(entryName) = 0
close_dir
END IF
DO
IF RIGHT$(entryName, 1) = "/" OR RIGHT$(entryName, 1) = "\" THEN
IF dirCount > UBOUND(DirList) THEN REDIM _PRESERVE DirList(0 TO UBOUND(DirList) + RESIZE_BLOCK_SIZE) AS STRING
DirList(dirCount) = entryName
dirCount = dirCount + 1
ELSE
ext = LCASE$(MID$(entryName, 1 + _INSTRREV(entryName, ".")))
SELECT CASE ext
CASE "", "txt", "log", "md", "lst", "bas", "bi", "bm", "frm", "vb", "cls", "c", "cpp", "h", "cc", "cxx", "c++", "hh", "hpp", "hxx", "h++", "cppm", "ixx"
IF fileCount > UBOUND(FileList) THEN REDIM _PRESERVE FileList(0 TO UBOUND(FileList) + RESIZE_BLOCK_SIZE) AS STRING
FileList(fileCount) = entryName
fileCount = fileCount + 1
END SELECT
END IF
entryName = _FILES$
LOOP UNTIL LEN(entryName) = 0
REDIM _PRESERVE DirList(0 TO dirCount) AS STRING
REDIM _PRESERVE FileList(0 TO fileCount) AS STRING
@ -428,6 +405,10 @@ END SUB
SUB __UI_TextChanged (id AS LONG)
SELECT CASE id
CASE lbTxt
CASE ListTxt
END SELECT
END SUB

View file

@ -1,66 +0,0 @@
// Cross-platform directory iterator
// Original version by Steve McNeill
// Changes and enhancements by a740g (24-June-2023)
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
enum : int8_t
{
QB_TRUE = -1,
QB_FALSE
};
enum : int32_t
{
IS_DIR_FLAG = 1,
IS_FILE_FLAG
};
static DIR *p_dir = nullptr;
int8_t __open_dir(const char *path)
{
if (p_dir)
return QB_FALSE; // return false if a directory is already open
p_dir = opendir(path);
if (!p_dir)
return QB_FALSE;
return QB_TRUE;
}
const char *read_dir(int *flags, int *file_size)
{
static char dir_name[4096]; // 4k static buffer
dir_name[0] = 0; // set to empty string
auto next_entry = readdir(p_dir);
if (!next_entry)
return dir_name; // return an empty string to indicate we have nothing
struct stat entry_info;
stat(next_entry->d_name, &entry_info);
*flags = S_ISDIR(entry_info.st_mode) ? IS_DIR_FLAG : IS_FILE_FLAG;
*file_size = entry_info.st_size;
strncpy(dir_name, next_entry->d_name, sizeof(dir_name));
dir_name[sizeof(dir_name)] = 0; // overflow protection
return dir_name; // QB64-PE does the right thing with this
}
void close_dir()
{
closedir(p_dir);
p_dir = nullptr; // set this to NULL so that subsequent __open_dir() works correctly
}