gmio/src/gmio_stl/stl_infos.c

117 lines
4.3 KiB
C
Raw Normal View History

/****************************************************************************
2017-01-27 01:05:12 +08:00
** Copyright (c) 2017, Fougue Ltd. <http://www.fougue.pro>
2016-07-05 18:46:22 +08:00
** All rights reserved.
**
2016-07-05 18:46:22 +08:00
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
2016-07-05 18:46:22 +08:00
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
**
** 2. Redistributions in binary form must reproduce the above
** copyright notice, this list of conditions and the following
** disclaimer in the documentation and/or other materials provided
** with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
****************************************************************************/
2015-12-10 01:51:03 +08:00
#include "stl_infos.h"
#include "../gmio_core/error.h"
#include "../gmio_core/internal/helper_memblock.h"
#include "../gmio_core/internal/helper_stream.h"
#include "stl_error.h"
2016-01-29 19:47:01 +08:00
#include "stl_format.h"
#include "internal/stla_infos_probe.h"
#include "internal/stlb_infos_probe.h"
int gmio_stl_infos_probe(
2016-01-29 19:47:01 +08:00
struct gmio_stl_infos* infos,
struct gmio_stream* stream,
2016-01-29 19:47:01 +08:00
unsigned flags,
const struct gmio_stl_infos_probe_options* opts)
{
2015-12-10 01:51:03 +08:00
int error = GMIO_ERROR_OK;
const struct gmio_streampos begin_streampos = gmio_streampos(stream, NULL);
struct gmio_memblock_helper mblock_helper =
2016-01-29 19:47:01 +08:00
gmio_memblock_helper(opts != NULL ? &opts->stream_memblock : NULL);
enum gmio_stl_format format =
opts != NULL ? opts->format_hint : GMIO_STL_FORMAT_UNKNOWN;
struct gmio_stl_infos_probe_options ovrdn_opts = {0};
2016-01-29 19:47:01 +08:00
if (opts != NULL)
ovrdn_opts = *opts;
ovrdn_opts.stream_memblock = mblock_helper.memblock;
/* Guess format when left unspecified */
if (format == GMIO_STL_FORMAT_UNKNOWN) {
2016-03-11 19:43:30 +08:00
format = gmio_stl_format_probe(stream);
2016-01-29 19:47:01 +08:00
if (format == GMIO_STL_FORMAT_UNKNOWN) {
error = GMIO_STL_ERROR_UNKNOWN_FORMAT;
goto label_end;
}
ovrdn_opts.format_hint = format;
}
if (flags & GMIO_STL_INFO_FLAG_FORMAT)
infos->format = format;
/* Dispatch to the sub-function */
switch (format) {
case GMIO_STL_FORMAT_ASCII:
error = gmio_stla_infos_probe(infos, stream, flags, &ovrdn_opts);
break;
case GMIO_STL_FORMAT_BINARY_LE:
case GMIO_STL_FORMAT_BINARY_BE:
error = gmio_stlb_infos_probe(infos, stream, flags, &ovrdn_opts);
break;
default:
error = GMIO_STL_ERROR_UNKNOWN_FORMAT;
break;
}
2016-01-29 19:47:01 +08:00
label_end:
gmio_stream_set_pos(stream, &begin_streampos);
2016-01-29 19:47:01 +08:00
gmio_memblock_helper_release(&mblock_helper);
return error;
}
int gmio_stl_infos_probe_file(
struct gmio_stl_infos *infos,
const char *filepath,
unsigned flags,
const struct gmio_stl_infos_probe_options *options)
{
FILE* file = fopen(filepath, "rb");
if (file != NULL) {
struct gmio_stream stream = gmio_stream_stdio(file);
const int error = gmio_stl_infos_probe(infos, &stream, flags, options);
fclose(file);
return error;
}
return GMIO_ERROR_STDIO;
}
gmio_streamsize_t gmio_stla_infos_probe_streamsize(
struct gmio_stream *stream, struct gmio_memblock *stream_memblock)
{
struct gmio_stl_infos infos = {0};
struct gmio_stl_infos_probe_options options = {0};
options.stream_memblock = *stream_memblock;
options.format_hint = GMIO_STL_FORMAT_ASCII;
gmio_stl_infos_probe(&infos, stream, GMIO_STL_INFO_FLAG_SIZE, &options);
return infos.size;
}