File: memory_file.h

package info (click to toggle)
dynare 4.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,408 kB
  • sloc: cpp: 84,998; ansic: 29,058; pascal: 13,843; sh: 4,833; objc: 4,236; yacc: 3,622; makefile: 2,278; lex: 1,541; python: 236; lisp: 69; xml: 8
file content (75 lines) | stat: -rw-r--r-- 2,014 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (C) 2005, Ondra Kamenik

// $Id: memory_file.h 762 2006-05-22 13:00:07Z kamenik $

#ifndef OGU_MEMORY_FILE
#define OGU_MEMORY_FILE

namespace ogu
{
  /** This function calculates an offset of a given position in a
   * given string. The position is given by the line number and by
   * the offset in the line (both starting from 1). */
  int calc_pos_offset(int length, const char *str, int line, int col);
  /** This function calculates a line number and column number of a
   * character given by the offset in the string. It is inverse to
   * calc_pos_offset. */
  void calc_pos_line_and_col(int length, const char *str, int offset,
                             int &line, int &col);

  /** This class opens a given file and makes its copy in memory and
   * appends it with the '\0' character. Since the type of length is
   * int, it can store files with size at most 4GB. If the file
   * could be opened for reading, data is NULL and length is -1. If
   * the file is empty but exists, len is zero and data points to a
   * newly allocated memory containing '\0' character at the end. */
  class MemoryFile
  {
  protected:
    int len;
    char *data;
  public:
    MemoryFile(const char *fname);
    virtual ~MemoryFile()
    {
      if (data)
        delete [] data;
    }
    int
    length() const
    {
      return len;
    }
    const char *
    base() const
    {
      return data;
    }
    bool
    exists() const
    {
      return len != -1;
    }
    /** Return the offset of a character in the given line
     * (starting from 1) with the given offset in the line. */
    int
    offset(int line, int lineoff) const
    {
      return calc_pos_offset(len, data, line, lineoff);
    }
    /** Return the line number and column number of the character
     * defined by the offset. */
    void
    line_and_col(int offset, int &line, int &col) const
    {
      calc_pos_line_and_col(len, data, offset, line, col);
    }
  };

};

#endif

// Local Variables:
// mode:C++
// End: