aboutsummaryrefslogtreecommitdiff
path: root/unix2dos.c
blob: 1415691b9340ba2485fc8752056059554bace432 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*  Copyright 1996,1997,1999,2001-2003,2008,2009,2021 Alain Knaff.
 *  This file is part of mtools.
 *
 *  Mtools is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Mtools is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "sysincludes.h"
#include "msdos.h"
#include "mtools.h"
#include "codepage.h"

#define U2D_BUFSIZE 4096

typedef struct Filter_t {
	struct Stream_t head;

	char buffer[U2D_BUFSIZE];
	
	size_t readBytes; /* how many bytes read into buffer */
	size_t bufPos; /* position in buffer */

	bool pendingNl;
	bool eof;
} Filter_t;

/* Add CR before NL, and 0x1a at end of file */
static ssize_t read_filter(Stream_t *Stream, char *output, size_t len)
{
	DeclareThis(Filter_t);
	size_t i;
	
	if(This->eof)
		return 0;
	
	for(i=0; i < len && !This->eof; i++) {
		char c;
		if(This->pendingNl) {
			c='\n';
			This->pendingNl=false;
		} else {
			if(This->bufPos == This->readBytes) {
				ssize_t ret = READS(This->head.Next,
						    This->buffer,
						    U2D_BUFSIZE);
				if(ret < 0) {
					/* an error */
					/* If we already have read some data,
					 * first return count of that data
					 * before returning error */
					if(i == 0)
						return -1;
					else
						break;
				}
				This->readBytes = (size_t) ret;
				This->bufPos = 0;
			}

			if(This->bufPos == This->readBytes) {
				/* Still at end of buffer, must be end
				   of file */
				c='\032';
				This->eof=true;
			} else {
				c = This->buffer[This->bufPos++];
				if(c == '\n') {
					This->pendingNl=true;
					c = '\r';
				}
			}
		}
		output[i]=c;
	}

	return (ssize_t) i;
}

static Class_t FilterClass = {
	read_filter,
	0,
	0,
	0,
	0, /* flush */
	0,
	0, /* set geometry */
	get_data_pass_through,
	0,
	0, /* get_dosconvert */
	0  /* discard */
};

Stream_t *open_unix2dos(Stream_t *Next, int convertCharset UNUSEDP)
{
	Filter_t *This;

	This = New(Filter_t);
	if (!This)
		return NULL;
	init_head(&This->head, &FilterClass, Next);

	This->readBytes = This->bufPos = 0;
	This->pendingNl = false;
	This->eof = false;

	return &This->head;
}