Context algorithm
Semi-predictive context algorithm implementation
 All Data Structures Files Functions Variables Typedefs Macros Pages
mapfile.c
Go to the documentation of this file.
1 /* Copyright 2013 Jorge Merlino
2 
3  This file is part of Context.
4 
5  Context is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Context is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Context. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include "mapfile.h"
24 #include "debug.h"
25 
26 #ifdef WIN32
27 
28 #include <io.h>
29 
30 
38 static int fileOpen(char *name, Uint *textlen, BOOL writefile)
39 {
40  int fd;
41  struct stat buf;
42 
43  if((fd = _open(name,(writefile) ? O_RDWR : O_RDONLY)) == -1)
44  {
45  fprintf(stderr,"fileOpen: Cannot open \"%s\"",name);
46  return -1;
47  }
48  if(fstat(fd,&buf) == -1)
49  {
50  fprintf(stderr, "file \"%s\": fstat(fd = %d) failed",name,fd);
51  return -2;
52  }
53  *textlen = (Uint) buf.st_size;
54  return fd;
55 }
56 
57 
68 static void * fileParts(int fd, Uint offset, Uint len, HANDLE *hndl, BOOL writemap) {
69  void * ret;
70 
71  *hndl = CreateFileMapping (
72  (HANDLE)_get_osfhandle(fd),
73  NULL,
74  (writemap ? PAGE_READWRITE : PAGE_READONLY),
75  0,
76  0,
77  NULL);
78 
79  if (*hndl == NULL) {
80  fprintf(stderr, "fileParts(fd = %d, left = %ld, len = %ld, %s) failed",fd,
81  (long) offset, (long) len, writemap ? "writable map" : "readable map");
82  }
83 
84  ret = (void *)MapViewOfFile (
85  *hndl,
86  (writemap ? FILE_MAP_WRITE : FILE_MAP_READ),
87  0,
88  offset,
89  len);
90  if (ret == NULL) {
91  fprintf(stderr, "fileParts(fd = %d, left = %ld, len = %ld, %s) failed",fd,
92  (long) offset, (long) len, writemap ? "writable map" : "readable map");
93  }
94 
95  return ret;
96 }
97 
98 
108 static void * genfile2String(char *name, Uint *textlen, HANDLE *hndl, BOOL writefile, BOOL writemap) {
109  int fd;
110 
111  fd = fileOpen(name, textlen, writefile);
112  if(fd < 0)
113  {
114  return NULL;
115  }
116  return fileParts(fd, 0, *textlen, hndl, writemap);
117 }
118 
119 
124 void freetextspace(Uchar *text, HANDLE hndl) {
125  if (!(UnmapViewOfFile(text))) {
126  fprintf(stderr, "Unable to unmap file");
127  }
128  if (!CloseHandle(hndl)) {
129  fprintf(stderr, "Unable to close handle");
130  }
131 }
132 
133 
140 void * file2String(char *name, Uint *textlen, HANDLE *hndl) {
141  return genfile2String(name, textlen, hndl, False, False);
142 }
143 
144 
145 #else /* UNIX */
146 
147 #include <sys/mman.h>
148 
149 
157 static int fileOpen(char *name, Uint *textlen, BOOL writefile)
158 {
159  int fd;
160  struct stat buf;
161 
162  if((fd = open(name,(writefile) ? O_RDWR : O_RDONLY)) == -1)
163  {
164  fprintf(stderr,"fileOpen: Cannot open \"%s\"",name);
165  return -1;
166  }
167  if(fstat(fd,&buf) == -1)
168  {
169  fprintf(stderr, "file \"%s\": fstat(fd = %d) failed",name,fd);
170  return -2;
171  }
172  *textlen = (Uint) buf.st_size;
173  return fd;
174 }
175 
176 
185 static void * fileParts(int fd, Uint offset, Uint len, BOOL writemap)
186 {
187  void *addr;
188 
189  addr = mmap((void *) 0, (size_t) len, writemap ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_PRIVATE, fd, (off_t) offset);
190  if(addr == (void *) MAP_FAILED)
191  {
192  fprintf(stderr, "fileParts(fd = %d, left = %ld, len = %ld, %s) failed",fd,
193  (long) offset,(long) len,
194  writemap ? "writable map" : "readable map");
195  return NULL;
196  }
197  return addr;
198 }
199 
200 
209 static void * genfile2String(char *name, Uint *textlen, BOOL writefile, BOOL writemap) {
210  int fd;
211 
212  fd = fileOpen(name, textlen, writefile);
213  if(fd < 0)
214  {
215  return NULL;
216  }
217  return fileParts(fd, 0, *textlen, writemap);
218 }
219 
220 
225 void freetextspace(Uchar *text, Uint textlen) {
226  if (munmap((void *) text, (size_t) textlen) == -1) {
227  fprintf(stderr, "Unable to unmap file");
228  }
229 }
230 
231 
237 void * file2String(char *name, Uint *textlen) {
238  return genfile2String(name, textlen, False, False);
239 }
240 
241 #endif
242 
Uint textlen
Length of the input text in the encoder.
Definition: text.h:23
static void * fileParts(int fd, Uint offset, Uint len, BOOL writemap)
Maps an open file into RAM memory.
Definition: mapfile.c:185
unsigned char Uchar
Unsigned char type.
Definition: types.h:48
Uchar * text
Input text to the encoder.
Definition: text.h:26
unsigned long Uint
Unsigned int type.
Definition: types.h:54
#define BOOL
Boolean data type.
Definition: types.h:92
static void * genfile2String(char *name, Uint *textlen, BOOL writefile, BOOL writemap)
Opens a file and maps it into memory.
Definition: mapfile.c:209
static int fileOpen(char *name, Uint *textlen, BOOL writefile)
Opens a file and reads gets its size.
Definition: mapfile.c:157
#define False
False boolean constant.
Definition: types.h:100
void * file2String(char *name, Uint *textlen)
Opens a file and maps it into memory.
Definition: mapfile.c:237
void freetextspace(Uchar *text, Uint textlen)
Frees the memory used by the mapping of a file.
Definition: mapfile.c:225