1// Aseprite TGA Library
2// Copyright (C) 2020-2021 Igara Studio S.A.
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#include "tga.h"
8
9#include <cassert>
10#include <limits>
11
12namespace tga {
13
14StdioFileInterface::StdioFileInterface(FILE* file)
15 : m_file(file)
16 , m_ok(true)
17{
18}
19
20bool StdioFileInterface::ok() const
21{
22 return m_ok;
23}
24
25size_t StdioFileInterface::tell()
26{
27 return ftell(m_file);
28}
29
30void StdioFileInterface::seek(size_t absPos)
31{
32 // To detect surprises with the size_t -> long cast.
33 assert(absPos <= std::numeric_limits<long>::max());
34
35 fseek(m_file, (long)absPos, SEEK_SET);
36}
37
38uint8_t StdioFileInterface::read8()
39{
40 int value = fgetc(m_file);
41 if (value != EOF) {
42 // We can safely cast to uint8_t as EOF is the only special
43 // non-uint8 value than fgetc() should return.
44 return (uint8_t)value;
45 }
46
47 m_ok = false;
48 return 0;
49}
50
51void StdioFileInterface::write8(uint8_t value)
52{
53 fputc(value, m_file);
54}
55
56} // namespace tga
57