~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/csv/transparent_file.cc

  • Committer: Monty Taylor
  • Date: 2011-03-09 20:59:40 UTC
  • mfrom: (2226.1.14 build)
  • Revision ID: mordred@inaugust.com-20110309205940-7f5mk6zba2u7bawa
Merged Dave - Filtered Replication docs
Merged Olaf - Refactoring work
Removed archive, blackhole, filesystem_engine, blitzdb, csv and pbxt from
the tree pre-GA as we have no interest in supporting them moving forward.

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
 
 
22
 
using namespace drizzled;
23
 
 
24
 
Transparent_file::Transparent_file() : lower_bound(0), buff_size(IO_SIZE)
25
 
{
26
 
  buff= static_cast<unsigned char *>(malloc(buff_size*sizeof(unsigned char)));
27
 
}
28
 
 
29
 
Transparent_file::~Transparent_file()
30
 
{
31
 
  free(buff);
32
 
}
33
 
 
34
 
void Transparent_file::init_buff(int filedes_arg)
35
 
{
36
 
  filedes= filedes_arg;
37
 
  /* read the beginning of the file */
38
 
  lower_bound= 0;
39
 
  lseek(filedes, 0, SEEK_SET);
40
 
  if (filedes && buff)
41
 
    upper_bound= internal::my_read(filedes, buff, buff_size, MYF(0));
42
 
}
43
 
 
44
 
unsigned char *Transparent_file::ptr()
45
 
{
46
 
  return buff;
47
 
}
48
 
 
49
 
off_t Transparent_file::start()
50
 
{
51
 
  return lower_bound;
52
 
}
53
 
 
54
 
off_t Transparent_file::end()
55
 
{
56
 
  return upper_bound;
57
 
}
58
 
 
59
 
off_t Transparent_file::read_next()
60
 
{
61
 
  size_t bytes_read;
62
 
 
63
 
  /*
64
 
     No need to seek here, as the file managed by Transparent_file class
65
 
     always points to upper_bound byte
66
 
  */
67
 
  if ((bytes_read= internal::my_read(filedes, buff, buff_size, MYF(0))) == MY_FILE_ERROR)
68
 
    return (off_t) -1;
69
 
 
70
 
  /* end of file */
71
 
  if (!bytes_read)
72
 
    return (off_t) -1;
73
 
 
74
 
  lower_bound= upper_bound;
75
 
  upper_bound+= bytes_read;
76
 
 
77
 
  return lower_bound;
78
 
}
79
 
 
80
 
 
81
 
char Transparent_file::get_value(off_t offset)
82
 
{
83
 
  size_t bytes_read;
84
 
 
85
 
  /* check boundaries */
86
 
  if ((lower_bound <= offset) && (offset < upper_bound))
87
 
    return buff[offset - lower_bound];
88
 
 
89
 
  lseek(filedes, offset, SEEK_SET);
90
 
  /* read appropriate portion of the file */
91
 
  if ((bytes_read= internal::my_read(filedes, buff, buff_size,
92
 
                           MYF(0))) == MY_FILE_ERROR)
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
 
}