JASSv2
instream_file_star.h
Go to the documentation of this file.
1 /*
2  INSTREAM_FILE_STAR.H
3  --------------------
4  Copyright (c) 2017 Andrew Trotman
5  Released under the 2-clause BSD license (See:https://en.wikipedia.org/wiki/BSD_licenses)
6 */
13 #pragma once
14 
15 #include <stdio.h>
16 
17 #include "instream_file.h"
18 
19 namespace JASS
20  {
21  /*
22  CLASS INSTREAM_FILE_STAR
23  ------------------------
24  */
29  {
30  public:
31  /*
32  INSTREAM_FILE_STAR::INSTREAM_FILE_STAR()
33  ----------------------------------------
34  */
40  instream_file(file)
41  {
42  /* Nothing */
43  }
44 
45  /*
46  INSTREAM_FILE_STAR::~INSTREAM_FILE_STAR()
47  -----------------------------------------
48  */
53  {
54  /* Nothing */
55  }
56 
57  /*
58  INSTREAM_FILE_STAR::UNITTEST()
59  ------------------------------
60  */
64  static void unittest(void)
65  {
66  /*
67  Example test string to read / write
68  */
69  const char example[] = "123456789012345678901234567890"; // sample to be written and read back
70 
71  /*
72  create the file Note that ::tmpfile() cannot be calle because its insecure (according to Coverity)
73  */
74  auto filename = file::mkstemp("jass");
75  FILE *fp = ::fopen(filename.c_str(), "w+b");
76  (void)::remove(filename.c_str()); // delete the file once we're done with it (cast to void to remove Coverity warning)
77 
78  /*
79  write to the file and rewind to the start
80  */
81  ::fwrite(example, sizeof(example), 1, fp);
82  ::fflush(fp);
83  ::rewind(fp);
84 
85  /*
86  create an instream_file_star and test it.
87  */
88  instream_file_star reader(fp);
90  document.contents = slice(document.contents_allocator, 30);
91 
92  /*
93  read from it making sure we got what we should have.
94  */
95  reader.read(document);
96  JASS_assert(document.contents.size() == 30);
97  for (size_t index = 0; index < document.contents.size(); index++)
98  JASS_assert(document.contents[index] == example[index]);
99 
100  /*
101  Yay, we passed
102  */
103  puts("instream_file_star::PASSED");
104  }
105  };
106 }
instream_file_star(FILE *file)
Constructor.
Definition: instream_file_star.h:39
C++ slices (string-descriptors)
Definition: slice.h:27
Container class representing a document through the indexing pipeline.
Definition: document.h:31
static void unittest(void)
Unit test this class.
Definition: instream_file_star.h:64
Subclass of the instream_file class used for reading data from a disk file that already exists (e...
Definition: instream_file_star.h:28
#define JASS_assert(expression)
Drop in replacement for assert() that aborts in Release as well as Debug.
Definition: asserts.h:33
virtual ~instream_file_star()
Destructior.
Definition: instream_file_star.h:52
Subclass of the instream base class used for reading data from a disk file.
Definition: instream_file.h:28
slice contents
The contents of the document (or likewise).
Definition: document.h:43
static std::string mkstemp(std::string prefix)
Generate a temporary filename containing the given prefix.
Definition: file.cpp:329
Subclass of instream for reading data from a disk file.
virtual void read(document &buffer)
Read buffer.contents.size() bytes of data into buffer.contents, resizing on eof.
Definition: instream_file.cpp:44
File based I/O methods including whole file and partial files.
Definition: file.h:45
allocator & contents_allocator
If memory is needed for the document contents then allocate from here.
Definition: document.h:41
Definition: compress_integer_elias_delta_simd.c:23
size_t size(void) const
Return the length of this slice.
Definition: slice.h:256