GrafX2 2.9.3227
The ultimate 256-color painting program
io.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <dirent.h>
#include "struct.h"
#include "io.h"
#include "realpath.h"
#include "unicode.h"
#include "global.h"
#include "gfx2log.h"
#include "gfx2mem.h"
#include "fileseltools.h"
+ Include dependency graph for io.c:

Macros

#define GFX2_LOCK_FILENAME   "gfx2.lck"
 

Functions

int Read_byte (FILE *file, byte *dest)
 Reads a single byte from an open file. Returns true if OK, false if a file i/o error occurred. More...
 
int Write_byte (FILE *file, byte b)
 Writes a single byte to an open file. Returns true if OK, false if a file i/o error occurred. More...
 
int Read_bytes (FILE *file, void *dest, size_t size)
 Reads several bytes from an open file. Returns true if OK, false if a file i/o error occurred. More...
 
int Read_byte_line (FILE *file, char *line, size_t size)
 
int Write_bytes (FILE *file, const void *src, size_t size)
 Writes several bytes to an open file. Returns true if OK, false if a file i/o error occurred. More...
 
int Read_word_le (FILE *file, word *dest)
 Reads a 16-bit Low-Endian word from an open file. Returns true if OK, false if a file i/o error occurred. More...
 
int Write_word_le (FILE *file, word w)
 Writes a 16-bit Low-Endian word to an open file. Returns true if OK, false if a file i/o error occurred. More...
 
int Read_word_be (FILE *file, word *dest)
 Reads a 16-bit Big-Endian word from an open file. Returns true if OK, false if a file i/o error occurred. More...
 
int Write_word_be (FILE *file, word w)
 Writes a 16-bit Big-Endian word to an open file. Returns true if OK, false if a file i/o error occurred. More...
 
int Read_dword_le (FILE *file, dword *dest)
 Reads a 32-bit Low-Endian dword from an open file. Returns true if OK, false if a file i/o error occurred. More...
 
int Write_dword_le (FILE *file, dword dw)
 Writes a 32-bit Low-Endian dword to an open file. Returns true if OK, false if a file i/o error occurred. More...
 
int Read_dword_be (FILE *file, dword *dest)
 Reads a 32-bit Big-Endian dword from an open file. Returns true if OK, false if a file i/o error occurred. More...
 
int Write_dword_be (FILE *file, dword dw)
 Writes a 32-bit Big-Endian dword to an open file. Returns true if OK, false if a file i/o error occurred. More...
 
char * Find_last_separator (const char *str)
 Finds the rightmost path separator in a full filename. Used to separate directory from file. More...
 
wordFind_last_separator_unicode (const word *str)
 Finds the rightmost path separator in a full filename in unicode. Used to separate directory from file. More...
 
char * Filepath_append_to_dir (const char *dir, const char *filename)
 Construct full file path. More...
 
char * Extract_filename (const char *source)
 Extracts the filename part from a full file name. More...
 
char * Extract_path (const char *source)
 Extracts the directory from a full file name. More...
 
void Append_path (char *path, const char *filename, char *reverse_path)
 Appends a file or directory name to an existing directory name. More...
 
int Position_last_dot (const char *fname)
 finds the rightmost '.' character in fullname. Used to find file extension. returns -1 if not found More...
 
int Position_last_dot_unicode (const word *fname)
 finds the rightmost '.' character in fullname. Used to find file extension. returns -1 if not found More...
 
int File_exists (const char *fname)
 Returns true if a file passed as a parameter exists in the current directory. More...
 
int Directory_exists (const char *directory)
 Returns true if a directory passed as a parameter exists in the current directory. More...
 
int Directory_create (const char *directory)
 Creates a directory. More...
 
int File_is_hidden (const char *fname, const char *full_name)
 Check if a file or directory is hidden. More...
 
unsigned long File_length (const char *fname)
 Size of a file, in bytes. Returns 0 in case of error. More...
 
unsigned long File_length_file (FILE *file)
 Size of an open file, in bytes. More...
 
void For_each_file (const char *directory_name, void Callback(const char *, const char *))
 
void For_each_directory_entry (const char *directory_name, void *pdata, T_File_dir_cb Callback)
 Scans a directory, calls Callback for each file or directory in it,. More...
 
wordGet_Unicode_Filename (word *filename_unicode, const char *filename, const char *directory)
 Convert a file name to unicode characters. More...
 
byte Create_lock_file (const char *file_directory)
 Creates a lock file, to check if an other instance of Grafx2 is running. More...
 
void Release_lock_file (const char *file_directory)
 Release a lock file created by Create_lock_file. More...
 
char * Get_current_directory (char *buf, word **unicode, size_t size)
 Return the current directory, equivalent to getcwd() More...
 
int Change_directory (const char *path)
 Change current directory. return 0 for success, -1 in case of error. More...
 
int Remove_path (const char *path)
 Remove the file. More...
 
int Remove_directory (const char *path)
 Remove the directory. More...
 
char * Calculate_relative_path (const char *ref_path, const char *path)
 Calculate relative path. More...
 

Variables

int Lock_file_handle = -1
 Lock file used to prevent several instances of grafx2 from harming each others' backups. More...
 

Macro Definition Documentation

◆ GFX2_LOCK_FILENAME

#define GFX2_LOCK_FILENAME   "gfx2.lck"

Function Documentation

◆ Append_path()

void Append_path ( char *  path,
const char *  filename,
char *  reverse_path 
)

Appends a file or directory name to an existing directory name.

As a special case, when the new item is equal to PARENT_DIR, this will remove the rightmost directory name. reverse_path is optional, if it's non-null, the function will write there :

  • if filename is ".." : The name of eliminated directory/file
  • else: ".."

References Find_last_separator(), PARENT_DIR, and PATH_SEPARATOR.

◆ For_each_file()

void For_each_file ( const char *  directory_name,
void   Callbackconst char *, const char * 
)

◆ Release_lock_file()

void Release_lock_file ( const char *  file_directory)

◆ Get_current_directory()

char * Get_current_directory ( char *  buf,
word **  unicode,
size_t  size 
)

Return the current directory, equivalent to getcwd()

Parameters
bufdestination buffer. can be NULL
unicodedestination pointer for the unicode version of the path
sizedestination buffer size, ignored if buf is NULL
Returns
NULL for error, buf or a malloc'ed buffer

References GFX2_ERROR, GFX2_Log(), GFX2_malloc, MAX_PATH_CHARACTERS, NULL, and PATH_SEPARATOR.

Referenced by Button_Load_or_Save(), Get_program_directory(), Init_program(), and Read_list_of_files().

◆ Change_directory()

int Change_directory ( const char *  path)

Change current directory. return 0 for success, -1 in case of error.

References GFX2_DEBUG, and GFX2_Log().

Referenced by Button_Load_or_Save(), Delete_safety_backups(), and Program_shutdown().

◆ Remove_path()

◆ Remove_directory()

int Remove_directory ( const char *  path)

Remove the directory.

Referenced by Button_Load_or_Save().

◆ Calculate_relative_path()

char * Calculate_relative_path ( const char *  ref_path,
const char *  path 
)

Calculate relative path.

References GFX2_malloc, NULL, PATH_SEPARATOR, and Realpath().

Referenced by Button_Load_or_Save(), and Test_Calculate_relative_path().

Variable Documentation

◆ Lock_file_handle

int Lock_file_handle = -1

Lock file used to prevent several instances of grafx2 from harming each others' backups.

Referenced by Create_lock_file(), and Release_lock_file().