xref: /aosp_15_r20/external/mtools/dos2unix.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1 /*  Copyright 1996,1997,1999,2001-2003,2008,2009,2021 Alain Knaff.
2  *  This file is part of mtools.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #include "sysincludes.h"
19 #include "msdos.h"
20 #include "mtools.h"
21 #include "codepage.h"
22 
23 typedef struct Filter_t {
24 	struct Stream_t head;
25 
26 	int mode;
27 	/* int convertCharset; */
28 } Filter_t;
29 
30 /* read filter filters out messy dos' bizarre end of lines and final 0x1a's */
31 
read_filter(Stream_t * Stream,char * buf,size_t len)32 static ssize_t read_filter(Stream_t *Stream, char *buf, size_t len)
33 {
34 	DeclareThis(Filter_t);
35 	size_t i,j;
36 	ssize_t ret;
37 	char newchar;
38 
39 	ret = READS(This->head.Next, buf, len);
40 	if ( ret < 0 )
41 		return ret;
42 
43 	j = 0;
44 	for (i=0; i< (size_t) ret; i++){
45 		if ( buf[i] == '\r' )
46 			continue;
47 		if (buf[i] == 0x1a)
48 			break;
49 		newchar = buf[i];
50 		/*
51 		if (This->convertCharset) newchar = contents_to_unix(newchar);
52 		*/
53 		buf[j++] = newchar;
54 	}
55 
56 	return (ssize_t) j;
57 }
58 
59 static Class_t FilterClass = {
60 	read_filter,
61 	0,
62 	0,
63 	0,
64 	0, /* flush */
65 	0,
66 	0, /* set geometry */
67 	get_data_pass_through,
68 	0,
69 	0, /* get_dosconvert */
70 	0  /* discard */
71 };
72 
open_dos2unix(Stream_t * Next,int convertCharset UNUSEDP)73 Stream_t *open_dos2unix(Stream_t *Next, int convertCharset UNUSEDP)
74 {
75 	Filter_t *This;
76 
77 	This = New(Filter_t);
78 	if (!This)
79 		return NULL;
80 	init_head(&This->head, &FilterClass, Next);
81 	/*
82 	  This->convertCharset = convertCharset;
83 	*/
84 
85 	return &This->head;
86 }
87