BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
binutils.cpp
Go to the documentation of this file.
1 #include "mvvm/utils/binutils.h"
2 #include <fstream>
3 #include <iostream>
4 
5 namespace {
6 //! Returns buffer size
7 int get_buffer_size(const std::string& filename);
8 
9 //! Returns part of file content in a buffer
10 void get_buffer_data(const std::string& filename, int* buffer, int buffer_size);
11 
12 //! Returns true if the interger is control character as defined above
13 bool is_control_char(int ch);
14 
15 //! Returns true if there is two null bytes in the buffer
16 bool null_check(int* buffer, int buffer_size);
17 } // namespace
18 
19 namespace ModelView ::Utils {
20 
21 // length of buffer
22 #define BYTE_LENGTH 2048
23 
24 // null character
25 #define NULL_CHR (0x00)
26 
27 // control characters
28 #define NUL 0
29 #define BS 8
30 #define CR 13
31 #define SUB 26
32 
33 bool is_binary(const std::string& filename)
34 {
35  int buffer[BYTE_LENGTH];
36 
37  // get buffer size
38  int buffer_size = get_buffer_size(filename);
39  if (buffer_size == 0)
40  return false;
41 
42  // buffer allocation
43  get_buffer_data(filename, buffer, buffer_size);
44 
45  // control character check
46  for (int count = 0; count < buffer_size; ++count)
47  if (is_control_char(buffer[count]))
48  return true;
49 
50  return null_check(buffer, buffer_size);
51 }
52 
53 bool is_text(const std::string& filename)
54 {
55 
56  return (!is_binary(filename));
57 }
58 
59 } // namespace ModelView::Utils
60 
61 namespace {
62 
63 int get_buffer_size(const std::string& filename)
64 {
65  std::ifstream mySource;
66  mySource.open(filename, std::ios_base::binary);
67  mySource.seekg(0, std::ios_base::end);
68  int size = mySource.tellg();
69  mySource.close();
70  return (size > BYTE_LENGTH) ? BYTE_LENGTH : size;
71 }
72 
73 void get_buffer_data(const std::string& filename, int* buffer, int byte_length)
74 {
75  std::ifstream fstr(filename, std::ios::in | std::ios::binary);
76  for (int i = 0; i < byte_length; i++)
77  buffer[i] = fstr.get();
78 }
79 
80 bool is_control_char(int ch)
81 {
82  return ((ch > NUL && ch < BS) || (ch > CR && ch < SUB));
83 }
84 
85 bool null_check(int* buffer, int buffer_size)
86 {
87  for (int i = 1; i < buffer_size; ++i)
88  if (buffer[i] == NULL_CHR && buffer[i - 1] == NULL_CHR)
89  return true;
90  return false;
91 }
92 } // namespace
#define NULL_CHR
Definition: binutils.cpp:25
#define BS
Definition: binutils.cpp:29
#define SUB
Definition: binutils.cpp:31
#define CR
Definition: binutils.cpp:30
#define BYTE_LENGTH
Definition: binutils.cpp:22
#define NUL
Definition: binutils.cpp:28
Defines class CLASS?
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")
bool is_text(const std::string &filename)
Returns true if file is text/ascii.
Definition: binutils.cpp:53
bool is_binary(const std::string &filename)
Returns true if file is binary.
Definition: binutils.cpp:33
materialitems.h Collection of materials to populate MaterialModel.