+ if (num_bytes_read < 1)
+ file->end_of_file = TRUE;
+
+ return (file->end_of_file ? EOF : (int)c);
+ }
+#endif
+
+ return fgetc(file->file);
+}
+
+char *getStringFromFile(File *file, char *line, int size)
+{
+#if defined(PLATFORM_ANDROID)
+ if (file->file_is_asset)
+ {
+ if (file->end_of_file)
+ return NULL;
+
+ char *line_ptr = line;
+ int num_bytes_read = 0;
+
+ while (num_bytes_read < size - 1 &&
+ SDL_RWread(file->asset_file, line_ptr, 1, 1) == 1 &&
+ *line_ptr++ != '\n')
+ num_bytes_read++;
+
+ *line_ptr = '\0';
+
+ if (strlen(line) == 0)
+ {
+ file->end_of_file = TRUE;
+
+ return NULL;
+ }
+
+ return line;
+ }
+#endif
+
+ return fgets(line, size, file->file);
+}
+
+
+/* ------------------------------------------------------------------------- */
+/* functions for directory handling */
+/* ------------------------------------------------------------------------- */
+
+Directory *openDirectory(char *dir_name)
+{
+ Directory *dir = checked_calloc(sizeof(Directory));
+
+ dir->dir = opendir(dir_name);
+
+ if (dir->dir != NULL)
+ {
+ dir->filename = getStringCopy(dir_name);
+
+ return dir;
+ }
+
+#if defined(PLATFORM_ANDROID)
+ char *asset_toc_filename = getPath2(dir_name, ASSET_TOC_BASENAME);
+
+ dir->asset_toc_file = SDL_RWFromFile(asset_toc_filename, MODE_READ);
+
+ checked_free(asset_toc_filename);
+
+ if (dir->asset_toc_file != NULL)
+ {
+ dir->directory_is_asset = TRUE;
+ dir->filename = getStringCopy(dir_name);
+
+ return dir;
+ }
+#endif
+
+ checked_free(dir);
+
+ return NULL;
+}
+
+int closeDirectory(Directory *dir)
+{
+ if (dir == NULL)
+ return -1;
+
+ int result = 0;
+
+#if defined(PLATFORM_ANDROID)
+ if (dir->asset_toc_file)
+ result = SDL_RWclose(dir->asset_toc_file);
+#endif
+
+ if (dir->dir)
+ result = closedir(dir->dir);
+
+ if (dir->dir_entry)
+ freeDirectoryEntry(dir->dir_entry);
+
+ checked_free(dir->filename);
+ checked_free(dir);
+
+ return result;
+}
+
+DirectoryEntry *readDirectory(Directory *dir)
+{
+ if (dir->dir_entry)
+ freeDirectoryEntry(dir->dir_entry);
+
+ dir->dir_entry = NULL;
+
+#if defined(PLATFORM_ANDROID)
+ if (dir->directory_is_asset)
+ {
+ char line[MAX_LINE_LEN];
+ char *line_ptr = line;
+ int num_bytes_read = 0;
+
+ while (num_bytes_read < MAX_LINE_LEN - 1 &&
+ SDL_RWread(dir->asset_toc_file, line_ptr, 1, 1) == 1 &&
+ *line_ptr != '\n')
+ {
+ line_ptr++;
+ num_bytes_read++;
+ }
+
+ *line_ptr = '\0';
+
+ if (strlen(line) == 0)
+ return NULL;
+
+ dir->dir_entry = checked_calloc(sizeof(DirectoryEntry));
+
+ dir->dir_entry->is_directory = FALSE;
+ if (line[strlen(line) - 1] == '/')
+ {
+ dir->dir_entry->is_directory = TRUE;
+
+ line[strlen(line) - 1] = '\0';
+ }
+
+ dir->dir_entry->basename = getStringCopy(line);
+ dir->dir_entry->filename = getPath2(dir->filename, line);
+
+ return dir->dir_entry;
+ }
+#endif
+
+ struct dirent *dir_entry = readdir(dir->dir);
+
+ if (dir_entry == NULL)
+ return NULL;
+
+ dir->dir_entry = checked_calloc(sizeof(DirectoryEntry));
+
+ dir->dir_entry->basename = getStringCopy(dir_entry->d_name);
+ dir->dir_entry->filename = getPath2(dir->filename, dir_entry->d_name);
+
+ struct stat file_status;
+
+ dir->dir_entry->is_directory =
+ (stat(dir->dir_entry->filename, &file_status) == 0 &&
+ (file_status.st_mode & S_IFMT) == S_IFDIR);
+
+#if 0
+ Error(ERR_INFO, "::: '%s' is directory: %d",
+ dir->dir_entry->basename,
+ dir->dir_entry->is_directory);
+#endif
+
+ return dir->dir_entry;
+}
+
+void freeDirectoryEntry(DirectoryEntry *dir_entry)
+{
+ if (dir_entry == NULL)
+ return;
+
+ checked_free(dir_entry->basename);
+ checked_free(dir_entry->filename);
+ checked_free(dir_entry);
+}
+
+
+/* ------------------------------------------------------------------------- */
+/* functions for checking files and filenames */
+/* ------------------------------------------------------------------------- */
+
+boolean directoryExists(char *dir_name)
+{
+ if (dir_name == NULL)
+ return FALSE;
+
+ boolean success = (access(dir_name, F_OK) == 0);
+
+#if defined(PLATFORM_ANDROID)
+ if (!success)
+ {
+ // this might be an asset directory; check by trying to open toc file
+ char *asset_toc_filename = getPath2(dir_name, ASSET_TOC_BASENAME);
+ SDL_RWops *file = SDL_RWFromFile(asset_toc_filename, MODE_READ);
+
+ checked_free(asset_toc_filename);
+
+ success = (file != NULL);
+
+ if (success)
+ SDL_RWclose(file);
+ }
+#endif
+
+ return success;
+}
+
+boolean fileExists(char *filename)
+{
+ if (filename == NULL)
+ return FALSE;
+
+ boolean success = (access(filename, F_OK) == 0);
+
+#if defined(PLATFORM_ANDROID)
+ if (!success)
+ {
+ // this might be an asset file; check by trying to open it
+ SDL_RWops *file = SDL_RWFromFile(filename, MODE_READ);
+
+ success = (file != NULL);
+
+ if (success)
+ SDL_RWclose(file);
+ }
+#endif
+
+ return success;
+}
+
+boolean fileHasPrefix(char *basename, char *prefix)
+{
+ static char *basename_lower = NULL;
+ int basename_length, prefix_length;
+
+ checked_free(basename_lower);
+
+ if (basename == NULL || prefix == NULL)
+ return FALSE;
+
+ basename_lower = getStringToLower(basename);
+ basename_length = strlen(basename_lower);
+ prefix_length = strlen(prefix);
+
+ if (basename_length > prefix_length + 1 &&
+ basename_lower[prefix_length] == '.' &&
+ strncmp(basename_lower, prefix, prefix_length) == 0)
+ return TRUE;
+
+ return FALSE;
+}
+
+boolean fileHasSuffix(char *basename, char *suffix)
+{
+ static char *basename_lower = NULL;
+ int basename_length, suffix_length;
+
+ checked_free(basename_lower);
+
+ if (basename == NULL || suffix == NULL)
+ return FALSE;
+
+ basename_lower = getStringToLower(basename);
+ basename_length = strlen(basename_lower);
+ suffix_length = strlen(suffix);
+
+ if (basename_length > suffix_length + 1 &&
+ basename_lower[basename_length - suffix_length - 1] == '.' &&
+ strEqual(&basename_lower[basename_length - suffix_length], suffix))
+ return TRUE;
+
+ return FALSE;
+}
+
+#if defined(TARGET_SDL)
+static boolean FileCouldBeArtwork(char *basename)
+{
+ return (!strEqual(basename, ".") &&
+ !strEqual(basename, "..") &&
+ !fileHasSuffix(basename, "txt") &&
+ !fileHasSuffix(basename, "conf"));
+}
+#endif
+
+boolean FileIsGraphic(char *filename)
+{
+ char *basename = getBaseNamePtr(filename);
+
+#if defined(TARGET_SDL)
+ return FileCouldBeArtwork(basename);
+#else
+ return fileHasSuffix(basename, "pcx");
+#endif
+}
+
+boolean FileIsSound(char *filename)
+{
+ char *basename = getBaseNamePtr(filename);
+
+#if defined(TARGET_SDL)
+ return FileCouldBeArtwork(basename);
+#else
+ return fileHasSuffix(basename, "wav");
+#endif
+}
+
+boolean FileIsMusic(char *filename)
+{
+ char *basename = getBaseNamePtr(filename);
+
+#if defined(TARGET_SDL)
+ return FileCouldBeArtwork(basename);
+#else
+ if (FileIsSound(basename))
+ return TRUE;
+
+#if 0
+#if defined(TARGET_SDL)
+ if ((fileHasPrefix(basename, "mod") && !fileHasSuffix(basename, "txt")) ||
+ fileHasSuffix(basename, "mod") ||
+ fileHasSuffix(basename, "s3m") ||
+ fileHasSuffix(basename, "it") ||
+ fileHasSuffix(basename, "xm") ||
+ fileHasSuffix(basename, "midi") ||
+ fileHasSuffix(basename, "mid") ||
+ fileHasSuffix(basename, "mp3") ||
+ fileHasSuffix(basename, "ogg"))
+ return TRUE;
+#endif
+#endif
+
+ return FALSE;
+#endif
+}
+
+boolean FileIsArtworkType(char *basename, int type)
+{
+ if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(basename)) ||
+ (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(basename)) ||
+ (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(basename)))
+ return TRUE;
+
+ return FALSE;
+}
+
+/* ------------------------------------------------------------------------- */
+/* functions for loading artwork configuration information */