|
14 | 14 |
|
15 | 15 | #include "scratch3reader.h" |
16 | 16 | #include "reader_common.h" |
| 17 | +#include "zipreader.h" |
17 | 18 |
|
18 | 19 | using namespace libscratchcpp; |
19 | 20 | using json = nlohmann::json; |
@@ -299,14 +300,9 @@ bool Scratch3Reader::load() |
299 | 300 | else |
300 | 301 | printErr(std::string("could not parse ") + step, e.what()); |
301 | 302 |
|
302 | | - // Close the zip file |
303 | | - zip_close(m_zip); |
304 | | - |
305 | 303 | return false; |
306 | 304 | } |
307 | 305 |
|
308 | | - // Close the zip file |
309 | | - zip_close(m_zip); |
310 | 306 | return true; |
311 | 307 | } |
312 | 308 |
|
@@ -352,37 +348,15 @@ std::vector<std::string> Scratch3Reader::extensions() |
352 | 348 |
|
353 | 349 | void Scratch3Reader::read() |
354 | 350 | { |
355 | | - // Open the zip file |
356 | | - unsigned char *buf; |
357 | | - size_t bufsize; |
358 | | - m_zip = zip_open(fileName().c_str(), 0, 'r'); |
359 | | - if (!m_zip) { |
360 | | - std::cerr << "Failed to open project file" << std::endl; |
361 | | - return; |
362 | | - } |
363 | | - |
364 | | - // Extract project.json |
365 | | - zip_entry_open(m_zip, "project.json"); |
366 | | - bufsize = zip_entry_size(m_zip); |
367 | | - buf = (unsigned char *)calloc(sizeof(unsigned char), bufsize); |
368 | | - zip_entry_noallocread(m_zip, (void *)buf, bufsize); |
369 | | - zip_entry_close(m_zip); |
370 | | - std::string str(reinterpret_cast<char const *>(buf)); |
371 | | - free(buf); |
372 | | - |
373 | | - // Remove garbage after the JSON |
374 | | - int end; |
375 | | - for (end = str.size(); end >= 0; end--) { |
376 | | - char ch = str[end]; |
377 | | - if (ch == '}') |
378 | | - break; |
379 | | - } |
380 | | - std::string out = str.substr(0, end + 1); |
381 | | - |
382 | | - // Parse the JSON |
383 | | - try { |
384 | | - m_json = json::parse(out); |
385 | | - } catch (std::exception &e) { |
386 | | - printErr("invalid JSON file", e.what()); |
387 | | - } |
| 351 | + // Read project.json |
| 352 | + ZipReader reader(fileName()); |
| 353 | + if (reader.open()) { |
| 354 | + // Parse the JSON |
| 355 | + try { |
| 356 | + m_json = json::parse(reader.readFileToString("project.json")); |
| 357 | + } catch (std::exception &e) { |
| 358 | + printErr("invalid JSON file", e.what()); |
| 359 | + } |
| 360 | + } else |
| 361 | + printErr("could not read " + fileName()); |
388 | 362 | } |
0 commit comments