summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/FileCache.cpp
blob: ac548970e9fa1ab1981d88a31696a0d0683264f9 (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceGrid/FileCache.h>
#include <IceGrid/Exception.h>

#include <fstream>

using namespace std;
using namespace IceGrid;

FileCache::FileCache()
{
}

Ice::StringSeq
FileCache::read(const string& filename, Ice::Long offset, int count, Ice::Long& newOffset)
{
    ifstream is(filename.c_str());
    if(is.fail())
    {
	throw FileNotAvailableException("failed to open file `" + filename + "'");
    }

    newOffset = offset;
    is.seekg(0, ios::end);
    if(offset >= is.tellg())
    {
	newOffset = is.tellg();
	return Ice::StringSeq();
    }

#ifdef _WIN32
    is.seekg(static_cast<int>(offset));
#else
    is.seekg(static_cast<streampos>(offset));
#endif
    Ice::StringSeq lines;
    for(int i = 0; i < count && is.good(); ++i)
    {
	assert(!is.eof());
	string line;
	getline(is, line);
	if(!is.fail())
	{
	    newOffset = is.tellg();
	    assert(newOffset >= 0);
	}
	lines.push_back(line);
    }
    return lines;
}