STIR  6.2.0
FileSignature.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006-2010, Hammersmith Imanet Ltd
3  Copyright (C) 2013, University College London
4  This file is part of STIR.
5  SPDX-License-Identifier: Apache-2.0
6 
7  See STIR/LICENSE.txt for details
8 */
9 #ifndef __stir_IO_FileSignature_h__
10 #define __stir_IO_FileSignature_h__
11 
18 #include "stir/utilities.h"
19 #include <fstream>
20 #include <string>
21 #include <algorithm>
22 
23 START_NAMESPACE_STIR
24 
26 
35 {
36 public:
38 
41  explicit FileSignature(std::istream& input) { this->_read_signature(input); }
43  explicit FileSignature(const std::string& filename)
44  {
45  std::ifstream input;
46  open_read_binary(input, filename);
47  this->_read_signature(input);
48  }
49 
51 
52  const char* get_signature() const { return this->_signature; }
53 
55  std::size_t size() const { return this->_size; }
56 
57 private:
58  static const std::size_t _max_signature_size = 1024U;
59  char _signature[_max_signature_size];
60  std::size_t _size;
61 
62  void _read_signature(std::istream& input)
63  {
64  // first initialise to zero, just in case the whole buffer isn't set
65  std::fill(this->_signature, this->_signature + this->_max_signature_size, '\0');
66  input.read(this->_signature, this->_max_signature_size);
67  this->_signature[this->_max_signature_size - 1] = '\0';
68  this->_size = static_cast<std::size_t>(input.gcount());
69  }
70 };
71 
72 END_NAMESPACE_STIR
73 #endif
A class to read/store the file signature.
Definition: FileSignature.h:34
This file declares various utility functions.
FILE *& open_read_binary(FILE *&fptr, const string &name)
opens a FILE for reading binary data. Calls error() when it does not succeed.
Definition: utilities.cxx:79
const char * get_signature() const
get access to the signature
Definition: FileSignature.h:52
FileSignature(const std::string &filename)
open file and read signature
Definition: FileSignature.h:43
std::size_t size() const
return size of valid signature read from the file
Definition: FileSignature.h:55
FileSignature(std::istream &input)
read signature
Definition: FileSignature.h:41