From 05fe8d844635bdde265276b110d397cc66fb677e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Fri, 1 Jan 2010 18:45:35 +0000 Subject: [PATCH] [INTERPRETER] * NEW: A new interpreter API to get temporary file names. * NEW: A new interpreter API to copy a file. * NEW: Rename the GB.GetTempDir() function to GB.TempDir(), and fix all components using it. [GB.GTK] * NEW: SvgImage is a new class that allows to generate SVG files by painting on it. But it cannot render them. git-svn-id: svn://localhost/gambas/trunk@2583 867c0c6c-44f3-4631-809d-bfa615b0a4ec --- app/src/gambas3/.project | 2 +- gb.db.sqlite2/src/main.cpp | 4 +- gb.db.sqlite3/src/main.cpp | 4 +- gb.gtk/src/Makefile.am | 1 + gb.gtk/src/cpaint_impl.cpp | 14 + gb.gtk/src/csvgimage.cpp | 108 +++++ gb.gtk/src/csvgimage.h | 52 +++ gb.gtk/src/main.cpp | 5 +- gb.gtk/src/main.h | 1 + main/gbx/gbx_api.c | 30 +- main/gbx/gbx_api.h | 4 +- main/lib/db/sqlite.c | 2 +- main/share/gambas.h | 4 +- main/share/gb_file_share.h | 2 +- main/share/gb_file_temp.h | 934 ++++++++++++++++++------------------- 15 files changed, 688 insertions(+), 479 deletions(-) create mode 100644 gb.gtk/src/csvgimage.cpp create mode 100644 gb.gtk/src/csvgimage.h diff --git a/app/src/gambas3/.project b/app/src/gambas3/.project index aa29c883c..93a81aca7 100644 --- a/app/src/gambas3/.project +++ b/app/src/gambas3/.project @@ -1,5 +1,5 @@ # Gambas Project File 3.0 -# Compiled with Gambas 2.99.0 +# Compiled with Gambas 2.99.0 (r2578) Title=Gambas 3 Startup=Project StackTrace=1 diff --git a/gb.db.sqlite2/src/main.cpp b/gb.db.sqlite2/src/main.cpp index 23c6cc2e3..24018db02 100644 --- a/gb.db.sqlite2/src/main.cpp +++ b/gb.db.sqlite2/src/main.cpp @@ -395,7 +395,7 @@ static char *FindDatabase (const char *name, const char *hostName) } #endif - GB.NewString(&fullpath, GB.GetTempDir(), 0); + GB.NewString(&fullpath, GB.TempDir(), 0); GB.AddString(&fullpath, "/sqlite/", 0); GB.AddString(&fullpath, name, 0); @@ -431,7 +431,7 @@ static char *GetDatabaseHome() } */ - sprintf(dbhome, "%s/sqlite", GB.GetTempDir()); + sprintf(dbhome, "%s/sqlite", GB.TempDir()); } else { strcpy(dbhome, env); diff --git a/gb.db.sqlite3/src/main.cpp b/gb.db.sqlite3/src/main.cpp index db5cd8e56..bdac1fad7 100644 --- a/gb.db.sqlite3/src/main.cpp +++ b/gb.db.sqlite3/src/main.cpp @@ -469,7 +469,7 @@ static char *FindDatabase(const char *name, const char *hostName) } #endif - GB.NewString(&fullpath, GB.GetTempDir(), 0); + GB.NewString(&fullpath, GB.TempDir(), 0); GB.AddString(&fullpath, "/sqlite/", 0); GB.AddString(&fullpath, name, 0); @@ -507,7 +507,7 @@ static char *GetDatabaseHome() } */ - sprintf(dbhome, "%s/sqlite", GB.GetTempDir()); + sprintf(dbhome, "%s/sqlite", GB.TempDir()); } else { diff --git a/gb.gtk/src/Makefile.am b/gb.gtk/src/Makefile.am index a9852bdd5..73f59b798 100644 --- a/gb.gtk/src/Makefile.am +++ b/gb.gtk/src/Makefile.am @@ -57,6 +57,7 @@ gb_gtk_la_SOURCES = \ CScrollView.h CScrollView.cpp \ CMenu.h CMenu.cpp CTrayIcon.h CTrayIcon.cpp CWindow.h CWindow.cpp \ cprinter.h cprinter.cpp \ + csvgimage.h csvgimage.cpp \ main.h main.cpp \ gkey.h \ gcursor.h \ diff --git a/gb.gtk/src/cpaint_impl.cpp b/gb.gtk/src/cpaint_impl.cpp index a24386561..c0e97ae65 100644 --- a/gb.gtk/src/cpaint_impl.cpp +++ b/gb.gtk/src/cpaint_impl.cpp @@ -35,6 +35,7 @@ #include "CPicture.h" #include "CImage.h" #include "cprinter.h" +#include "csvgimage.h" #include "CFont.h" #include "CDraw.h" #include "cpaint_impl.h" @@ -190,6 +191,14 @@ static int Begin(GB_PAINT *d) rx = (int)gtk_print_context_get_dpi_x(context); ry = (int)gtk_print_context_get_dpi_y(context); } + else if (GB.Is(device, CLASS_SvgImage)) + { + CSVGIMAGE *svgimage = ((CSVGIMAGE *)device); + target = svgimage->surface; + cairo_surface_reference(target); + w = svgimage->width; + h = svgimage->height; + } else return TRUE; @@ -210,6 +219,11 @@ static void End(GB_PAINT *d) if (wid->cached()) wid->setCache(); } + else if (GB.Is(device, CLASS_SvgImage)) + { + CSVGIMAGE *svgimage = ((CSVGIMAGE *)device); + cairo_surface_finish(svgimage->surface); + } } static void Save(GB_PAINT *d) diff --git a/gb.gtk/src/csvgimage.cpp b/gb.gtk/src/csvgimage.cpp new file mode 100644 index 000000000..9e63a5dc1 --- /dev/null +++ b/gb.gtk/src/csvgimage.cpp @@ -0,0 +1,108 @@ +/*************************************************************************** + + csvgimage.cpp + + (c) 2004-2006 - Daniel Campos Fernández + + 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, 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. + +***************************************************************************/ + +#define __CSVGIMAGE_CPP + +#include +#include + +#include "main.h" +#include "gambas.h" +#include "widgets.h" +#include "cpaint_impl.h" +#include "csvgimage.h" + +#define MM_TO_PT(_mm) ((int)((_mm) * 72 / 25.4 + 0.5)) +#define PT_TO_MM(_pt) ((_pt) / 72 * 25.4) + +static void init(CSVGIMAGE *_object) +{ + GB.NewString(&THIS->file, GB.TempFile(NULL), 0); + THIS->surface = cairo_svg_surface_create(THIS->file, THIS->width, THIS->height); +} + +static void release(CSVGIMAGE *_object) +{ + cairo_surface_destroy(THIS->surface); + unlink(THIS->file); + GB.FreeString(&THIS->file); +} + +BEGIN_METHOD(SvgImage_new, GB_FLOAT width; GB_FLOAT height) + + THIS->width = MM_TO_PT(VARG(width)); + THIS->height = MM_TO_PT(VARG(height)); + init(THIS); + +END_METHOD + +BEGIN_METHOD_VOID(SvgImage_free) + + release(THIS); + +END_METHOD + +BEGIN_PROPERTY(SvgImage_Width) + + GB.ReturnInteger(PT_TO_MM(THIS->width)); + +END_PROPERTY + +BEGIN_PROPERTY(SvgImage_Height) + + GB.ReturnInteger(PT_TO_MM(THIS->height)); + +END_PROPERTY + +BEGIN_METHOD(SvgImage_Save, GB_STRING file) + + GB.CopyFile(THIS->file, GB.FileName(STRING(file), LENGTH(file))); + +END_METHOD + +BEGIN_METHOD_VOID(SvgImage_Clear) + + release(THIS); + init(THIS); + +END_METHOD + +GB_DESC SvgImageDesc[] = +{ + GB_DECLARE("SvgImage", sizeof(CSVGIMAGE)), + + GB_METHOD("_new", 0, SvgImage_new, "(Width)f(Height)f"), + GB_METHOD("_free", 0, SvgImage_free, 0), + + GB_PROPERTY_READ("Width", "f", SvgImage_Width), + GB_PROPERTY_READ("Height", "f", SvgImage_Height), + + //GB_STATIC_METHOD("Load", "Picture", CPICTURE_load, "(Path)s"), + GB_METHOD("Save", 0, SvgImage_Save, "(Path)s"), + + GB_METHOD("Clear", 0, SvgImage_Clear, 0), + + GB_INTERFACE("Paint", &PAINT_Interface), + + GB_END_DECLARE +}; + diff --git a/gb.gtk/src/csvgimage.h b/gb.gtk/src/csvgimage.h new file mode 100644 index 000000000..b46244a4b --- /dev/null +++ b/gb.gtk/src/csvgimage.h @@ -0,0 +1,52 @@ +/*************************************************************************** + + csvgimage.h + + (c) 2004-2006 - Daniel Campos Fernández + + 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, 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. + +***************************************************************************/ + +#ifndef __CSVGIMAGE_H +#define __CSVGIMAGE_H + +#include "gambas.h" +#include "widgets.h" +#include + +typedef + struct + { + GB_BASE ob; + cairo_surface_t *surface; + char *file; + int width; + int height; + } + CSVGIMAGE; + +#ifndef __CSVGIMAGE_CPP + +extern GB_DESC SvgImageDesc[]; + +#else + +#define THIS OBJECT(CSVGIMAGE) +#define SURFACE (THIS->surface) + +#endif + +#endif diff --git a/gb.gtk/src/main.cpp b/gb.gtk/src/main.cpp index b3269ec2f..7687fddc3 100644 --- a/gb.gtk/src/main.cpp +++ b/gb.gtk/src/main.cpp @@ -69,6 +69,7 @@ #include "CGridView.h" #include "CSeparator.h" #include "cprinter.h" +#include "csvgimage.h" #include #include @@ -79,6 +80,7 @@ GB_CLASS CLASS_DrawingArea; GB_CLASS CLASS_Menu; GB_CLASS CLASS_Window; GB_CLASS CLASS_Printer; +GB_CLASS CLASS_SvgImage; static void my_lang(char *lang,int rtl1); static void my_error(int code,char *error,char *where); @@ -196,6 +198,7 @@ extern "C" CGridViewDesc, CStockDesc, PrinterDesc, + SvgImageDesc, NULL }; @@ -253,8 +256,8 @@ extern "C" //CLASS_Drawing = GB.FindClass("Drawing"); CLASS_DrawingArea = GB.FindClass("DrawingArea"); CLASS_Printer = GB.FindClass("Printer"); - //CLASS_ScrollView = GB.FindClass("ScrollView"); CLASS_Image = GB.FindClass("Image"); + CLASS_SvgImage = GB.FindClass("SvgImage"); return TRUE; } diff --git a/gb.gtk/src/main.h b/gb.gtk/src/main.h index 2c53a5fe0..2b6899be0 100644 --- a/gb.gtk/src/main.h +++ b/gb.gtk/src/main.h @@ -38,6 +38,7 @@ extern GB_CLASS CLASS_DrawingArea; extern GB_CLASS CLASS_Menu; extern GB_CLASS CLASS_Window; extern GB_CLASS CLASS_Printer; +extern GB_CLASS CLASS_SvgImage; #endif diff --git a/main/gbx/gbx_api.c b/main/gbx/gbx_api.c index a21d2430e..844244cbe 100644 --- a/main/gbx/gbx_api.c +++ b/main/gbx/gbx_api.c @@ -165,7 +165,9 @@ void *GAMBAS_Api[] = (void *)GB_LoadFile, (void *)STREAM_unmap, (void *)FILE_exist, - (void *)GB_GetTempDir, + (void *)GB_TempDir, + (void *)GB_TempFile, + (void *)GB_CopyFile, (void *)GB_Store, (void *)GB_StoreString, @@ -1639,11 +1641,35 @@ int GB_toupper(int c) return toupper(c); } -char *GB_GetTempDir(void) +char *GB_TempDir(void) { return FILE_make_temp(NULL, NULL); } +char *GB_TempFile(const char *pattern) +{ + int len; + return FILE_make_temp(&len, pattern); +} + +int GB_CopyFile(const char *src, const char *dst) +{ + int ret = 0; + + TRY + { + FILE_copy(src, dst); + } + CATCH + { + ret = 1; + GAMBAS_Error = TRUE; + } + END_TRY + + return ret; +} + char *GB_RealFileName(const char *name, int len) { char *path = STRING_conv_file_name(name, len); diff --git a/main/gbx/gbx_api.h b/main/gbx/gbx_api.h index 61d091003..e8b47ddc5 100644 --- a/main/gbx/gbx_api.h +++ b/main/gbx/gbx_api.h @@ -85,8 +85,10 @@ char *GB_ToZeroString(GB_STRING *src); int GB_LoadFile(const char *path, int lenp, char **addr, int *len); //void GB_ReleaseFile(char **addr, int len); #define GB_ReleaseFile STREAM_unmap -char *GB_GetTempDir(void); char *GB_RealFileName(const char *path, int len); +char *GB_TempDir(void); +char *GB_TempFile(const char *pattern); +int GB_CopyFile(const char *src, const char *dst); int GB_IsMissing(int param); diff --git a/main/lib/db/sqlite.c b/main/lib/db/sqlite.c index aadfa62cc..aa3507c46 100644 --- a/main/lib/db/sqlite.c +++ b/main/lib/db/sqlite.c @@ -129,7 +129,7 @@ static char *FindDatabase(char *name, char *hostName) } } - GB.NewString(&fullpath, GB.GetTempDir(), 0); + GB.NewString(&fullpath, GB.TempDir(), 0); GB.AddString(&fullpath, "/sqlite/", 0); GB.AddString(&fullpath, name, 0); diff --git a/main/share/gambas.h b/main/share/gambas.h index fb146c41b..e7411ebfc 100644 --- a/main/share/gambas.h +++ b/main/share/gambas.h @@ -825,7 +825,9 @@ typedef int (*LoadFile)(const char *, int, char **, int *); void (*ReleaseFile)(char *, int); int (*ExistFile)(char *); - char *(*GetTempDir)(void); + char *(*TempDir)(void); + char *(*TempFile)(const char *); + int (*CopyFile)(const char *, const char *); void (*Store)(GB_TYPE, GB_VALUE *, void *); void (*StoreString)(GB_STRING *, char **); diff --git a/main/share/gb_file_share.h b/main/share/gb_file_share.h index 75002abd6..e8cafd92a 100644 --- a/main/share/gb_file_share.h +++ b/main/share/gb_file_share.h @@ -116,7 +116,7 @@ void FILE_copy(const char *src, const char *dst); bool FILE_access(const char *path, int mode); void FILE_link(const char *src, const char *dst); -char *FILE_make_temp(int *len, char *pattern); +char *FILE_make_temp(int *len, const char *pattern); void FILE_recursive_dir(const char *dir, void (*found)(const char *), void (*afterfound)(const char *), int attr); diff --git a/main/share/gb_file_temp.h b/main/share/gb_file_temp.h index ba02c8245..ecb099abe 100644 --- a/main/share/gb_file_temp.h +++ b/main/share/gb_file_temp.h @@ -78,343 +78,343 @@ static bool _last_is_dir; #ifdef PROJECT_EXEC typedef - struct _path { - struct _path *next; - char *path; - } - FILE_PATH; + struct _path { + struct _path *next; + char *path; + } + FILE_PATH; static void push_path(void **list, const char *path) { - FILE_PATH *slot; + FILE_PATH *slot; - ALLOC(&slot, sizeof(FILE_PATH), "push_path"); - STRING_new(&slot->path, path, 0); + ALLOC(&slot, sizeof(FILE_PATH), "push_path"); + STRING_new(&slot->path, path, 0); - slot->next = *list; - *list = slot; + slot->next = *list; + *list = slot; - //printf("push_path: %s\n", path); + //printf("push_path: %s\n", path); } static char *pop_path(void **list) { - char *path; - FILE_PATH *slot; + char *path; + FILE_PATH *slot; - if (!*list) - return NULL; + if (!*list) + return NULL; - path = ((FILE_PATH *)*list)->path; - slot = *list; - *list = ((FILE_PATH *)*list)->next; - FREE(&slot, "pop_path"); + path = ((FILE_PATH *)*list)->path; + slot = *list; + *list = ((FILE_PATH *)*list)->next; + FREE(&slot, "pop_path"); - //printf("pop_path: %s\n", path); - return path; + //printf("pop_path: %s\n", path); + return path; } static void dir_exit(void) { - if (file_dir != NULL) - { - closedir(file_dir); - file_dir = NULL; - } + if (file_dir != NULL) + { + closedir(file_dir); + file_dir = NULL; + } - STRING_free(&file_pattern); - STRING_free(&file_path); + STRING_free(&file_pattern); + STRING_free(&file_path); } -char *FILE_make_temp(int *len, char *pattern) +char *FILE_make_temp(int *len, const char *pattern) { - static int count = 0; + static int count = 0; - if (len) - { - if (pattern) - *len = snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_PATTERN, (int)getuid(), (int)getpid(), pattern); - else - { - count++; - *len = snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_FILE, (int)getuid(), (int)getpid(), count); - } - } - else - snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_DIR, (int)getuid(), (int)getpid()); + if (len) + { + if (pattern) + *len = snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_PATTERN, (int)getuid(), (int)getpid(), pattern); + else + { + count++; + *len = snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_FILE, (int)getuid(), (int)getpid(), count); + } + } + else + snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_DIR, (int)getuid(), (int)getpid()); - return file_buffer; + return file_buffer; } static void remove_temp_file(const char *path) { - if (FILE_is_dir(path)) - { - //fprintf(stderr, "rmdir: %s\n", path); - rmdir(path); + if (FILE_is_dir(path)) + { + //fprintf(stderr, "rmdir: %s\n", path); + rmdir(path); } - else - { - //fprintf(stderr, "unlink: %s\n", path); - unlink(path); + else + { + //fprintf(stderr, "unlink: %s\n", path); + unlink(path); } } void FILE_remove_temp_file(void) { - FILE_recursive_dir(FILE_make_temp(NULL, NULL), NULL, remove_temp_file, 0); - rmdir(FILE_make_temp(NULL, NULL)); + FILE_recursive_dir(FILE_make_temp(NULL, NULL), NULL, remove_temp_file, 0); + rmdir(FILE_make_temp(NULL, NULL)); } void FILE_init(void) { FILE_remove_temp_file(); - - snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_PREFIX, (int)getuid()); - mkdir(file_buffer, S_IRWXU); - snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_DIR, (int)getuid(), (int)getpid()); - mkdir(file_buffer, S_IRWXU); + + snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_PREFIX, (int)getuid()); + mkdir(file_buffer, S_IRWXU); + snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_DIR, (int)getuid(), (int)getpid()); + mkdir(file_buffer, S_IRWXU); } void FILE_exit(void) { FILE_remove_temp_file(); - STRING_free(&file_rdir_path); - dir_exit(); + STRING_free(&file_rdir_path); + dir_exit(); } #endif #define stradd(d, s) \ ({ \ - char *_d = (d); \ - const char *_s = (s); \ - \ - for(;;) \ - { \ - if ((*_d = *_s) == 0) \ - break; \ - \ - _d++; \ - _s++; \ - } \ - \ - _d; \ + char *_d = (d); \ + const char *_s = (s); \ + \ + for(;;) \ + { \ + if ((*_d = *_s) == 0) \ + break; \ + \ + _d++; \ + _s++; \ + } \ + \ + _d; \ }) const char *FILE_cat(const char *path, ...) { - char *p; - va_list args; - int len; - bool end_slash = FALSE; - bool add_slash = FALSE; + char *p; + va_list args; + int len; + bool end_slash = FALSE; + bool add_slash = FALSE; - va_start(args, path); + va_start(args, path); - p = file_buffer; + p = file_buffer; - if (path != file_buffer) - *p = 0; + if (path != file_buffer) + *p = 0; - for(;;) - { - if (*path == '/' && p != file_buffer) - path++; + for(;;) + { + if (*path == '/' && p != file_buffer) + path++; - len = strlen(path); - if (add_slash) - len++; + len = strlen(path); + if (add_slash) + len++; - if (len > 0) - { - if ((p + len) > &file_buffer[MAX_PATH]) - return NULL; + if (len > 0) + { + if ((p + len) > &file_buffer[MAX_PATH]) + return NULL; - if (p != path) - { - if (add_slash) - p = stradd(p, "/"); + if (p != path) + { + if (add_slash) + p = stradd(p, "/"); - p = stradd(p, path); - } - else - p += len; + p = stradd(p, path); + } + else + p += len; - end_slash = (p[-1] == '/'); - } + end_slash = (p[-1] == '/'); + } - path = va_arg(args, char *); - if (path == NULL) - break; + path = va_arg(args, char *); + if (path == NULL) + break; - add_slash = ((!end_slash) && (*path != 0) && (*path != '/')); - } + add_slash = ((!end_slash) && (*path != 0) && (*path != '/')); + } - file_buffer_length = p - file_buffer; - return file_buffer; + file_buffer_length = p - file_buffer; + return file_buffer; } char *FILE_buffer(void) { - return file_buffer; + return file_buffer; } int FILE_buffer_length(void) { - if (file_buffer_length < 0) - file_buffer_length = strlen(file_buffer); + if (file_buffer_length < 0) + file_buffer_length = strlen(file_buffer); - return file_buffer_length; + return file_buffer_length; } const char *FILE_get_dir(const char *path) { - char *p; + char *p; - if (path == NULL || path[0] == 0) - return NULL; + if (path == NULL || path[0] == 0) + return NULL; - if (path[0] == '/' && path[1] == 0) - return "/"; + if (path[0] == '/' && path[1] == 0) + return "/"; - if (file_buffer != path) - strcpy(file_buffer, path); + if (file_buffer != path) + strcpy(file_buffer, path); - p = rindex(file_buffer, '/'); + p = rindex(file_buffer, '/'); - if (p == NULL) - *file_buffer = 0; - else - { - *p = 0; + if (p == NULL) + *file_buffer = 0; + else + { + *p = 0; - if (file_buffer[0] == 0 && path[0] == '/') - strcpy(file_buffer, "/"); - } + if (file_buffer[0] == 0 && path[0] == '/') + strcpy(file_buffer, "/"); + } - file_buffer_length = -1; - return file_buffer; + file_buffer_length = -1; + return file_buffer; } const char *FILE_get_name(const char *path) { - const char *p; + const char *p; - p = rindex(path, '/'); - if (p) - return &p[1]; - else - return path; + p = rindex(path, '/'); + if (p) + return &p[1]; + else + return path; } const char *FILE_get_ext(const char *path) { - const char *p; + const char *p; - p = rindex(path, '/'); - if (p) - path = &p[1]; + p = rindex(path, '/'); + if (p) + path = &p[1]; - p = rindex(path, '.'); - if (p == NULL) - return &path[strlen(path)]; - else - return p + 1; + p = rindex(path, '.'); + if (p == NULL) + return &path[strlen(path)]; + else + return p + 1; } const char *FILE_set_ext(const char *path, const char *ext) { - char *p; + char *p; - if (path != file_buffer) - { - strcpy(file_buffer, path); - path = file_buffer; - } + if (path != file_buffer) + { + strcpy(file_buffer, path); + path = file_buffer; + } - p = (char *)FILE_get_ext(path); + p = (char *)FILE_get_ext(path); - if (!ext) - { - if (p > file_buffer && p[-1] == '.') - p[-1] = 0; - else - *p = 0; - return path; - } + if (!ext) + { + if (p > file_buffer && p[-1] == '.') + p[-1] = 0; + else + *p = 0; + return path; + } - if (&p[strlen(ext)] >= &file_buffer[MAX_PATH]) - return path; + if (&p[strlen(ext)] >= &file_buffer[MAX_PATH]) + return path; - if (p == path || p[-1] != '.') - *p++ = '.'; + if (p == path || p[-1] != '.') + *p++ = '.'; - if (*ext == '.') - ext++; + if (*ext == '.') + ext++; - strcpy(p, ext); + strcpy(p, ext); - file_buffer_length = -1; - return path; + file_buffer_length = -1; + return path; } const char *FILE_get_basename(const char *path) { - char *p; + char *p; - path = FILE_get_name(path); + path = FILE_get_name(path); - if (file_buffer != path) - strcpy(file_buffer, path); + if (file_buffer != path) + strcpy(file_buffer, path); - p = rindex(file_buffer, '.'); - if (p) - *p = 0; + p = rindex(file_buffer, '.'); + if (p) + *p = 0; - file_buffer_length = -1; + file_buffer_length = -1; - return file_buffer; + return file_buffer; } bool FILE_is_dir(const char *path) { - struct stat buf; + struct stat buf; #ifdef PROJECT_EXEC - if (FILE_is_relative(path)) - { - /*if (!EXEC_arch) - { - chdir(PROJECT_path); - if (lstat(path, &buf) == 0) - goto __OK; - }*/ + if (FILE_is_relative(path)) + { + /*if (!EXEC_arch) + { + chdir(PROJECT_path); + if (lstat(path, &buf) == 0) + goto __OK; + }*/ - return ARCHIVE_is_dir(NULL, path); + return ARCHIVE_is_dir(NULL, path); } #endif - if (stat(path, &buf)) - return FALSE; + if (stat(path, &buf)) + return FALSE; /*#ifdef PROJECT_EXEC __OK: #endif*/ - return (S_ISDIR(buf.st_mode)); + return (S_ISDIR(buf.st_mode)); } @@ -422,200 +422,200 @@ __OK: bool FILE_exist_real(const char *path) { - struct stat buf; + struct stat buf; - chdir(PROJECT_path); - return (stat(path, &buf) == 0); + chdir(PROJECT_path); + return (stat(path, &buf) == 0); } void FILE_stat(const char *path, FILE_STAT *info, bool follow) { - struct stat buf; - int ret; + struct stat buf; + int ret; - if (FILE_is_relative(path)) - { - /*if (!EXEC_arch) - { - chdir(PROJECT_path); - if (lstat(path, &buf) == 0) - goto _OK; - }*/ + if (FILE_is_relative(path)) + { + /*if (!EXEC_arch) + { + chdir(PROJECT_path); + if (lstat(path, &buf) == 0) + goto _OK; + }*/ - ARCHIVE_stat(NULL, path, info); - return; - } + ARCHIVE_stat(NULL, path, info); + return; + } if (follow) ret = stat(path, &buf); else ret = lstat(path, &buf); - if (ret) - THROW_SYSTEM(errno, path); + if (ret) + THROW_SYSTEM(errno, path); //_OK: - if (S_ISREG(buf.st_mode)) - info->type = GB_STAT_FILE; - else if (S_ISDIR(buf.st_mode)) - info->type = GB_STAT_DIRECTORY; - else if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) - info->type = GB_STAT_DEVICE; - else if (S_ISFIFO(buf.st_mode)) - info->type = GB_STAT_PIPE; - else if (S_ISSOCK(buf.st_mode)) - info->type = GB_STAT_SOCKET; - else if (S_ISLNK(buf.st_mode)) - info->type = GB_STAT_LINK; + if (S_ISREG(buf.st_mode)) + info->type = GB_STAT_FILE; + else if (S_ISDIR(buf.st_mode)) + info->type = GB_STAT_DIRECTORY; + else if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) + info->type = GB_STAT_DEVICE; + else if (S_ISFIFO(buf.st_mode)) + info->type = GB_STAT_PIPE; + else if (S_ISSOCK(buf.st_mode)) + info->type = GB_STAT_SOCKET; + else if (S_ISLNK(buf.st_mode)) + info->type = GB_STAT_LINK; - info->mode = buf.st_mode & 07777; - info->size = buf.st_size; - info->atime = (int)buf.st_atime; - info->mtime = (int)buf.st_mtime; - info->ctime = (int)buf.st_ctime; - info->hidden = (*FILE_get_name(path) == '.'); - info->uid = buf.st_uid; - info->gid = buf.st_gid; + info->mode = buf.st_mode & 07777; + info->size = buf.st_size; + info->atime = (int)buf.st_atime; + info->mtime = (int)buf.st_mtime; + info->ctime = (int)buf.st_ctime; + info->hidden = (*FILE_get_name(path) == '.'); + info->uid = buf.st_uid; + info->gid = buf.st_gid; } void FILE_dir_first(const char *path, const char *pattern, int attr) { - dir_exit(); + dir_exit(); - if (!path || *path == 0) - path = "."; + if (!path || *path == 0) + path = "."; - if (attr == (GB_STAT_FILE | GB_STAT_DIRECTORY)) - attr = 0; - file_attr = attr; + if (attr == (GB_STAT_FILE | GB_STAT_DIRECTORY)) + attr = 0; + file_attr = attr; - if (FILE_is_relative(path)) - { - /*if (!EXEC_arch) - { - chdir(PROJECT_path); - if (lstat(path, &buf) == 0) - goto _OK; - }*/ + if (FILE_is_relative(path)) + { + /*if (!EXEC_arch) + { + chdir(PROJECT_path); + if (lstat(path, &buf) == 0) + goto _OK; + }*/ - file_dir_arch = TRUE; - ARCHIVE_dir_first(NULL, path, pattern, attr); - return; - } + file_dir_arch = TRUE; + ARCHIVE_dir_first(NULL, path, pattern, attr); + return; + } - file_dir_arch = FALSE; - file_dir = opendir(path); - if (file_dir == NULL) - THROW_SYSTEM(errno, path); + file_dir_arch = FALSE; + file_dir = opendir(path); + if (file_dir == NULL) + THROW_SYSTEM(errno, path); - STRING_new(&file_pattern, pattern, 0); - STRING_new(&file_path, path, 0); + STRING_new(&file_pattern, pattern, 0); + STRING_new(&file_path, path, 0); } bool FILE_dir_next(char **path, int *len) { - struct dirent *entry; - int len_entry; - bool ret; + struct dirent *entry; + int len_entry; + bool ret; #ifdef _DIRENT_HAVE_D_TYPE #else - struct stat info; - char *p = file_buffer; + struct stat info; + char *p = file_buffer; #endif - if (file_dir_arch) - { - ret = ARCHIVE_dir_next(path, len, file_attr); - if (ret) - file_dir_arch = FALSE; - return ret; - } + if (file_dir_arch) + { + ret = ARCHIVE_dir_next(path, len, file_attr); + if (ret) + file_dir_arch = FALSE; + return ret; + } - if (file_dir == NULL) - return TRUE; + if (file_dir == NULL) + return TRUE; #ifdef _DIRENT_HAVE_D_TYPE #else - if (file_attr) - { - strcpy(p, file_path); - p += strlen(file_path); - if (p[-1] != '/' && (file_buffer[1] || file_buffer[0] != '/')) - *p++ = '/'; - } + if (file_attr) + { + strcpy(p, file_path); + p += strlen(file_path); + if (p[-1] != '/' && (file_buffer[1] || file_buffer[0] != '/')) + *p++ = '/'; + } #endif - for(;;) - { - entry = readdir(file_dir); - if (entry == NULL) - { - dir_exit(); - return TRUE; - } + for(;;) + { + entry = readdir(file_dir); + if (entry == NULL) + { + dir_exit(); + return TRUE; + } - if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) - continue; + if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) + continue; - if (file_attr) - { + if (file_attr) + { #ifdef _DIRENT_HAVE_D_TYPE - if ((file_attr == GB_STAT_DIRECTORY) ^ (entry->d_type == DT_DIR)) - continue; + if ((file_attr == GB_STAT_DIRECTORY) ^ (entry->d_type == DT_DIR)) + continue; #else - strcpy(p, entry->d_name); - stat(file_buffer, &info); - if ((file_attr == GB_STAT_DIRECTORY) ^ (S_ISDIR(info.st_mode) != 0)) - continue; + strcpy(p, entry->d_name); + stat(file_buffer, &info); + if ((file_attr == GB_STAT_DIRECTORY) ^ (S_ISDIR(info.st_mode) != 0)) + continue; #endif - } + } - len_entry = strlen(entry->d_name); + len_entry = strlen(entry->d_name); - if (file_pattern == NULL) - break; + if (file_pattern == NULL) + break; - if (REGEXP_match(file_pattern, STRING_length(file_pattern), entry->d_name, len_entry)) - break; - } + if (REGEXP_match(file_pattern, STRING_length(file_pattern), entry->d_name, len_entry)) + break; + } - *path = entry->d_name; - *len = len_entry; + *path = entry->d_name; + *len = len_entry; #ifdef _DIRENT_HAVE_D_TYPE _last_is_dir = entry->d_type == DT_DIR; #endif - return FALSE; + return FALSE; } //#undef _DIRENT_HAVE_D_TYPE void FILE_recursive_dir(const char *dir, void (*found)(const char *), void (*afterfound)(const char *), int attr) { - void *list = NULL; + void *list = NULL; void *dir_list = NULL; - char *file; - int len; - char *path; - //struct stat buf; + char *file; + int len; + char *path; + //struct stat buf; #ifdef _DIRENT_HAVE_D_TYPE #else - FILE_STAT info; + FILE_STAT info; #endif - char *temp; + char *temp; bool is_dir; - if (!FILE_is_dir(dir)) - return; + if (!FILE_is_dir(dir)) + return; - if (!dir || *dir == 0) - dir = "."; + if (!dir || *dir == 0) + dir = "."; - STRING_free(&file_rdir_path); - STRING_new(&file_rdir_path, dir, 0); + STRING_free(&file_rdir_path); + STRING_new(&file_rdir_path, dir, 0); FILE_dir_first(dir, NULL, attr); while (!FILE_dir_next(&file, &len)) @@ -635,9 +635,9 @@ void FILE_recursive_dir(const char *dir, void (*found)(const char *), void (*aft push_path(&list, path); } - while (dir_list) - { - path = pop_path(&dir_list); + while (dir_list) + { + path = pop_path(&dir_list); //fprintf(stderr, "%s\n", path); TRY @@ -652,12 +652,12 @@ void FILE_recursive_dir(const char *dir, void (*found)(const char *), void (*aft } END_TRY - STRING_free((char **)&path); - } + STRING_free((char **)&path); + } - while (list) - { - path = pop_path(&list); + while (list) + { + path = pop_path(&list); //fprintf(stderr, "%s\n", path); TRY @@ -671,148 +671,148 @@ void FILE_recursive_dir(const char *dir, void (*found)(const char *), void (*aft } END_TRY - STRING_free((char **)&path); - } + STRING_free((char **)&path); + } } void FILE_unlink(const char *path) { - if (FILE_is_relative(path)) - THROW(E_ACCESS); + if (FILE_is_relative(path)) + THROW(E_ACCESS); - if (unlink(path) != 0) - THROW_SYSTEM(errno, path); + if (unlink(path) != 0) + THROW_SYSTEM(errno, path); } void FILE_rmdir(const char *path) { - if (FILE_is_relative(path)) - THROW(E_ACCESS); + if (FILE_is_relative(path)) + THROW(E_ACCESS); - if (rmdir(path) != 0) - THROW_SYSTEM(errno, path); + if (rmdir(path) != 0) + THROW_SYSTEM(errno, path); } void FILE_mkdir(const char *path) { - if (FILE_is_relative(path)) - THROW(E_ACCESS); + if (FILE_is_relative(path)) + THROW(E_ACCESS); - if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) - THROW_SYSTEM(errno, path); + if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) + THROW_SYSTEM(errno, path); } void FILE_make_path_dir(const char *path) { - int i; - char c; + int i; + char c; - if (FILE_is_relative(path)) - return; + if (FILE_is_relative(path)) + return; - if (path != file_buffer) - strcpy(file_buffer, path); + if (path != file_buffer) + strcpy(file_buffer, path); - for (i = 1;; i++) - { - c = file_buffer[i]; - if (c == 0) - break; - if (c == '/') - { - file_buffer[i] = 0; - mkdir(file_buffer, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); - file_buffer[i] = c; - } - c++; - } + for (i = 1;; i++) + { + c = file_buffer[i]; + if (c == 0) + break; + if (c == '/') + { + file_buffer[i] = 0; + mkdir(file_buffer, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); + file_buffer[i] = c; + } + c++; + } } void FILE_rename(const char *src, const char *dst) { - if (FILE_is_relative(src) || FILE_is_relative(dst)) - THROW(E_ACCESS); + if (FILE_is_relative(src) || FILE_is_relative(dst)) + THROW(E_ACCESS); - if (FILE_exist(dst)) - THROW(E_EXIST, dst); + if (FILE_exist(dst)) + THROW(E_EXIST, dst); - if (rename(src, dst) != 0) - THROW_SYSTEM(errno, dst); + if (rename(src, dst) != 0) + THROW_SYSTEM(errno, dst); } void FILE_copy(const char *src, const char *dst) { - STREAM stream_src; - STREAM stream_dst; - int64_t len; - int64_t n; - char *buf = NULL; + STREAM stream_src; + STREAM stream_dst; + int64_t len; + int64_t n; + char *buf = NULL; - CLEAR(&stream_src); - CLEAR(&stream_dst); + CLEAR(&stream_src); + CLEAR(&stream_dst); - if (FILE_exist(dst)) - THROW(E_EXIST, dst); + if (FILE_exist(dst)) + THROW(E_EXIST, dst); - ALLOC(&buf, MAX_IO, "FILE_copy"); + ALLOC(&buf, MAX_IO, "FILE_copy"); - TRY - { - STREAM_open(&stream_src, src, ST_READ); - STREAM_open(&stream_dst, dst, ST_CREATE); + TRY + { + STREAM_open(&stream_src, src, ST_READ); + STREAM_open(&stream_dst, dst, ST_CREATE); - STREAM_lof(&stream_src, &len); + STREAM_lof(&stream_src, &len); - while (len) - { - n = len > MAX_IO ? MAX_IO : len; - STREAM_read(&stream_src, buf, n); - STREAM_write(&stream_dst, buf, n); - len -= n; - } + while (len) + { + n = len > MAX_IO ? MAX_IO : len; + STREAM_read(&stream_src, buf, n); + STREAM_write(&stream_dst, buf, n); + len -= n; + } - STREAM_close(&stream_src); - STREAM_close(&stream_dst); + STREAM_close(&stream_src); + STREAM_close(&stream_dst); - FREE(&buf, "FILE_copy"); - } - CATCH - { - if (stream_src.type) - STREAM_close(&stream_src); - if (stream_dst.type) - STREAM_close(&stream_dst); - FREE(&buf, "FILE_copy"); + FREE(&buf, "FILE_copy"); + } + CATCH + { + if (stream_src.type) + STREAM_close(&stream_src); + if (stream_dst.type) + STREAM_close(&stream_dst); + FREE(&buf, "FILE_copy"); - PROPAGATE(); - } - END_TRY + PROPAGATE(); + } + END_TRY } bool FILE_access(const char *path, int mode) { - if (FILE_is_relative(path)) - { - if (mode & (W_OK | X_OK)) - return FALSE; + if (FILE_is_relative(path)) + { + if (mode & (W_OK | X_OK)) + return FALSE; - /*if (!EXEC_arch) - { - chdir(PROJECT_path); - if (access(path, mode) == 0) - return TRUE; - }*/ + /*if (!EXEC_arch) + { + chdir(PROJECT_path); + if (access(path, mode) == 0) + return TRUE; + }*/ - return ARCHIVE_exist(NULL, path); - } + return ARCHIVE_exist(NULL, path); + } - return (access(path, mode) == 0); + return (access(path, mode) == 0); } @@ -820,107 +820,107 @@ bool FILE_exist(const char *path) { struct stat buf; - if (FILE_is_relative(path)) - { - /*if (!EXEC_arch) - { - chdir(PROJECT_path); - if (lstat(path, &buf) == 0) - return TRUE; - }*/ + if (FILE_is_relative(path)) + { + /*if (!EXEC_arch) + { + chdir(PROJECT_path); + if (lstat(path, &buf) == 0) + return TRUE; + }*/ - return ARCHIVE_exist(NULL, path); - } + return ARCHIVE_exist(NULL, path); + } - return lstat(path, &buf) == 0; + return lstat(path, &buf) == 0; } void FILE_link(const char *src, const char *dst) { - /* src can be relative */ - if (FILE_is_relative(dst)) - THROW(E_ACCESS); + /* src can be relative */ + if (FILE_is_relative(dst)) + THROW(E_ACCESS); - if (FILE_exist(dst)) - THROW(E_EXIST, dst); + if (FILE_exist(dst)) + THROW(E_EXIST, dst); - if (symlink(src, dst) != 0) - THROW_SYSTEM(errno, dst); + if (symlink(src, dst) != 0) + THROW_SYSTEM(errno, dst); } int64_t FILE_free(const char *path) { - struct statfs info; + struct statfs info; - if (FILE_is_relative(path)) - return 0; + if (FILE_is_relative(path)) + return 0; - statfs(path, &info); - return (int64_t)(getuid() == 0 ? info.f_bfree : info.f_bavail) * info.f_bsize; + statfs(path, &info); + return (int64_t)(getuid() == 0 ? info.f_bfree : info.f_bavail) * info.f_bsize; } #else bool FILE_exist(const char *path) { - return (access(path, F_OK) == 0); + return (access(path, F_OK) == 0); } time_t FILE_get_time(const char *path) { - struct stat info; + struct stat info; - if (stat(path, &info) == 0) - return info.st_mtime; - else - return (time_t)-1L; + if (stat(path, &info) == 0) + return info.st_mtime; + else + return (time_t)-1L; } #endif const char *FILE_getcwd(const char *subdir) { - if (getcwd(file_buffer, PATH_MAX) == NULL) - return NULL; + if (getcwd(file_buffer, PATH_MAX) == NULL) + return NULL; - file_buffer_length = strlen(file_buffer); + file_buffer_length = strlen(file_buffer); - if (subdir != NULL) - return FILE_cat(file_buffer, subdir, NULL); - else - return file_buffer; + if (subdir != NULL) + return FILE_cat(file_buffer, subdir, NULL); + else + return file_buffer; } const char *FILE_readlink(const char *link) { - int len = readlink(link, file_buffer, MAX_PATH); + int len = readlink(link, file_buffer, MAX_PATH); - if (len < 0) - return NULL; + if (len < 0) + return NULL; - file_buffer[len] = 0; - file_buffer_length = len; - return file_buffer; + file_buffer[len] = 0; + file_buffer_length = len; + return file_buffer; } const char *FILE_find_gambas(void) { - const char *path; + const char *path; - if (FILE_exist(GAMBAS_LINK_PATH)) - { - path = FILE_readlink(GAMBAS_LINK_PATH); - if (!path) - path = GAMBAS_LINK_PATH; - } - else - { - path = GAMBAS_PATH "/gbx" GAMBAS_VERSION_STRING; - } + if (FILE_exist(GAMBAS_LINK_PATH)) + { + path = FILE_readlink(GAMBAS_LINK_PATH); + if (!path) + path = GAMBAS_LINK_PATH; + } + else + { + path = GAMBAS_PATH "/gbx" GAMBAS_VERSION_STRING; + } - return path; + return path; }