#include #include #include #include "glib.h" #include "ufo-roof.h" #include "ufo-roof-read-file.h" typedef struct { UfoRoofReadInterface iface; UfoRoofConfig *cfg; gchar *fname; FILE *fd; } UfoRoofReadFile; static void ufo_roof_read_file_free(UfoRoofReadInterface *iface) { UfoRoofReadFile *reader = (UfoRoofReadFile*)iface; if (reader) { if (reader->fname) g_free(reader->fname); if (reader->fd) fclose(reader->fd); free(reader); } } static guint ufo_roof_read_file(UfoRoofReadInterface *iface, uint8_t *buffers, GError **error) { UfoRoofReadFile *reader = (UfoRoofReadFile*)iface; UfoRoofConfig *cfg = reader->cfg; size_t bytes = 0; size_t packet_size = cfg->header_size + cfg->payload_size; size_t expected = cfg->max_packets * packet_size; while ((!feof(reader->fd))&&(!ferror(reader->fd))&&(bytes < expected)) { size_t ret = fread(buffers + bytes, 1, expected - bytes, reader->fd); bytes += ret; } guint packets = bytes / packet_size; if (ferror(reader->fd)) { roof_network_error_with_retval(error, 0, "read failed, error %i", ferror(reader->fd)); } else if ((feof(reader->fd))&&(bytes % packet_size)) { roof_network_error_with_retval(error, packets, "extra data in the end of input"); } return packets; } UfoRoofReadInterface *ufo_roof_read_file_new(UfoRoofConfig *cfg, const char *path, guint file_id, GError **error) { UfoRoofReadFile *reader = (UfoRoofReadFile*)calloc(1, sizeof(UfoRoofReadFile)); if (!reader) roof_new_error(error, "Can't allocate UfoRoofReadFile"); // FIXME: Shall we jump if max_packet_size > header+payload (or will be extra data included in the data files)? Report error for now. if ((cfg->header_size + cfg->payload_size) != cfg->max_packet_size) roof_new_error(error, "packet_size (%u) should be equal to max_packet_size (%u) if UfoRoofReadFile is used", cfg->header_size + cfg->payload_size, cfg->max_packet_size); reader->cfg = cfg; reader->iface.close = ufo_roof_read_file_free; reader->iface.read =ufo_roof_read_file; reader->fname = g_strdup_printf(path, file_id); if (!reader->fname) { free(reader); roof_new_error(error, "Can't build file name"); } reader->fd = fopen(reader->fname, "rb"); if (!reader->fd) { g_free(reader->fname); g_free(reader); roof_new_error(error, "Can't open file %i at path %s", file_id, path); } return (UfoRoofReadInterface*)reader; }