-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
97 lines (90 loc) · 2.66 KB
/
main.cpp
File metadata and controls
97 lines (90 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Defines the entry point for the console application.
#ifdef _MSC_VER
#include <windows.h>
#endif
#include "TinyNPY.h"
#include <iostream> // std::cout
int main(int argc, const char** argv)
{
if (argc < 2 || argc > 3) {
std::cout << "Usage:\n";
std::cout << " TinyNPY <file.npy> - Read single NPY file\n";
std::cout << " TinyNPY <file.npz> --all - Read all arrays from NPZ file\n";
std::cout << " TinyNPY <file.npz> <name> - Read specific array from NPZ file\n";
return -1;
}
// Check if requesting all arrays or specific array
const std::string filepath = argv[1];
std::string varname;
bool read_all = false;
if (argc == 3) {
varname = argv[2];
if (varname == "--all") {
read_all = true;
varname.clear();
}
}
// Check file extension to determine format
bool is_npz = (filepath.length() > 4 && filepath.substr(filepath.length() - 4) == ".npz");
if (is_npz && read_all) {
// Read all arrays from NPZ
NpyArray::npz_t arrays;
LPCSTR ret = NpyArray::LoadNPZ(filepath.c_str(), arrays);
if (ret != NULL) {
std::cout << ret << " '" << filepath << "'\n";
return -2;
}
std::cout << "Loaded " << arrays.size() << " arrays from NPZ file:\n\n";
for (auto& pair : arrays)
std::cout << pair.second.PrintInfo(pair.first) << "\n";
return EXIT_SUCCESS;
}
if (is_npz && !varname.empty()) {
// Read specific array from NPZ
NpyArray arr;
LPCSTR ret = arr.LoadNPZ(filepath.c_str(), varname.c_str());
if (ret != NULL) {
std::cout << ret << " '" << filepath << "' (varname='" << varname << "')\n";
return -2;
}
if (arr.IsEmpty()) {
std::cout << "Array '" << varname << "' not found in '" << filepath << "'\n";
return -3;
}
std::cout << "Loaded array '" << varname << "' from NPZ:\n\n";
std::cout << arr.PrintInfo(varname);
return EXIT_SUCCESS;
}
// Read single NPY file or first array from NPZ
NpyArray arr;
LPCSTR ret = NULL;
if (is_npz) {
ret = arr.LoadNPZ(filepath.c_str(), "data");
if (ret != NULL) {
// Try loading the first array if "data" doesn't exist
NpyArray::npz_t arrays;
ret = NpyArray::LoadNPZ(filepath.c_str(), arrays);
if (ret != NULL) {
std::cout << ret << " '" << filepath << "'\n";
return -2;
}
if (!arrays.empty()) {
// Since we can't assign, just read the first array by name
std::string first_name = arrays.begin()->first;
ret = arr.LoadNPZ(filepath.c_str(), first_name.c_str());
if (ret != NULL) {
std::cout << ret << "\n";
return -2;
}
}
}
} else {
ret = arr.LoadNPY(filepath.c_str());
}
if (ret != NULL) {
std::cout << ret << " '" << filepath << "'\n";
return -2;
}
std::cout << arr.PrintInfo();
return EXIT_SUCCESS;
}