mirror of
https://github.com/FellippeHeitor/InForm.git
synced 2025-01-14 19:49:33 +00:00
Fix TextFetch example
This commit is contained in:
parent
2bbd574a22
commit
a7df4058e0
2 changed files with 63 additions and 37 deletions
|
@ -9,8 +9,6 @@
|
||||||
|
|
||||||
OPTION _EXPLICIT
|
OPTION _EXPLICIT
|
||||||
|
|
||||||
REDIM SHARED Dir(1 TO 1) AS STRING, File(1 TO 1) AS STRING
|
|
||||||
|
|
||||||
': 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,16 +29,30 @@ DIM SHARED lbEnd AS LONG
|
||||||
'$INCLUDE:'../../InForm/InForm.bi'
|
'$INCLUDE:'../../InForm/InForm.bi'
|
||||||
'$INCLUDE:'TextFetch.frm'
|
'$INCLUDE:'TextFetch.frm'
|
||||||
|
|
||||||
|
' --- DIRENTRY STUFF ---
|
||||||
|
|
||||||
DECLARE LIBRARY "direntry"
|
DECLARE LIBRARY "direntry"
|
||||||
FUNCTION __load_dir%% ALIAS load_dir (s AS STRING)
|
' This opens a directly for reading it's contents
|
||||||
FUNCTION get_next_entry$ (flags AS LONG, file_size AS LONG)
|
' 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
|
SUB close_dir
|
||||||
END DECLARE
|
END DECLARE
|
||||||
|
|
||||||
FUNCTION load_dir%% (s AS STRING)
|
' This properly null-terminates the directory name before passing it to __load_dir()
|
||||||
load_dir = __load_dir(s + CHR$(0))
|
FUNCTION open_dir%% (dirName AS STRING)
|
||||||
|
open_dir = __open_dir(dirName + CHR$(0))
|
||||||
END FUNCTION
|
END FUNCTION
|
||||||
|
|
||||||
|
' --- DIRENTRY STUFF ---
|
||||||
|
|
||||||
SUB loadText
|
SUB loadText
|
||||||
DIM i AS INTEGER, b$, clip$
|
DIM i AS INTEGER, b$, clip$
|
||||||
ResetList ListTxt
|
ResetList ListTxt
|
||||||
|
@ -58,7 +70,7 @@ SUB loadDirsFilesList
|
||||||
|
|
||||||
Caption(lbCWD) = "Current Directory: " + _CWD$
|
Caption(lbCWD) = "Current Directory: " + _CWD$
|
||||||
|
|
||||||
REDIM Dir(1 TO 1), File(1 TO 1)
|
REDIM AS STRING Dir(0 TO 0), File(0 TO 0)
|
||||||
nDirs = GetCurDirLists(Dir(), File())
|
nDirs = GetCurDirLists(Dir(), File())
|
||||||
|
|
||||||
IF nDirs > 0 THEN
|
IF nDirs > 0 THEN
|
||||||
|
@ -77,33 +89,33 @@ END SUB
|
||||||
FUNCTION GetCurDirLists& (DirList() AS STRING, FileList() AS STRING)
|
FUNCTION GetCurDirLists& (DirList() AS STRING, FileList() AS STRING)
|
||||||
CONST RESIZE_BLOCK_SIZE = 64
|
CONST RESIZE_BLOCK_SIZE = 64
|
||||||
|
|
||||||
REDIM DirList(1 TO RESIZE_BLOCK_SIZE), FileList(1 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, flags, fileSize
|
||||||
DIM entryName AS STRING
|
DIM entryName AS STRING
|
||||||
|
|
||||||
IF load_dir(dirName) THEN
|
IF open_dir(dirName) THEN
|
||||||
DO
|
DO
|
||||||
entryName = get_next_entry(flags, fileSize)
|
entryName = read_dir(flags, fileSize)
|
||||||
IF entryName <> "" THEN
|
IF entryName <> "" THEN
|
||||||
SELECT CASE flags
|
SELECT CASE flags
|
||||||
CASE 1
|
CASE 1
|
||||||
dirCount = dirCount + 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(1 TO UBOUND(DirList) + RESIZE_BLOCK_SIZE)
|
|
||||||
DirList(dirCount) = entryName
|
DirList(dirCount) = entryName
|
||||||
|
dirCount = dirCount + 1
|
||||||
CASE 2
|
CASE 2
|
||||||
fileCount = fileCount + 1
|
IF fileCount > UBOUND(FileList) THEN REDIM _PRESERVE FileList(0 TO UBOUND(FileList) + RESIZE_BLOCK_SIZE) AS STRING
|
||||||
IF fileCount > UBOUND(FileList) THEN REDIM _PRESERVE FileList(1 TO UBOUND(FileList) + RESIZE_BLOCK_SIZE)
|
|
||||||
FileList(fileCount) = entryName
|
FileList(fileCount) = entryName
|
||||||
|
fileCount = fileCount + 1
|
||||||
END SELECT
|
END SELECT
|
||||||
END IF
|
END IF
|
||||||
LOOP UNTIL entryName = ""
|
LOOP UNTIL entryName = ""
|
||||||
close_dir
|
close_dir
|
||||||
END IF
|
END IF
|
||||||
|
|
||||||
REDIM _PRESERVE DirList(1 TO dirCount)
|
REDIM _PRESERVE DirList(0 TO dirCount) AS STRING
|
||||||
REDIM _PRESERVE FileList(1 TO fileCount)
|
REDIM _PRESERVE FileList(0 TO fileCount) AS STRING
|
||||||
|
|
||||||
GetCurDirLists = dirCount + fileCount ' return total count of items
|
GetCurDirLists = dirCount + fileCount ' return total count of items
|
||||||
END FUNCTION
|
END FUNCTION
|
||||||
|
@ -125,16 +137,14 @@ SUB Split (SplitMeString AS STRING, delim AS STRING, loadMeArray() AS STRING)
|
||||||
REDIM _PRESERVE loadMeArray(LBOUND(loadMeArray) TO arrpos) AS STRING 'get the ubound correct
|
REDIM _PRESERVE loadMeArray(LBOUND(loadMeArray) TO arrpos) AS STRING 'get the ubound correct
|
||||||
END SUB
|
END SUB
|
||||||
|
|
||||||
FUNCTION fileStr$ (txtFile$)
|
FUNCTION fileStr$ (txtFile AS STRING)
|
||||||
DIM rtn$
|
IF _FILEEXISTS(txtFile) THEN
|
||||||
IF _FILEEXISTS(txtFile$) THEN
|
DIM ff AS LONG: ff = FREEFILE
|
||||||
OPEN txtFile$ FOR BINARY AS #1
|
OPEN txtFile FOR BINARY AS ff
|
||||||
rtn$ = SPACE$(LOF(1))
|
fileStr$ = INPUT$(LOF(ff), ff)
|
||||||
GET #1, , rtn$
|
CLOSE ff
|
||||||
CLOSE #1
|
|
||||||
fileStr$ = rtn$
|
|
||||||
END IF
|
END IF
|
||||||
END FUNCTION 'last line 317 + CRLF always added at end of .bas files
|
END FUNCTION
|
||||||
|
|
||||||
FUNCTION rightOf$ (source$, of$)
|
FUNCTION rightOf$ (source$, of$)
|
||||||
IF INSTR(source$, of$) > 0 THEN rightOf$ = MID$(source$, INSTR(source$, of$) + LEN(of$))
|
IF INSTR(source$, of$) > 0 THEN rightOf$ = MID$(source$, INSTR(source$, of$) + LEN(of$))
|
||||||
|
@ -162,7 +172,9 @@ SUB __UI_BeforeUnload
|
||||||
END SUB
|
END SUB
|
||||||
|
|
||||||
SUB __UI_Click (id AS LONG)
|
SUB __UI_Click (id AS LONG)
|
||||||
DIM dir$, fi$, fs$, i AS INTEGER, value AS INTEGER
|
DIM AS LONG value, i
|
||||||
|
DIM AS STRING dir, fi, fs, fa(0)
|
||||||
|
|
||||||
SELECT CASE id
|
SELECT CASE id
|
||||||
CASE frmTextFetch
|
CASE frmTextFetch
|
||||||
|
|
||||||
|
@ -184,11 +196,10 @@ SUB __UI_Click (id AS LONG)
|
||||||
fi$ = GetItem$(ListFiles, Control(ListFiles).Value)
|
fi$ = GetItem$(ListFiles, Control(ListFiles).Value)
|
||||||
IF _FILEEXISTS(fi$) THEN
|
IF _FILEEXISTS(fi$) THEN
|
||||||
fs$ = fileStr$(fi$)
|
fs$ = fileStr$(fi$)
|
||||||
REDIM fa$(0)
|
Split fs$, CHR$(13) + CHR$(10), fa()
|
||||||
Split fs$, CHR$(13) + CHR$(10), fa$()
|
|
||||||
ResetList ListFile
|
ResetList ListFile
|
||||||
FOR i = LBOUND(fa$) TO UBOUND(fa$)
|
FOR i = LBOUND(fa) TO UBOUND(fa)
|
||||||
AddItem ListFile, fa$(i)
|
AddItem ListFile, fa(i)
|
||||||
NEXT
|
NEXT
|
||||||
'clear
|
'clear
|
||||||
Caption(lbStart) = "Line Start"
|
Caption(lbStart) = "Line Start"
|
||||||
|
@ -414,6 +425,7 @@ SUB __UI_TextChanged (id AS LONG)
|
||||||
END SUB
|
END SUB
|
||||||
|
|
||||||
SUB __UI_ValueChanged (id AS LONG)
|
SUB __UI_ValueChanged (id AS LONG)
|
||||||
|
|
||||||
SELECT CASE id
|
SELECT CASE id
|
||||||
CASE ListDirs
|
CASE ListDirs
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
|
// Cross-platform directory iterator
|
||||||
|
// Original version by Steve McNeill
|
||||||
// Changes and enhancements by a740g (24-June-2023)
|
// Changes and enhancements by a740g (24-June-2023)
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <cstdint>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <stdint.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
@ -20,8 +23,12 @@ enum : int32_t
|
||||||
|
|
||||||
static DIR *p_dir = nullptr;
|
static DIR *p_dir = nullptr;
|
||||||
|
|
||||||
int8_t load_dir(const char *path)
|
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);
|
p_dir = opendir(path);
|
||||||
if (!p_dir)
|
if (!p_dir)
|
||||||
return QB_FALSE;
|
return QB_FALSE;
|
||||||
|
@ -29,12 +36,16 @@ int8_t load_dir(const char *path)
|
||||||
return QB_TRUE;
|
return QB_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *get_next_entry(int *flags, int *file_size)
|
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);
|
auto next_entry = readdir(p_dir);
|
||||||
|
|
||||||
if (!next_entry)
|
if (!next_entry)
|
||||||
return ""; // return an empty string to indicate we have nothing
|
return dir_name; // return an empty string to indicate we have nothing
|
||||||
|
|
||||||
struct stat entry_info;
|
struct stat entry_info;
|
||||||
stat(next_entry->d_name, &entry_info);
|
stat(next_entry->d_name, &entry_info);
|
||||||
|
@ -42,11 +53,14 @@ const char *get_next_entry(int *flags, int *file_size)
|
||||||
*flags = S_ISDIR(entry_info.st_mode) ? IS_DIR_FLAG : IS_FILE_FLAG;
|
*flags = S_ISDIR(entry_info.st_mode) ? IS_DIR_FLAG : IS_FILE_FLAG;
|
||||||
*file_size = entry_info.st_size;
|
*file_size = entry_info.st_size;
|
||||||
|
|
||||||
return next_entry->d_name; // QB64-PE does the right thing with this
|
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()
|
void close_dir()
|
||||||
{
|
{
|
||||||
closedir(p_dir);
|
closedir(p_dir);
|
||||||
p_dir = nullptr;
|
p_dir = nullptr; // set this to NULL so that subsequent __open_dir() works correctly
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue