~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to storage/csv/transparent_file.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

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., 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
 
 
20
#include "mysql_priv.h"
 
21
#include "transparent_file.h"
 
22
 
 
23
Transparent_file::Transparent_file() : lower_bound(0), buff_size(IO_SIZE)
 
24
 
25
  buff= (uchar *) my_malloc(buff_size*sizeof(uchar),  MYF(MY_WME)); 
 
26
}
 
27
 
 
28
Transparent_file::~Transparent_file()
 
29
 
30
  my_free((uchar*)buff, MYF(MY_ALLOW_ZERO_PTR)); 
 
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;
 
38
  VOID(my_seek(filedes, 0, MY_SEEK_SET, MYF(0)));
 
39
  if (filedes && buff)
 
40
    upper_bound= my_read(filedes, buff, buff_size, MYF(0));
 
41
}
 
42
 
 
43
uchar *Transparent_file::ptr()
 
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
 
 
88
  VOID(my_seek(filedes, offset, MY_SEEK_SET, MYF(0)));
 
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
}