Beatmup
asset.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "asset.h"
20 #include <cstdio>
21 
22 using namespace Beatmup;
23 using namespace Android;
24 
25 AssetPath::AssetPath(AAssetManager *manager, const char *path_): manager(manager) {
26  follow(path_);
27 }
28 
30  for (auto _ = path.rbegin(); _ != path.rend(); ++_) {
31  AAssetDir_close(*_);
32  }
33 }
34 
35 void AssetPath::follow(const char *path_) {
36  size_t pos = 0;
37  std::string path(path_);
38  do {
39  size_t i = path.find(PATH_DELIMITER, pos);
40  if (i == std::string::npos)
41  i = path.length() + 1;
42  std::string dir(path.substr(pos, i - pos - 1));
43  if (dir == "..")
44  up();
45  else
46  this->path.push_back( AAssetManager_openDir(manager, dir.c_str()) );
47  pos = i + 1;
48  } while (pos < path.length());
49 }
50 
51 msize AssetPath::listFiles(std::vector<std::string> &files) {
52  if (path.empty())
53  return (msize)0;
54  const char* filename = nullptr;
55  msize counter = 0;
56  while (( filename = AAssetDir_getNextFileName(path.back()) ) != nullptr) {
57  files.emplace_back(filename);
58  counter++;
59  }
60  AAssetDir_rewind(path.back());
61  return counter;
62 }
63 
64 bool AssetPath::up() {
65  if (path.size() <= 1)
66  return false;
67  AAssetDir_close(path.back());
68  path.pop_back();
69  return true;
70 }
71 
72 
73 Asset::Asset(AAssetManager *manager, const char *path) {
74  asset = AAssetManager_open(manager, path, AASSET_MODE_STREAMING);
75  if (!asset)
76  throw IOError(path, "Cannot access the asset");
77 }
78 
80  AAsset_close(asset);
81 }
82 
83 bool Asset::operator()(void *buffer, msize bytes) {
84  if (bytes == 0)
85  return true;
86  return AAsset_read(asset, buffer, bytes) > 0;
87 }
88 
89 bool Asset::seek(msize pos) {
90  auto result = AAsset_seek64(asset, pos, SEEK_SET);
91  return result != (off64_t) -1;
92 }
93 
94 bool Asset::eof() const {
95  return AAsset_getRemainingLength(asset) <= 0;
96 }
97 
98 
99 ChunkAsset::ChunkAsset(AAssetManager* manager, const std::string& filename):
100  stream(manager, filename.c_str()),
101  ChunkStream(stream)
102 {
103  if (!parse())
104  throw IOError(filename, "Cannot parse asset");
105 }
AAssetManager * manager
Definition: asset.h:36
static const char PATH_DELIMITER
Definition: asset.h:39
AssetPath(AAssetManager *manager, const char *path="")
Definition: asset.cpp:25
bool up()
Goes one level up ("..") from the current asset folder.
Definition: asset.cpp:64
std::vector< AAssetDir * > path
Definition: asset.h:37
void follow(const char *path)
Definition: asset.cpp:35
msize listFiles(std::vector< std::string > &files)
Lists files in the current folder.
Definition: asset.cpp:51
bool eof() const
Returns true, if the end of the stream is reached (i.e., all the data is read or the stream is empty)...
Definition: asset.cpp:94
bool operator()(void *buffer, msize bytes)
Reads a given number of bytes into a specific memory location.
Definition: asset.cpp:83
bool seek(msize pos)
Moves the read pointer to a given position in the stream.
Definition: asset.cpp:89
Asset(AAssetManager *manager, const char *path)
Definition: asset.cpp:73
ChunkAsset(AAssetManager *manager, const std::string &filename)
Creates a read-only chunk collection from an asset.
Definition: asset.cpp:99
Stream of chunks.
Definition: chunkfile.h:132
bool parse()
Goes through the input stream to build the list of existing chunks.
Definition: chunkfile.cpp:39
uint32_t msize
memory size
Definition: basic_types.h:30
JNIEnv jlong jstring filename
Beatmup::IntPoint result