~drizzle-trunk/drizzle/development

1 by brian
clean slate
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
243.1.14 by Jay Pipes
* Ensured all drizzled/field/x.cc files to include mysql_priv.h
17
#include <drizzled/common_includes.h>
1 by brian
clean slate
18
#include "transparent_file.h"
19
20
Transparent_file::Transparent_file() : lower_bound(0), buff_size(IO_SIZE)
21
{ 
481 by Brian Aker
Remove all of uchar.
22
  buff= (unsigned char *) my_malloc(buff_size*sizeof(unsigned char),  MYF(MY_WME)); 
1 by brian
clean slate
23
}
24
25
Transparent_file::~Transparent_file()
26
{ 
481 by Brian Aker
Remove all of uchar.
27
  free((unsigned char*)buff); 
1 by brian
clean slate
28
}
29
30
void Transparent_file::init_buff(File filedes_arg)
31
{
32
  filedes= filedes_arg;
33
  /* read the beginning of the file */
34
  lower_bound= 0;
398.1.10 by Monty Taylor
Actually removed VOID() this time.
35
  my_seek(filedes, 0, MY_SEEK_SET, MYF(0));
1 by brian
clean slate
36
  if (filedes && buff)
37
    upper_bound= my_read(filedes, buff, buff_size, MYF(0));
38
}
39
481 by Brian Aker
Remove all of uchar.
40
unsigned char *Transparent_file::ptr()
1 by brian
clean slate
41
{ 
42
  return buff; 
43
}
44
45
off_t Transparent_file::start()
46
{ 
47
  return lower_bound; 
48
}
49
50
off_t Transparent_file::end()
51
{ 
52
  return upper_bound; 
53
}
54
55
off_t Transparent_file::read_next()
56
{
57
  size_t bytes_read;
58
59
  /*
60
     No need to seek here, as the file managed by Transparent_file class
61
     always points to upper_bound byte
62
  */
63
  if ((bytes_read= my_read(filedes, buff, buff_size, MYF(0))) == MY_FILE_ERROR)
64
    return (off_t) -1;
65
66
  /* end of file */
67
  if (!bytes_read)
68
    return (off_t) -1;
69
70
  lower_bound= upper_bound;
71
  upper_bound+= bytes_read;
72
73
  return lower_bound;
74
}
75
76
77
char Transparent_file::get_value(off_t offset)
78
{
79
  size_t bytes_read;
80
81
  /* check boundaries */
82
  if ((lower_bound <= offset) && (offset < upper_bound))
83
    return buff[offset - lower_bound];
84
398.1.10 by Monty Taylor
Actually removed VOID() this time.
85
  my_seek(filedes, offset, MY_SEEK_SET, MYF(0));
1 by brian
clean slate
86
  /* read appropriate portion of the file */
87
  if ((bytes_read= my_read(filedes, buff, buff_size,
88
                           MYF(0))) == MY_FILE_ERROR)
89
    return 0;
90
91
  lower_bound= offset;
92
  upper_bound= lower_bound + bytes_read;
93
94
  /* end of file */
95
  if (upper_bound == offset)
96
    return 0;
97
98
  return buff[0];
99
}