📄 files.c
字号:
/******************************************************************
* SEAL 2.0 *
* Copyright (c) 1999-2002 SEAL Developers. All Rights Reserved. *
* *
* Web site: http://sealsystem.sourceforge.net/ *
* E-mail (current maintainer): orudge@users.sourceforge.net *
******************************************************************/
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <seal.h>
#include <files.h>
#include <vfile.h>
#include <io.h>
#include <fcntl.h>
#include <dir.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <lnk.h>
#ifndef BUFFER_FILE_COPY
#define BUFFER_FILE_COPY 2048
#endif
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
t_file *new_tfile ( l_text path, l_text filename, l_int attrib, l_word time, l_word date, l_dword size )
{
t_file *p = (t_file *)_malloc(sizeof(t_file));
if ( p ) {
clear_type(p, sizeof(t_file));
p->path = _strdup(path);
p->filename = _strdup(filename);
p->attrib = attrib;
p->time = time;
p->date = date;
p->size = size;
};
return p;
};
////////////////////////////////////////////////////////////////////////////////
void free_tfile ( void *p )
{
if ( p ) {
if ( ((t_file*)p)->filename ) _free(((t_file*)p)->filename);
if ( ((t_file*)p)->path ) _free(((t_file*)p)->path);
};
_free(p);
};
////////////////////////////////////////////////////////////////////////////////
void io_cleartfile ( t_file *f )
{
if ( f ) {
if ( f->path ) _free(f->path);
if ( f->filename) _free(f->filename);
};
};
////////////////////////////////////////////////////////////////////////////////
t_file io_filetotfile ( l_text filename )
{
l_text path = io_path(filename);
l_text file = io_filename(filename);
t_file f;
clear_type(&f, sizeof(t_file));
f.path = path;
f.filename = file;
return f;
};
////////////////////////////////////////////////////////////////////////////////
t_dirinfo io_foreach_file_ex ( l_text path, l_int flags, l_int (*callback)(), p_object o, l_dword *ind )
{
t_dirinfo all;
clear_type(&all, sizeof(t_dirinfo));
if ( path ) {
struct t_ffblk f;
l_text files = io_realpath(path, "*.*");
l_int done = io_findfirst(files, &f, FA_ALL);
_while ( !done && obj_exist(o) != TAG_DISPOSE ) { /* multitasking while */
if ( f.ff_filename && f.info.ff_attrib & FA_DIREC && stricmp(f.ff_filename, "..") &&
stricmp(f.ff_filename, ".") ) { /* found directory */
l_text f1 = io_realpath(path, f.ff_filename); /* get real filename */
t_dirinfo dir = io_foreach_file_ex(f1, flags, callback, o, ind);
if ( ind ) (*ind)++;
all.files += dir.files;
all.dirs += dir.dirs+1;
all.size += dir.size;
all.flags = dir.flags;
_free(f1);
} else if ( !(f.info.ff_attrib & FA_DIREC) ) { /* found file */
l_text curfile = io_realpath(path, f.ff_filename);
all.files++;
if ( ind ) (*ind)++;
if ( flags & DIF_SIZE ) { /* get also sizes of files */
l_int fl = _open(curfile, O_RDONLY);
if ( fl != -1 ) {
all.size += filelength(fl);
close(fl);
};
};
if ( callback ) callback(curfile);
_free(curfile);
};
done = io_findnext(&f);
};
if ( !done ) all.flags |= DIF_HALT;
_free(files);
};
if ( path && flags & DIF_DIRCALLBACK && callback ) { /* call calback for directory */
if ( ind ) (*ind)++;
callback(path);
};
return all;
};
////////////////////////////////////////////////////////////////////////////////
l_int io_foreach_file_copy ( l_text dst, l_text path, p_object o, l_dword *ind )
{
l_int ok = 0;
if ( path ) {
struct t_ffblk f;
l_text files = io_realpath(path, "*.*");
l_int done = io_findfirst(files, &f, FA_ALL);
ok = _io_copyfile(dst, path);
if ( ind ) (*ind)++;
_while ( !done && obj_exist(o) != TAG_DISPOSE ) { /* multitasking while */
if ( f.ff_filename && f.info.ff_attrib & FA_DIREC && stricmp(f.ff_filename, "..") &&
stricmp(f.ff_filename, ".") ) { /* found directory */
l_text f1 = io_realpath(path, f.ff_filename); /* get real filename */
l_text dst1 = io_realpath(dst, f.ff_filename); /* get real filename */
ok = io_foreach_file_copy(dst1, f1, o, ind);
_free(f1);
_free(dst1);
} else if ( !(f.info.ff_attrib & FA_DIREC) ) { /* found file */
l_text curfile = io_realpath(path, f.ff_filename);
l_text dstfile = io_realpath(dst, f.ff_filename);
ok = _io_copyfile(dstfile, curfile);
if ( ind ) (*ind)++;
_free(curfile);
_free(dstfile);
};
done = io_findnext(&f);
};
_free(files);
};
return ok;
};
////////////////////////////////////////////////////////////////////////////////
l_int io_removefile ( p_object ob, t_file *f, l_dword *ind )
{
if ( f ) {
t_dirinfo ok;
l_text path = io_realpath(f->path, f->filename);
if ( f->attrib & FA_DIREC )
ok = io_foreach_file_ex(path, DIF_DIRCALLBACK, &_io_removefile, ob, ind);
else {
l_bool ok2 = _io_removefile(path);
if ( ind ) (*ind)++;
_free(path);
return ok2;
};
_free(path);
return (!(ok.flags & DIF_HALT));
};
return false;
};
////////////////////////////////////////////////////////////////////////////////
l_int io_copyfile ( p_object o, t_file *dst, t_file *src, l_dword *ind )
{
if ( src && dst ) {
l_text srcpath = io_realpath(src->path, src->filename);
l_text dpath = io_realpath(dst->path, dst->filename);
l_text dstpath = io_realpath(dpath, src->filename);
l_int ret = io_foreach_file_copy(dstpath, srcpath, o, ind);
_free(dpath);
_free(dstpath);
_free(srcpath);
return ret;
};
return 0;
};
////////////////////////////////////////////////////////////////////////////////
l_int _io_copyfile ( l_text dst, l_text src )
{
if ( !dst || !src || !stricmp(dst, src) ) return false;
if ( io_isdir(src) ) { /* (src) is directory */
return mkdir(dst, S_IWUSR/* ignored under DOS */);
} else { /* (src) is file */
static l_char buffer[BUFFER_FILE_COPY];
FILE *s = fopen(src, "rb");
FILE *d = fopen(dst, "wb");
l_bool ok = false;
if ( s && d ) {
ok = true;
_while ( !feof(s) ) { /* multitasking while */
l_dword size = fread(buffer, sizeof(l_char), BUFFER_FILE_COPY, s);
fwrite(buffer, sizeof(l_char), size, d);
};
};
fclose(s);
fclose(d);
return ok;
};
return false;
};
////////////////////////////////////////////////////////////////////////////////
l_int io_numberfile ( p_object ob, t_file *f, l_dword *ind )
{
if ( f ) {
if ( f->attrib & FA_DIREC ) {
l_text path = io_realpath(f->path, f->filename);
io_foreach_file_ex(path, 0, NULL, NULL, ind);
_free(path);
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -