~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/filesystem_engine/transparent_file.cc

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2003 MySQL AB
2
 
 
3
 
  This program is free software; you can redistribute it and/or modify
4
 
  it under the terms of the GNU General Public License as published by
5
 
  the Free Software Foundation; version 2 of the License.
6
 
 
7
 
  This program is distributed in the hope that it will be useful,
8
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
  GNU General Public License for more details.
11
 
 
12
 
  You should have received a copy of the GNU General Public License
13
 
  along with this program; if not, write to the Free Software
14
 
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
 
 
16
 
 
17
 
#include "config.h"
18
 
#include <cstdlib>
19
 
#include "drizzled/internal/my_sys.h"
20
 
#include "transparent_file.h"
21
 
#include "utility.h"
22
 
 
23
 
using namespace drizzled;
24
 
 
25
 
TransparentFile::TransparentFile() : lower_bound(0), buff_size(IO_SIZE)
26
 
{
27
 
  buff= static_cast<unsigned char *>(malloc(buff_size*sizeof(unsigned char)));
28
 
}
29
 
 
30
 
TransparentFile::~TransparentFile()
31
 
{
32
 
  free(buff);
33
 
}
34
 
 
35
 
void TransparentFile::init_buff(int filedes_arg)
36
 
{
37
 
  filedes= filedes_arg;
38
 
  /* read the beginning of the file */
39
 
  lower_bound= 0;
40
 
  lseek(filedes, 0, SEEK_SET);
41
 
  if (filedes && buff)
42
 
    upper_bound= ::read(filedes, buff, buff_size);
43
 
}
44
 
 
45
 
unsigned char *TransparentFile::ptr()
46
 
{
47
 
  return buff;
48
 
}
49
 
 
50
 
off_t TransparentFile::start()
51
 
{
52
 
  return lower_bound;
53
 
}
54
 
 
55
 
off_t TransparentFile::end()
56
 
{
57
 
  return upper_bound;
58
 
}
59
 
 
60
 
off_t TransparentFile::read_next()
61
 
{
62
 
  ssize_t bytes_read;
63
 
 
64
 
  /*
65
 
     No need to seek here, as the file managed by TransparentFile class
66
 
     always points to upper_bound byte
67
 
  */
68
 
  if ((bytes_read= ::read(filedes, buff, buff_size)) < 0)
69
 
    return (off_t) -1;
70
 
 
71
 
  /* end of file */
72
 
  if (!bytes_read)
73
 
    return (off_t) -1;
74
 
 
75
 
  lower_bound= upper_bound;
76
 
  upper_bound+= bytes_read;
77
 
 
78
 
  return lower_bound;
79
 
}
80
 
 
81
 
 
82
 
char TransparentFile::get_value(off_t offset)
83
 
{
84
 
  ssize_t bytes_read;
85
 
 
86
 
  /* check boundaries */
87
 
  if ((lower_bound <= offset) && (offset < upper_bound))
88
 
    return buff[offset - lower_bound];
89
 
 
90
 
  lseek(filedes, offset, SEEK_SET);
91
 
  /* read appropriate portion of the file */
92
 
  if ((bytes_read= ::read(filedes, buff, buff_size)) < 0)
93
 
    return 0;
94
 
 
95
 
  lower_bound= offset;
96
 
  upper_bound= lower_bound + bytes_read;
97
 
 
98
 
  /* end of file */
99
 
  if (upper_bound == offset)
100
 
    return 0;
101
 
 
102
 
  return buff[0];
103
 
}