summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/FileCache.cpp
blob: 71087e0e899bf2e4ce55c44d14fb956565866783 (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
// **********************************************************************
//
// 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();
    }

    is.seekg(offset);
    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;
}