2015-09-25 22:26:08 +08:00
|
|
|
/****************************************************************************
|
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.
|
2015-09-25 22:26:08 +08:00
|
|
|
**
|
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:
|
2015-09-25 22:26:08 +08:00
|
|
|
**
|
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-09-25 22:26:08 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-12-10 01:51:03 +08:00
|
|
|
#include "stl_infos.h"
|
2015-09-25 22:26:08 +08:00
|
|
|
|
|
|
|
#include "../gmio_core/error.h"
|
2015-12-17 19:03:45 +08:00
|
|
|
#include "../gmio_core/internal/helper_memblock.h"
|
2015-12-18 22:30:50 +08:00
|
|
|
#include "../gmio_core/internal/helper_stream.h"
|
2015-12-17 19:03:45 +08:00
|
|
|
#include "stl_error.h"
|
2016-01-29 19:47:01 +08:00
|
|
|
#include "stl_format.h"
|
2016-07-13 17:46:28 +08:00
|
|
|
#include "internal/stla_infos_probe.h"
|
|
|
|
#include "internal/stlb_infos_probe.h"
|
2015-12-04 01:00:25 +08:00
|
|
|
|
2016-07-13 17:46:28 +08:00
|
|
|
int gmio_stl_infos_probe(
|
2016-01-29 19:47:01 +08:00
|
|
|
struct gmio_stl_infos* infos,
|
2016-03-01 21:50:31 +08:00
|
|
|
struct gmio_stream* stream,
|
2016-01-29 19:47:01 +08:00
|
|
|
unsigned flags,
|
2016-07-13 17:46:28 +08:00
|
|
|
const struct gmio_stl_infos_probe_options* opts)
|
2015-09-25 22:26:08 +08:00
|
|
|
{
|
2015-12-10 01:51:03 +08:00
|
|
|
int error = GMIO_ERROR_OK;
|
2016-03-01 21:50:31 +08:00
|
|
|
const struct gmio_streampos begin_streampos = gmio_streampos(stream, NULL);
|
2015-12-17 19:03:45 +08:00
|
|
|
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;
|
2016-07-13 17:46:28 +08:00
|
|
|
struct gmio_stl_infos_probe_options ovrdn_opts = {0};
|
2015-12-17 19:03:45 +08:00
|
|
|
|
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 */
|
2015-12-17 19:03:45 +08:00
|
|
|
switch (format) {
|
|
|
|
case GMIO_STL_FORMAT_ASCII:
|
2016-07-13 17:46:28 +08:00
|
|
|
error = gmio_stla_infos_probe(infos, stream, flags, &ovrdn_opts);
|
2015-12-17 19:03:45 +08:00
|
|
|
break;
|
|
|
|
case GMIO_STL_FORMAT_BINARY_LE:
|
|
|
|
case GMIO_STL_FORMAT_BINARY_BE:
|
2016-07-13 17:46:28 +08:00
|
|
|
error = gmio_stlb_infos_probe(infos, stream, flags, &ovrdn_opts);
|
2015-12-17 19:03:45 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = GMIO_STL_ERROR_UNKNOWN_FORMAT;
|
|
|
|
break;
|
2015-12-04 01:00:25 +08:00
|
|
|
}
|
2015-09-25 22:26:08 +08:00
|
|
|
|
2016-01-29 19:47:01 +08:00
|
|
|
label_end:
|
2016-03-01 21:50:31 +08:00
|
|
|
gmio_stream_set_pos(stream, &begin_streampos);
|
2016-01-29 19:47:01 +08:00
|
|
|
gmio_memblock_helper_release(&mblock_helper);
|
2015-12-17 19:03:45 +08:00
|
|
|
return error;
|
2015-09-25 22:26:08 +08:00
|
|
|
}
|
2016-02-24 23:43:40 +08:00
|
|
|
|
2016-07-13 18:30:30 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-07-13 17:46:28 +08:00
|
|
|
gmio_streamsize_t gmio_stla_infos_probe_streamsize(
|
2016-02-24 23:43:40 +08:00
|
|
|
struct gmio_stream *stream, struct gmio_memblock *stream_memblock)
|
|
|
|
{
|
|
|
|
struct gmio_stl_infos infos = {0};
|
2016-07-13 17:46:28 +08:00
|
|
|
struct gmio_stl_infos_probe_options options = {0};
|
2016-02-24 23:43:40 +08:00
|
|
|
options.stream_memblock = *stream_memblock;
|
|
|
|
options.format_hint = GMIO_STL_FORMAT_ASCII;
|
2016-07-13 17:46:28 +08:00
|
|
|
gmio_stl_infos_probe(&infos, stream, GMIO_STL_INFO_FLAG_SIZE, &options);
|
2016-02-24 23:43:40 +08:00
|
|
|
return infos.size;
|
|
|
|
}
|