1 /* Copyright 1996,1997,1999,2001-2003,2008,2009 Alain Knaff.
2 * This file is part of mtools.
4 * Mtools is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Mtools is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
18 #include "sysincludes.h"
23 typedef struct Filter_t {
34 /* int convertCharset; */
40 /* read filter filters out messy dos' bizarre end of lines and final 0x1a's */
42 static int read_filter(Stream_t *Stream, char *buf, mt_off_t iwhere, size_t len)
44 DeclareThis(Filter_t);
46 unsigned char newchar;
48 off_t where = truncBytes32(iwhere);
50 if ( where != This->unixpos ){
51 fprintf(stderr,"Bad offset\n");
54 if (This->rw == F_WRITE){
55 fprintf(stderr,"Change of transfer direction!\n");
60 ret = READS(This->Next, buf, (mt_off_t) This->dospos, len);
65 for (i=0; i< ret; i++){
72 if (This->convertCharset) newchar = contents_to_unix(newchar);
74 This->lastchar = buf[j++] = newchar;
82 static int write_filter(Stream_t *Stream, char *buf, mt_off_t iwhere,
85 DeclareThis(Filter_t);
89 unsigned char newchar;
91 off_t where = truncBytes32(iwhere);
93 if(This->unixpos == -1)
96 if (where != This->unixpos ){
97 fprintf(stderr,"Bad offset\n");
101 if (This->rw == F_READ){
102 fprintf(stderr,"Change of transfer direction!\n");
108 while(i < 1024 && j < len){
109 if (buf[j] == '\n' ){
117 if (This->convertCharset) newchar = to_dos(newchar);
119 buffer[i++] = newchar;
123 ret = force_write(This->Next, buffer, (mt_off_t) This->dospos, i);
126 if ( ret != (signed int) i ){
127 /* no space on target file ? */
134 static int free_filter(Stream_t *Stream)
136 DeclareThis(Filter_t);
139 /* write end of file */
140 if (This->rw == F_WRITE)
141 return force_write(This->Next, &buffer, (mt_off_t) This->dospos, 1);
146 static Class_t FilterClass = {
151 0, /* set geometry */
152 get_data_pass_through,
156 Stream_t *open_filter(Stream_t *Next, int convertCharset UNUSEDP)
160 This = New(Filter_t);
163 This->Class = &FilterClass;
164 This->dospos = This->unixpos = This->rw = 0;
169 This->convertCharset = convertCharset;
172 return (Stream_t *) This;