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