void property_tree::from_unknown( std::istream& stream, boost::property_tree::ptree& ptree, property_tree::path_value::check_repeated_paths check_type, char equal_sign, char delimiter, bool use_index ) { if( is_seekable( stream ) ) { from_unknown_seekable( stream, ptree, check_type, equal_sign, delimiter, use_index ); } else { std::stringstream buffer; buffer << stream.rdbuf(); from_unknown_seekable( buffer, ptree, check_type, equal_sign, delimiter, use_index ); } }
void fseek_sub (int * unit, GFC_IO_INT * offset, int * whence, int * status) { gfc_unit * u = find_unit (*unit); ssize_t result = -1; if (u != NULL && is_seekable(u->s)) { result = sseek(u->s, *offset, *whence); unlock_unit (u); } if (status) *status = (result < 0 ? -1 : 0); }
void property_tree::from_unknown_seekable( std::istream& stream, boost::property_tree::ptree& ptree, property_tree::path_value::check_repeated_paths check_type, char equal_sign, char delimiter, bool use_index ) { if( !is_seekable( stream ) ) { COMMA_THROW( comma::exception, "input stream is not seekable" ); } try { stream.clear(); stream.seekg( 0, std::ios::beg ); boost::property_tree::read_json( stream, ptree ); return; } catch( const boost::property_tree::ptree_error& ex ) {} catch(...) { throw; } try { stream.clear(); stream.seekg( 0, std::ios::beg ); comma::property_tree::read_xml( stream, ptree ); return; } catch( const boost::property_tree::ptree_error& ex ) {} catch(...) { throw; } try { stream.clear(); stream.seekg( 0, std::ios::beg ); comma::property_tree::from_path_value( stream, ptree, check_type, equal_sign, delimiter, use_index ); return; } catch( const boost::property_tree::ptree_error& ex ) {} catch( const comma::exception& ex ) {} catch(...) { throw; } // TODO: add try for ini format (currently the problem is that path-value treats ini sections and comments as valid entries; possible solution: make path-value parser stricter) COMMA_THROW( comma::exception, "failed to guess format" ); }
sox_format_t * sox_open_read( char const * path, sox_signalinfo_t const * signal, sox_encodinginfo_t const * encoding, char const * filetype) { sox_format_t * ft = lsx_calloc(1, sizeof(*ft)); sox_format_handler_t const * handler; char const * const io_types[] = {"file", "pipe", "file URL"}; char const * type = ""; size_t input_bufsiz = sox_globals.input_bufsiz? sox_globals.input_bufsiz : sox_globals.bufsiz; if (filetype) { if (!(handler = sox_find_format(filetype, sox_false))) { lsx_fail("no handler for given file type `%s'", filetype); goto error; } ft->handler = *handler; } if (!(ft->handler.flags & SOX_FILE_NOSTDIO)) { if (!strcmp(path, "-")) { /* Use stdin if the filename is "-" */ if (sox_globals.stdin_in_use_by) { lsx_fail("`-' (stdin) already in use by `%s'", sox_globals.stdin_in_use_by); goto error; } sox_globals.stdin_in_use_by = "audio input"; SET_BINARY_MODE(stdin); ft->fp = stdin; } else { ft->fp = xfopen(path, "rb", &ft->io_type); type = io_types[ft->io_type]; if (ft->fp == NULL) { lsx_fail("can't open input %s `%s': %s", type, path, strerror(errno)); goto error; } } if (setvbuf (ft->fp, NULL, _IOFBF, sizeof(char) * input_bufsiz)) { lsx_fail("Can't set read buffer"); goto error; } ft->seekable = is_seekable(ft); } if (!filetype) { if (ft->seekable) { filetype = auto_detect_format(ft, lsx_find_file_extension(path)); lsx_rewind(ft); } #ifndef NO_REWIND_PIPE else if (!(ft->handler.flags & SOX_FILE_NOSTDIO) && input_bufsiz >= AUTO_DETECT_SIZE) { filetype = auto_detect_format(ft, lsx_find_file_extension(path)); rewind_pipe(ft->fp); ft->tell_off = 0; } #endif if (filetype) { lsx_report("detected file format type `%s'", filetype); if (!(handler = sox_find_format(filetype, sox_false))) { lsx_fail("no handler for detected file type `%s'", filetype); goto error; } } else { if (ft->io_type == lsx_io_pipe) { filetype = "sox"; /* With successful pipe rewind, this isn't useful */ lsx_report("assuming input pipe `%s' has file-type `sox'", path); } else if (!(filetype = lsx_find_file_extension(path))) { lsx_fail("can't determine type of %s `%s'", type, path); goto error; } if (!(handler = sox_find_format(filetype, sox_true))) { lsx_fail("no handler for file extension `%s'", filetype); goto error; } } ft->handler = *handler; if (ft->handler.flags & SOX_FILE_NOSTDIO) { xfclose(ft->fp, ft->io_type); ft->fp = NULL; } } if (!ft->handler.startread && !ft->handler.read) { lsx_fail("file type `%s' isn't readable", filetype); goto error; } ft->mode = 'r'; ft->filetype = lsx_strdup(filetype); ft->filename = lsx_strdup(path); if (signal) ft->signal = *signal; if (encoding) ft->encoding = *encoding; else sox_init_encodinginfo(&ft->encoding); set_endiannesses(ft); if ((ft->handler.flags & SOX_FILE_DEVICE) && !(ft->handler.flags & SOX_FILE_PHONY)) lsx_set_signal_defaults(ft); ft->priv = lsx_calloc(1, ft->handler.priv_size); /* Read and write starters can change their formats. */ if (ft->handler.startread && (*ft->handler.startread)(ft) != SOX_SUCCESS) { lsx_fail("can't open input %s `%s': %s", type, ft->filename, ft->sox_errstr); goto error; } /* Fill in some defaults: */ if (sox_precision(ft->encoding.encoding, ft->encoding.bits_per_sample)) ft->signal.precision = sox_precision(ft->encoding.encoding, ft->encoding.bits_per_sample); if (!(ft->handler.flags & SOX_FILE_PHONY) && !ft->signal.channels) ft->signal.channels = 1; if (sox_checkformat(ft) != SOX_SUCCESS) { lsx_fail("bad input format for %s `%s': %s", type, ft->filename, ft->sox_errstr); goto error; } if (signal) { if (signal->rate && signal->rate != ft->signal.rate) lsx_warn("can't set sample rate %g; using %g", signal->rate, ft->signal.rate); if (signal->channels && signal->channels != ft->signal.channels) lsx_warn("can't set %u channels; using %u", signal->channels, ft->signal.channels); } return ft; error: if (ft->fp && ft->fp != stdin) xfclose(ft->fp, ft->io_type); free(ft->priv); free(ft->filename); free(ft->filetype); free(ft); return NULL; }
ft_t st_open_read(const char *path, const st_signalinfo_t *info, const char *filetype) { ft_t ft; ft = (ft_t)calloc(sizeof(struct st_soundstream), 1); if (!ft ) return NULL; ft->filename = strdup(path); /* Let auto effect do the work if user is not overriding. */ if (!filetype) ft->filetype = strdup("auto"); else ft->filetype = strdup(filetype); if (!ft->filename || !ft->filetype) goto input_error; if (st_gettype(ft) != ST_SUCCESS) { st_warn("Unknown input file format for '%s': %s", ft->filename, ft->st_errstr); goto input_error; } ft->info.size = -1; ft->info.encoding = -1; ft->info.channels = -1; if (info) ft->info = *info; /* FIXME: Remove ft->swap from code */ ft->swap = ft->info.swap; ft->mode = 'r'; if (!(ft->h->flags & ST_FILE_NOSTDIO)) { /* Open file handler based on input name. Used stdin file handler * if the filename is "-" */ if (!strcmp(ft->filename, "-")) ft->fp = stdin; else if ((ft->fp = fopen(ft->filename, "rb")) == NULL) { st_warn("Can't open input file '%s': %s", ft->filename, strerror(errno)); goto input_error; } /* See if this file is seekable or not */ ft->seekable = is_seekable(ft); } /* Read and write starters can change their formats. */ if ((*ft->h->startread)(ft) != ST_SUCCESS) { st_warn("Failed reading %s: %s", ft->filename, ft->st_errstr); goto input_error; } /* Go a head and assume 1 channel audio if nothing is detected. * This is because libst usually doesn't set this for mono file * formats (for historical reasons). */ if (ft->info.channels == -1) ft->info.channels = 1; if (st_checkformat(ft) ) { st_fail("bad input format for file %s: %s", ft->filename, ft->st_errstr); goto input_error; } return ft; input_error: if (ft->filename) free(ft->filename); if (ft->filetype) free(ft->filetype); free(ft); return NULL; }
ft_t st_open_write_instr(const char *path, const st_signalinfo_t *info, const char *filetype, const char *comment, const st_instrinfo_t *instr, const st_loopinfo_t *loops) { ft_t ft; int i; ft = (ft_t)calloc(sizeof(struct st_soundstream), 1); if (!ft ) return NULL; ft->filename = strdup(path); /* Let auto effect do the work if user is not overriding. */ if (!filetype) { char *chop; int len; len = strlen(ft->filename); /* Use filename extension to determine audio type. */ chop = ft->filename + len; while (chop > ft->filename && *chop != LASTCHAR) chop--; while (chop < ft->filename+len && *chop != '.') chop++; if (*chop == '.') { chop++; ft->filetype = strdup(chop); } } else ft->filetype = strdup(filetype); if (!ft->filename || !ft->filetype) goto output_error; if (st_gettype(ft) != ST_SUCCESS) { st_warn("Unknown output file format for '%s': %s", ft->filename, ft->st_errstr); goto output_error; } ft->info.size = -1; ft->info.encoding = -1; ft->info.channels = -1; if (info) ft->info = *info; ft->mode = 'w'; if (!(ft->h->flags & ST_FILE_NOSTDIO)) { /* Open file handler based on input name. Used stdin file handler * if the filename is "-" */ if (!strcmp(ft->filename, "-")) { ft->fp = stdout; } else if ((ft->fp = fopen(ft->filename, "wb")) == NULL) { st_warn("Can't open output file '%s': %s", ft->filename, strerror(errno)); goto output_error; } /* stdout tends to be line-buffered. Override this */ /* to be Full Buffering. */ /* FIXME: Use buffer size from ft structure */ if (setvbuf (ft->fp, NULL, _IOFBF, sizeof(char)*ST_BUFSIZ)) { st_warn("Can't set write buffer"); goto output_error; } /* See if this file is seekable or not */ ft->seekable = is_seekable(ft); } if (ft->comment == NULL && comment != NULL) ft->comment = strdup(comment); else ft->comment = strdup("Processed by SoX"); if (loops) { for (i = 0; i < ST_MAX_NLOOPS; i++) { ft->loops[i] = loops[i]; } } /* leave SMPTE # alone since it's absolute */ if (instr) ft->instr = *instr; /* FIXME: Remove ft->swap from code */ ft->swap = ft->info.swap; /* Read and write starters can change their formats. */ if ((*ft->h->startwrite)(ft) != ST_SUCCESS) { st_warn("Failed writing %s: %s", ft->filename, ft->st_errstr); goto output_error; } if (st_checkformat(ft) ) { st_fail("bad output format for file %s: %s", ft->filename, ft->st_errstr); goto output_error; } return ft; output_error: if (ft->filename) free(ft->filename); if (ft->filetype) free(ft->filetype); free(ft); return NULL; }