FILE *linkBuf(FILE *stream, LINEBUF *IFS, size_t size) { if (stream != NULL) { if (IFS == NULL) stream = NULL; else { if (IFS->stream != NULL && closeBuf(IFS, 1)) stream = NULL; else { IFS->stream = NULL; if (size != 0 && size != IFS->lineLength) { if (IFS->buffer != NULL) FREE(IFS->buffer); IFS->buffer = MALLOC(size); IFS->lineLength = size; } IFS->stream = stream; IFS->norewind=!rewindable(IFS->stream); IFS->fullLineRead = 1; IFS->isComment = 0; IFS->commentChar = '#'; IFS->bufferOffset = ftell(stream); } } } return stream; }
BufferedImage::Ref load_image(InputStream& in, std::string source_name, std::string format_name, Logger* logger, FileFormat::ProgressTracker* tracker, FileFormat::Registry::ConstRefArg r) { FileFormat::Registry::ConstRef registry = r; if (!registry) registry = FileFormat::Registry::get_default_registry(); RewindableStream rewindable(in); // Primary auto-detection if (format_name.empty()) { for (int i = 0; i < registry->get_num_formats(); ++i) { FileFormat::ConstRef f = registry->get_format(i); bool s = f->check_signature(rewindable); rewindable.rewind(); if (!s) continue; format_name = f->get_name(); break; } } rewindable.release(); // Secondary auto-detection if (format_name.empty()) { std::string suffix = ascii_tolower(file::suffix_of(source_name)); if (!suffix.empty()) { for (int i = 0; i < registry->get_num_formats(); ++i) { FileFormat::ConstRef f = registry->get_format(i); if (!f->check_suffix(suffix)) continue; format_name = f->get_name(); break; } } } if (format_name.empty()) { throw UnresolvableFormatException("Image format could not be detected from the initial " "data nor from the file name: \""+source_name+"\""); } for (int i = 0; i < registry->get_num_formats(); ++i) { FileFormat::ConstRef f = registry->get_format(i); if (format_name != f->get_name()) continue; BufferedImage::Ref j = f->load(rewindable, logger, tracker); return j; } throw UnknownFormatException("Unrecognized format specifier: \""+format_name+"\""); }
FILE *openBuf(const char *path, const char *mode, LINEBUF *IFS, size_t size) { if (size != 0) { IFS->buffer = MALLOC(size); IFS->lineLength = size; IFS->stream = NULL; } if (size == 0 || IFS->buffer != NULL) { IFS->stream=fopen(path, mode); IFS->norewind=!rewindable(IFS->stream); IFS->fullLineRead = 1; IFS->isComment = 0; IFS->commentChar = '#'; IFS->bufferOffset = (IFS->stream == NULL) ? 0 : ftell(IFS->stream); } return IFS->stream; }