ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/PrimerMatch/mapFile.cc
Revision: 1.3
Committed: Mon Oct 9 21:42:05 2006 UTC (9 years, 8 months ago) by nje01
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +4 -0 lines
Log Message:
Updates for OSX/Darwin

Line File contents
1 /**************************************************************************
2 * This code is part of the supporting infrastructure for ATA Mapper.
3 * Copyright (C) 2002,2003,2004 Applera Corporation. All rights reserved.
4 * Author: Nathan Edwards
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received (LICENSE.txt) a copy of the GNU General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *************************************************************************/
20
21
22 #include <stdio.h>
23
24 /* posix compliant mmap */
25 # include <fcntl.h>
26 # include <errno.h>
27 # include <sys/types.h>
28 # include <unistd.h>
29 # include <stdlib.h>
30 # include <sys/stat.h>
31 # ifndef __USE_MISC
32 # define __USE_MISC
33 # endif
34 # if ! defined(__MAINGW32__)
35 # include <sys/mman.h>
36 # if !defined(MAP_SHARED)
37 # error MAP_SHARED
38 # endif
39 #endif
40
41 #ifdef __alpha
42 #define MMAPFLAGS (MAP_FILE | MAP_VARIABLE | MAP_SHARED)
43 #endif
44
45 #ifdef __linux
46 #define MMAPFLAGS (MAP_FILE | MAP_SHARED)
47 #endif
48
49 #ifdef __FreeBSD__
50 #define MMAPFLAGS (MAP_FILE | MAP_SHARED)
51 #endif
52
53 #ifdef _AIX
54 #define MMAPFLAGS (MAP_FILE | MAP_SHARED)
55 #endif
56
57 #ifdef __CYGWIN__
58 #define MMAPFLAGS (MAP_FILE | MAP_SHARED)
59 #endif
60
61 #ifdef __sun
62 #define MMAPFLAGS (MAP_SHARED)
63 #endif
64
65 #ifdef __APPLE_CC__
66 #define MMAPFLAGS (MAP_FILE | MAP_SHARED)
67 #endif
68
69 #include "mapFile.h"
70
71 /* posix mmap */
72
73 MapFile::MapFile() {
74 fileString=0;
75 _pos=0;
76 mapsize=0;
77 }
78
79 MapFile::MapFile(const char *filename) {
80 errno = 0;
81
82 int f = open(filename, O_RDONLY);
83 if ((f < 0) || (errno)) {
84 fprintf(stderr, "Couldn't open '%s'\n", filename);
85 perror("open");
86 exit(1);
87 }
88
89 struct stat sb;
90 fstat(f, &sb);
91 if (errno) {
92 fprintf(stderr, "Couldn't stat '%s'\n", filename);
93 perror("fstat\n");
94 exit(1);
95 }
96
97 if (sizeof(FILE_SIZE_TYPE) > sizeof(void*) &&
98 ((((FILE_SIZE_TYPE)1)<<(8*sizeof(void*)))-1) < sb.st_size) {
99 fprintf(stderr, "Couldn't map '%s'\n", filename);
100 exit(1);
101 }
102
103 mapsize = sb.st_size;
104
105 // fprintf(stderr, "%40.40s: Mapping %lu bytes.\n", filename, mapsize);
106 // fflush(stderr);
107
108 # ifndef __MINGW32__
109 fileString = (char *)mmap(0L, mapsize, PROT_READ, MMAPFLAGS, f, 0);
110 # else
111 fileString = new unsigned char[mapsize];
112 read(f,fileString,mapsize);
113 # endif
114
115 if (errno) {
116 fprintf(stderr, "Couldn't map '%s'\n", filename);
117 perror("mmap");
118 exit(1);
119 }
120
121 _pos=0;
122
123 close(f);
124 }
125
126 void MapFile::createMap(const char *filename) {
127 errno = 0;
128
129 int f = open(filename, O_RDONLY);
130 if ((f < 0) || (errno)) {
131 fprintf(stderr, "Couldn't open '%s'\n", filename);
132 perror("open");
133 exit(1);
134 }
135
136 struct stat sb;
137 fstat(f, &sb);
138 if (errno) {
139 fprintf(stderr, "Couldn't stat '%s'\n", filename);
140 perror("fstat\n");
141 exit(1);
142 }
143
144 if (sizeof(FILE_SIZE_TYPE) > sizeof(void*) &&
145 ((((FILE_SIZE_TYPE)1)<<(8*sizeof(void*)))-1) < sb.st_size) {
146 fprintf(stderr, "Couldn't map '%s'\n", filename);
147 exit(1);
148 }
149
150 mapsize = sb.st_size;
151
152 // fprintf(stderr, "%40.40s: Mapping %lu bytes.\n", filename, mapsize);
153 // fflush(stderr);
154
155 # ifndef __MINGW32__
156 fileString = (char *)mmap(0L, mapsize, PROT_READ, MMAPFLAGS, f, 0);
157 # else
158 fileString = new unsigned char[mapsize];
159 read(f,fileString,mapsize);
160 # endif
161
162 if (errno) {
163 fprintf(stderr, "Couldn't map '%s'\n", filename);
164 perror("mmap");
165 exit(1);
166 }
167
168 _pos=0;
169
170 close(f);
171 }
172
173 MapFile::~MapFile() {
174 if (fileString) {
175 #ifdef __CYGWIN__
176 (void)munmap((char*)fileString, mapsize);
177 #elif defined(__MINGW32__)
178 delete [] fileString;
179 #else
180 (void)munmap(fileString, mapsize);
181 #endif
182 }
183 }
184
185 int MapFile::setPos(const FILE_POSITION_TYPE pos) {
186 _pos=pos;
187 return (pos>=mapsize);
188 }