Elaboradar  0.1
 Tutto Classi Namespace File Funzioni Variabili Tipi enumerati (enum) Gruppi
utils.h
Vai alla documentazione di questo file.
1 
5 #ifndef RADARELAB_UTILS_H
6 #define RADARELAB_UTILS_H
7 
8 #include <functional>
9 #include <cstdio>
10 #include <string>
11 #include "logging.h"
12 #include "matrix.h"
13 
14 namespace radarelab {
15 
21 class File
22 {
23 protected:
24  log4c_category_t* logging_category;
25  std::string fname;
26  std::string fdesc;
27  FILE* fd = nullptr;
28 
29 public:
30  File();
31  File(log4c_category_t* logging_category);
32  File(const File&) = delete;
33  File(File&&);
34  ~File();
35 
36  File& operator=(const File*) = delete;
37 
43  bool open_from_env(const char* varname, const char* mode, const char* desc=nullptr);
44 
50  bool open(const std::string& fname, const char* mode, const char* desc=nullptr);
51 
52  const char* name() const { return fname.c_str(); }
53  const char* desc() const { return fdesc.c_str(); }
54 
56  operator FILE*() { return fd; }
57 
62  operator bool() const { return fd; }
63 
71  bool fread(void* buf, size_t size);
72 
73  void fseek(size_t seek_par, int origin);
74 
78  void read_lines(std::function<void (char*, size_t)> line_cb);
79 };
80 
84 void str_split(char* str, const char* sep, std::function<void (const char* tok)> val_cb);
85 
90 const char* getenv_default(const char* envname, const char* default_value);
91 
100 FILE* fopen_checked(const char* fname, const char* mode, const char* description=0);
101 
102 }
103 
104 #endif
bool fread(void *buf, size_t size)
Performs a fread on the file, throwing an exception if anything goes wrong.
Definition: utils.cpp:109
void read_lines(std::function< void(char *, size_t)> line_cb)
Read the file line by line, calling line_cb on each line read.
Definition: utils.cpp:75
FILE * fopen_checked(const char *fname, const char *mode, const char *description)
A wrapper of fopen that throws an exception if it cannot open the file.
Definition: utils.cpp:144
void str_split(char *str, const char *sep, std::function< void(const char *tok)> val_cb)
Split a string in tokens, skipping the given separator characters.
Definition: utils.cpp:165
bool open_from_env(const char *varname, const char *mode, const char *desc=nullptr)
Opens a file taking its name from the environment variable envname.
Definition: utils.cpp:37
const char * getenv_default(const char *envname, const char *default_value)
A wrapper of getenv, that returns &#39;default_value&#39; if the given environment name is not defined...
Definition: utils.cpp:137
Open a file taking its name from a given env variable.
Definition: utils.h:21
bool open(const std::string &fname, const char *mode, const char *desc=nullptr)
Opens a file by its pathname.
Definition: utils.cpp:49