Swarm-NG  1.1
fileformat.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2011 by Saleh Dindar and the Swarm-NG Development Team *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 3 of the License. *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU General Public License *
14  * along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17  ************************************************************************/
18 
26 #pragma once
27 namespace swarm {
28 
30  struct ALIGN(16) swarm_header
31  {
32  char magic[6];
33  char version[2];
34  char m_type[76];
35  uint32_t flags;
36  uint64_t datalen;
37 
38  static const uint64_t npos = 0xFFFFFFFFFFFFFFFFLL;
39 
41  swarm_header(const std::string &type, int flags_ = 0, uint64_t datalen_ = npos)
42  {
43  strcpy(magic, "SWARM");
44  strcpy(version, "0");
45 
46  strncpy(this->m_type, type.c_str(), sizeof(this->m_type));
47  this->m_type[sizeof(this->m_type)-1] = '\0';
48 
49  flags = flags_;
50  datalen = datalen_;
51  }
52 
54  std::string type() const
55  {
56  const char *c = strstr(m_type, "//");
57  if(!c) { return trim(m_type); }
58  return trim(std::string(m_type, c - m_type));
59  }
61  bool is_compatible(const swarm_header &a)
62  {
63  bool ffver_ok = memcmp(magic, a.magic, 8) == 0;
64  std::string t = type();
65  bool type_ok = t.empty() || (t == a.type());
66  return ffver_ok && type_ok;
67  }
68  };
69 
71  struct ALIGN(16) swarm_index_header : public swarm_header
72  {
73  uint64_t timestamp; // datafile timestamp (mtime)
74  uint64_t datafile_size; // datafile file size
75 
76  swarm_index_header(const std::string &type, uint64_t timestamp_ = 0, uint64_t datafile_size_ = 0 , uint64_t datalen_ = npos)
77  : swarm_header(type, 0, datalen_), timestamp(timestamp_), datafile_size(datafile_size_)
78  {
79  }
80  };
81 
82 }