Пример #1
0
Файл: test.c Проект: tmud/libs
static void test_append(void) {
  struct zip_t *zip = zip_open(ZIPNAME, ZIP_DEFAULT_COMPRESSION_LEVEL, 'a');
  assert(zip != NULL);

  assert(0 == zip_entry_open(zip, "test\\test-2.txt"));
  assert(0 == strcmp(zip_entry_name(zip), "test/test-2.txt"));
  assert(total_entries == zip_entry_index(zip));
  assert(0 == zip_entry_write(zip, TESTDATA2, strlen(TESTDATA2)));
  assert(strlen(TESTDATA2) == zip_entry_size(zip));
  assert(CRC32DATA2 == zip_entry_crc32(zip));

  ++total_entries;
  assert(0 == zip_entry_close(zip));

  assert(0 == zip_entry_open(zip, "test\\empty/"));
  assert(0 == strcmp(zip_entry_name(zip), "test/empty/"));
  assert(0 == zip_entry_size(zip));
  assert(0 == zip_entry_crc32(zip));

  assert(total_entries == zip_entry_index(zip));
  ++total_entries;
  assert(0 == zip_entry_close(zip));

  assert(0 == zip_entry_open(zip, "empty/"));
  assert(0 == strcmp(zip_entry_name(zip), "empty/"));
  assert(0 == zip_entry_size(zip));
  assert(0 == zip_entry_crc32(zip));

  assert(total_entries == zip_entry_index(zip));
  ++total_entries;
  assert(0 == zip_entry_close(zip));

  zip_close(zip);
}
Пример #2
0
Файл: test.c Проект: tmud/libs
static void test_entry_name(void) {
  struct zip_t *zip = zip_open(ZIPNAME, 0, 'r');
  assert(zip != NULL);

  assert(zip_entry_name(zip) == NULL);

  assert(0 == zip_entry_open(zip, "test\\test-1.txt"));
  assert(NULL != zip_entry_name(zip));
  assert(0 == strcmp(zip_entry_name(zip), "test/test-1.txt"));
  assert(strlen(TESTDATA1) == zip_entry_size(zip));
  assert(CRC32DATA1 == zip_entry_crc32(zip));
  assert(0 == zip_entry_index(zip));

  assert(0 == zip_entry_close(zip));

  assert(0 == zip_entry_open(zip, "test/test-2.txt"));
  assert(NULL != zip_entry_name(zip));
  assert(0 == strcmp(zip_entry_name(zip), "test/test-2.txt"));
  assert(strlen(TESTDATA2) == zip_entry_size(zip));
  assert(CRC32DATA2 == zip_entry_crc32(zip));
  assert(1 == zip_entry_index(zip));

  assert(0 == zip_entry_close(zip));

  zip_close(zip);
}
Пример #3
0
Файл: test.c Проект: tmud/libs
static void test_fwrite(void) {
  const char *filename = WFILE;
  FILE *stream = NULL;
  struct zip_t *zip = NULL;
#if defined(_MSC_VER)
  if (0 != fopen_s(&stream, filename, "w+"))
#else
  if (!(stream = fopen(filename, "w+")))
#endif
  {
    // Cannot open filename
    fprintf(stdout, "Cannot open filename\n");
    assert(0 == -1);
  }
  fwrite(TESTDATA1, sizeof(char), strlen(TESTDATA1), stream);
  assert(0 == fclose(stream));

  zip = zip_open(ZIPNAME, 9, 'w');
  assert(zip != NULL);
  assert(0 == zip_entry_open(zip, WFILE));
  assert(0 == zip_entry_fwrite(zip, WFILE));
  assert(0 == zip_entry_close(zip));

  zip_close(zip);
  remove(WFILE);
  remove(ZIPNAME);
}
Пример #4
0
void D3MFExporter::writeRelInfoToFile( const std::string &folder, const std::string &relName ) {
    if ( nullptr == m_zipArchive ) {
        throw DeadlyExportError( "3MF-Export: Zip archive not valid, nullptr." );
    }
    const std::string entry = folder + "/" + relName;
    zip_entry_open( m_zipArchive, entry.c_str() );

    const std::string &exportTxt( mRelOutput.str() );
    zip_entry_write( m_zipArchive, exportTxt.c_str(), exportTxt.size() );

    zip_entry_close( m_zipArchive );
}
Пример #5
0
void D3MFExporter::exportContentTyp( const std::string &filename ) {
    if ( nullptr == m_zipArchive ) {
        throw DeadlyExportError( "3MF-Export: Zip archive not valid, nullptr." );
    }
    const std::string entry = filename;
    zip_entry_open( m_zipArchive, entry.c_str() );

    const std::string &exportTxt( mContentOutput.str() );
    zip_entry_write( m_zipArchive, exportTxt.c_str(), exportTxt.size() );

    zip_entry_close( m_zipArchive );
}
Пример #6
0
Файл: test.c Проект: tmud/libs
static void test_extract(void) {
  struct buffer_t buf;

  struct zip_t *zip = zip_open(ZIPNAME, 0, 'r');
  assert(zip != NULL);
  memset((void *)&buf, 0, sizeof(struct buffer_t));

  assert(0 == zip_entry_open(zip, "test/test-1.txt"));
  assert(0 == zip_entry_extract(zip, on_extract, &buf));

  assert(buf.size == strlen(TESTDATA1));
  assert(0 == strncmp(buf.data, TESTDATA1, buf.size));
  assert(0 == zip_entry_close(zip));
  free(buf.data);
  buf.data = NULL;
  buf.size = 0;

  zip_close(zip);
}
Пример #7
0
Файл: test.c Проект: tmud/libs
static void test_mtime(void) {
  struct MZ_FILE_STAT_STRUCT file_stat1, file_stat2;

  const char *filename = WFILE;
  FILE *stream = NULL;
  struct zip_t *zip = NULL;
#if defined(_MSC_VER)
  if (0 != fopen_s(&stream, filename, "w+"))
#else
  if (!(stream = fopen(filename, "w+")))
#endif
  {
    // Cannot open filename
    fprintf(stdout, "Cannot open filename\n");
    assert(0 == -1);
  }
  fwrite(TESTDATA1, sizeof(char), strlen(TESTDATA1), stream);
  assert(0 == fclose(stream));

  memset(&file_stat1, 0, sizeof(file_stat1));
  memset(&file_stat2, 0, sizeof(file_stat2));
  zip = zip_open(ZIPNAME, ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
  assert(zip != NULL);
  assert(0 == zip_entry_open(zip, filename));
  assert(0 == zip_entry_fwrite(zip, filename));
  assert(0 == zip_entry_close(zip));
  zip_close(zip);

  assert(0 == MZ_FILE_STAT(filename, &file_stat1));

  remove(filename);
  assert(0 == zip_extract(ZIPNAME, ".", NULL, NULL));
  assert(0 == MZ_FILE_STAT(filename, &file_stat2));
  fprintf(stdout, "file_stat1.st_mtime: %lu\n", file_stat1.st_mtime);
  fprintf(stdout, "file_stat2.st_mtime: %lu\n", file_stat2.st_mtime);
  assert(labs(file_stat1.st_mtime - file_stat2.st_mtime) <= 1);

  remove(filename);
  remove(ZIPNAME);
}
Пример #8
0
Файл: test.c Проект: tmud/libs
static void test_read(void) {
  char *buf = NULL;
  ssize_t bufsize;
  size_t buftmp;
  struct zip_t *zip = zip_open(ZIPNAME, 0, 'r');
  assert(zip != NULL);

  assert(0 == zip_entry_open(zip, "test\\test-1.txt"));
  assert(strlen(TESTDATA1) == zip_entry_size(zip));
  assert(CRC32DATA1 == zip_entry_crc32(zip));

  bufsize = zip_entry_read(zip, (void **)&buf, &buftmp);
  assert(bufsize == strlen(TESTDATA1));
  assert((size_t)bufsize == buftmp);
  assert(0 == strncmp(buf, TESTDATA1, bufsize));
  assert(0 == zip_entry_close(zip));
  free(buf);
  buf = NULL;
  bufsize = 0;

  assert(0 == zip_entry_open(zip, "test/test-2.txt"));
  assert(strlen(TESTDATA2) == zip_entry_size(zip));
  assert(CRC32DATA2 == zip_entry_crc32(zip));

  bufsize = zip_entry_read(zip, (void **)&buf, NULL);
  assert((size_t)bufsize == strlen(TESTDATA2));
  assert(0 == strncmp(buf, TESTDATA2, (size_t)bufsize));
  assert(0 == zip_entry_close(zip));
  free(buf);
  buf = NULL;
  bufsize = 0;

  assert(0 == zip_entry_open(zip, "test\\empty/"));
  assert(0 == strcmp(zip_entry_name(zip), "test/empty/"));
  assert(0 == zip_entry_size(zip));
  assert(0 == zip_entry_crc32(zip));
  assert(0 == zip_entry_close(zip));

  buftmp = strlen(TESTDATA2);
  buf = calloc(buftmp, sizeof(char));
  assert(0 == zip_entry_open(zip, "test/test-2.txt"));

  bufsize = zip_entry_noallocread(zip, (void *)buf, buftmp);
  assert(buftmp == (size_t)bufsize);
  assert(0 == strncmp(buf, TESTDATA2, buftmp));
  assert(0 == zip_entry_close(zip));
  free(buf);
  buf = NULL;
  bufsize = 0;

  buftmp = strlen(TESTDATA1);
  buf = calloc(buftmp, sizeof(char));
  assert(0 == zip_entry_open(zip, "test/test-1.txt"));

  bufsize = zip_entry_noallocread(zip, (void *)buf, buftmp);
  assert(buftmp == (size_t)bufsize);
  assert(0 == strncmp(buf, TESTDATA1, buftmp));
  assert(0 == zip_entry_close(zip));
  free(buf);
  buf = NULL;
  bufsize = 0;

  zip_close(zip);
}
void main_window::on_save_mission()
{
    if (m_filename.empty())
    {
        on_save_as_mission();
        return;
    }

    zip_t *zip = zip_open(m_filename.c_str(), ZIP_DEFAULT_COMPRESSION_LEVEL, 0);
    if (!zip)
    {
        alert("Unable to save mission " + m_filename);
        return;
    }

    std::string str = "<!--Open Horizon mission-->\n";
    str += "<mission location=\"" + m_location + "\">\n";

    auto &p = m_scene_view->get_player();
    str += "\t<player ";
    str += "x=\"" + std::to_string(p.pos.x) + "\" ";
    str += "y=\"" + std::to_string(p.pos.y + p.y) + "\" ";
    str += "z=\"" + std::to_string(p.pos.z) + "\" ";
    str += "yaw=\"" + std::to_string(p.yaw.get_deg()) + "\" ";
    str += "editor_y=\"" + std::to_string(p.y) + "\" ";
    str += ">\n";
    str += "\t\t<attribute ";
    for (auto &a: p.attributes)
    {
        if (!a.second.empty())
            str += a.first + "=\"" + a.second + "\" ";
    }
    str += "/>\n";
    str += "\t</player>\n";

    for (auto &o: m_scene_view->get_objects())
    {
        str += "\n\t<object ";
        str += "name=\"" + o.name + "\" ";
        str += "id=\"" + o.id + "\" ";
        str += "active=\"" + to_string(o.active) + "\" ";
        str += "x=\"" + std::to_string(o.pos.x) + "\" ";
        str += "y=\"" + std::to_string(o.pos.y + o.y) + "\" ";
        str += "z=\"" + std::to_string(o.pos.z) + "\" ";
        str += "yaw=\"" + std::to_string(o.yaw.get_deg()) + "\" ";
        str += "editor_y=\"" + std::to_string(o.y) + "\" ";
        str += ">\n";
        str += "\t\t<attribute ";
        for (auto &a: o.attributes)
        {
            if (!a.second.empty())
                str += a.first + "=\"" + a.second + "\" ";
        }
        str += "/>\n";
        str += "\t</object>\n";
    }

    for (auto &z: m_scene_view->get_zones())
    {
        str += "\n\t<zone ";
        str += "name=\"" + z.name + "\" ";
        str += "active=\"" + to_string(z.active) + "\" ";
        str += "x=\"" + std::to_string(z.pos.x) + "\" ";
        str += "y=\"" + std::to_string(z.pos.y) + "\" ";
        str += "z=\"" + std::to_string(z.pos.z) + "\" ";
        str += "radius=\"" + std::to_string(z.radius) + "\" ";
        str += ">\n";
        str += "\t\t<attribute ";
        for (auto &a: z.attributes)
        {
            if (!a.second.empty())
                str += a.first + "=\"" + a.second + "\" ";
        }
        str += "/>\n";
        str += "\t</zone>\n";
    }

    for (auto &pth: m_scene_view->get_paths())
    {
        str += "\n\t<path ";
        str += "name=\"" + pth.name + "\" ";
        str += ">\n";

        for (auto &p: pth.points)
        {
            str += "\t\t<point ";
            str += "x=\"" + std::to_string(p.x) + "\" ";
            str += "y=\"" + std::to_string(p.y + p.w) + "\" ";
            str += "z=\"" + std::to_string(p.z) + "\" ";
            str += "editor_y=\"" + std::to_string(p.w) + "\" ";
            str += "/>\n";
        }

        str += "\t</path>\n";
    }

    str += "</mission>\n";

    zip_entry_open(zip, "objects.xml");
    zip_entry_write(zip, str.c_str(), str.length());
    zip_entry_close(zip);

    std::string script = to_str(m_script_edit->toPlainText());
    zip_entry_open(zip, "script.lua");
    zip_entry_write(zip, script.c_str(), script.size());
    zip_entry_close(zip);

    std::string info = "<!--Open Horizon mission info-->\n";
    info += "<info ";
    info += "name=\"" + std::string(to_str(m_mission_title->text())) + "\">\n";
    info += "\t<author name=\"" + std::string(to_str(m_mission_author->text())) + "\" ";
    info += "email=\"" + std::string(to_str(m_mission_email->text())) + "\"/>\n";
    info += "\t<description>";
    info += to_str(m_mission_description->toPlainText());
    info += "</description>\n";
    info += "</info>\n";
    zip_entry_open(zip, "info.xml");
    zip_entry_write(zip, info.c_str(), info.size());
    zip_entry_close(zip);

    zip_close(zip);
}