|
NAME
SYNOPSIS#include <agar/core.h> DESCRIPTION
FILESint
The typedef struct ag_file_info {
enum ag_file_info_type type;
int perms;
int flags;
} AG_FileInfo;
The type field can take on the values:
The perms field can contain the following permission flags, assumed to be true under the effective privileges of the current process:
The flags field allows the following values:
DIRECTORIESint
The
The
typedef struct ag_dir {
char **ents; /* Filenames */
int nents;
} AG_Dir;
The ents array contains the filenames for all directory entries. Regardless of the filesystem's character encoding, ents is in UTF-8 encoding.
UTILITY ROUTINESconst char *
Agar maintains a general-purpose
table mapping file extensions to Agar object classes. The
typedef struct ag_file_ext_mapping {
const char *ext;
const char *descr;
void *cls;
int editDirect;
} AG_FileExtMapping;
The ext member should be set to
the file extension, including the leading dot. descr
is a short description string. The cls pointer should
be set to to an Agar object class (see
AG_ObjectClass(3))
with a EXAMPLESThe following code lists the contents of a directory: AG_Dir *dir;
int i;
dir = AG_OpenDir("example-directory");
if (dir == NULL) {
AG_FatalError(NULL);
}
for (i = 0; i < dir->nents; i++) {
AG_Verbose("%s\n", dir->ents[i]);
}
AG_CloseDir(dir);
The following code displays information about a file: AG_FileInfo info;
if (AG_GetFileInfo("file.txt", &info) != 0) {
AG_FatalError(NULL);
}
AG_Verbose("File type: %d\n", info.type);
AG_Verbose("Permissions: %x\n", info.perms);
AG_Verbose("Flags: %x\n", info.flags);
The following code checks if a given file exists in the system temporary directory and if so, deletes it: char path[AG_PATHNAME_MAX];
if (AG_GetSystemTempDir(path, sizeof(path)) != 0) {
AG_FatalError(NULL);
}
AG_Strlcat(path, "file-to-delete.txt", sizeof(path));
if (AG_FileExists(path) == 1) {
if (AG_FileDelete(path) == -1)
AG_Verbose("Delete failed (%s)\n",
AG_GetError());
}
The following code creates a new directory, changes the working directory to it, prints out its complete path name and finally deletes it: if (AG_MkDir("tempdir") != 0) {
AG_FatalError(NULL);
}
if (AG_ChDir("tempdir") == 0) {
char dir[AG_PATHNAME_MAX];
if (AG_GetCWD(dir, sizeof(dir)) == 0) {
AG_Verbose("Created %s (%s)\n",
AG_ShortFilename(dir), dir);
}
AG_ChDir("..");
}
if (AG_RmDir("tempdir") != 0) {
AG_Verbose("Delete failed (%s)\n",
AG_GetError());
}
The following code maps the ".foo", ".bar" and ".baz" extensions to the AG_ObjectClass(3) at fooClass, barClass and bazClass, respectively. const AG_FileExtMapping myExts[] = {
{ ".foo", N_("Foo file"), &fooClass, 1 },
{ ".bar", N_("Bar file"), &barClass, 1 },
{ ".baz", N_("Baz file"), &bazClass, 1 }
};
const Uint myExtsCount = sizeof(myExts) /
sizeof(myExts[0]);
AG_RegisterFileExtMappings(myExts, myExtsCount);
SEE ALSOHISTORYThe
|