mirror of
https://github.com/FellippeHeitor/InForm.git
synced 2025-01-15 03:49:56 +00:00
Update TextFetch example to use QB64-PE v3.11.0 features
This commit is contained in:
parent
b62e99935b
commit
a76449ad62
2 changed files with 30 additions and 115 deletions
|
@ -1,5 +1,5 @@
|
||||||
' Text Fetch.bas started b+ 2019-11-12 from other work with Dirs and Files loading
|
' 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
|
': This program uses
|
||||||
': InForm - GUI library for QB64 - v1.0
|
': InForm - GUI library for QB64 - v1.0
|
||||||
|
@ -9,6 +9,10 @@
|
||||||
|
|
||||||
OPTION _EXPLICIT
|
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: ------------------------------------------------------------------
|
': Controls' IDs: ------------------------------------------------------------------
|
||||||
DIM SHARED frmTextFetch AS LONG
|
DIM SHARED frmTextFetch AS LONG
|
||||||
DIM SHARED lbCWD AS LONG
|
DIM SHARED lbCWD AS LONG
|
||||||
|
@ -31,30 +35,6 @@ DIM SHARED lbEnd AS LONG
|
||||||
'$INCLUDE:'../../InForm/xp.uitheme'
|
'$INCLUDE:'../../InForm/xp.uitheme'
|
||||||
'$INCLUDE:'../../InForm/InForm.ui'
|
'$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
|
SUB loadText
|
||||||
DIM i AS INTEGER, b$, clip$
|
DIM i AS INTEGER, b$, clip$
|
||||||
ResetList ListTxt
|
ResetList ListTxt
|
||||||
|
@ -93,20 +73,18 @@ 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)
|
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 dirName AS STRING: dirName = _CWD$ + "/" ' we'll enumerate the current directory contents
|
||||||
DIM AS LONG dirCount, fileCount, flags, fileSize
|
DIM AS LONG dirCount, fileCount
|
||||||
DIM entryName AS STRING, ext AS STRING
|
DIM entryName AS STRING, ext AS STRING
|
||||||
|
|
||||||
IF open_dir(dirName) THEN
|
entryName = _FILES$(dirName)
|
||||||
|
|
||||||
DO
|
DO
|
||||||
entryName = read_dir(flags, fileSize)
|
IF RIGHT$(entryName, 1) = "/" OR RIGHT$(entryName, 1) = "\" THEN
|
||||||
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
|
IF dirCount > UBOUND(DirList) THEN REDIM _PRESERVE DirList(0 TO UBOUND(DirList) + RESIZE_BLOCK_SIZE) AS STRING
|
||||||
DirList(dirCount) = entryName
|
DirList(dirCount) = entryName
|
||||||
dirCount = dirCount + 1
|
dirCount = dirCount + 1
|
||||||
CASE 2
|
ELSE
|
||||||
ext = LCASE$(MID$(entryName, 1 + _INSTRREV(entryName, ".")))
|
ext = LCASE$(MID$(entryName, 1 + _INSTRREV(entryName, ".")))
|
||||||
|
|
||||||
SELECT CASE ext
|
SELECT CASE ext
|
||||||
|
@ -115,11 +93,10 @@ FUNCTION GetCurDirLists& (DirList() AS STRING, FileList() AS STRING)
|
||||||
FileList(fileCount) = entryName
|
FileList(fileCount) = entryName
|
||||||
fileCount = fileCount + 1
|
fileCount = fileCount + 1
|
||||||
END SELECT
|
END SELECT
|
||||||
END SELECT
|
|
||||||
END IF
|
END IF
|
||||||
|
|
||||||
|
entryName = _FILES$
|
||||||
LOOP UNTIL LEN(entryName) = 0
|
LOOP UNTIL LEN(entryName) = 0
|
||||||
close_dir
|
|
||||||
END IF
|
|
||||||
|
|
||||||
REDIM _PRESERVE DirList(0 TO dirCount) AS STRING
|
REDIM _PRESERVE DirList(0 TO dirCount) AS STRING
|
||||||
REDIM _PRESERVE FileList(0 TO fileCount) AS STRING
|
REDIM _PRESERVE FileList(0 TO fileCount) AS STRING
|
||||||
|
@ -428,6 +405,10 @@ END SUB
|
||||||
|
|
||||||
SUB __UI_TextChanged (id AS LONG)
|
SUB __UI_TextChanged (id AS LONG)
|
||||||
SELECT CASE id
|
SELECT CASE id
|
||||||
|
CASE lbTxt
|
||||||
|
|
||||||
|
CASE ListTxt
|
||||||
|
|
||||||
END SELECT
|
END SELECT
|
||||||
END SUB
|
END SUB
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in a new issue