File: memory_file.cpp

package info (click to toggle)
dynare 4.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,356 kB
  • ctags: 15,842
  • sloc: cpp: 77,029; ansic: 29,056; pascal: 13,241; sh: 4,811; objc: 3,061; yacc: 3,013; makefile: 1,479; lex: 1,258; python: 162; lisp: 54; xml: 8
file content (62 lines) | stat: -rw-r--r-- 1,104 bytes parent folder | download | duplicates (5)
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
// Copyright (C) 2005, Ondra Kamenik

// $Id: memory_file.cpp 987 2006-10-17 14:39:19Z kamenik $

#include "memory_file.h"

#include <cstdio>

using namespace ogu;

int ogu::calc_pos_offset(int length, const char* str, int line, int col)
{
	int i = 0;
	int il = 1;
	int ic = 1;
	while (i < length && il <= line && ic <= col) {
		if (str[i] == '\n') {
			il++;
			ic = 1;
		} else {
			ic++;
		}
	}
	return i;
}

void ogu::calc_pos_line_and_col(int length, const char* str, int offset,
						   int& line, int& col)
{
	line = 1;
	col = 0;
	int i = 0;
	while (i < length && i < offset) {
		if (str[i] == '\n') {
			line++;
			col = 0;
		}
		i++;
		col++;
	}
}

MemoryFile::MemoryFile(const char* fname)
	: len(-1), data(NULL)
{
	FILE* fd = fopen(fname, "rb");
	if (fd) {
		// get the file size
		fseek(fd, 0, SEEK_END);
		len = ftell(fd);
		// allocate space for the file plus ending '\0' character
		data = new char[len+1];
		// read file and set data
		fseek(fd, 0, SEEK_SET);
		int i = 0;
		int c;
		while (EOF != (c = fgetc(fd)))
			data[i++] = (unsigned char)c;
		data[len] = '\0';
		fclose(fd);
	}
}