aboutsummaryrefslogtreecommitdiffstats
path: root/src/c_subst.c
blob: 3fce51f1a1e22476cd43e77d076dcdba02f554d5 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * ========================
 * =====    ===============
 * ======  ================
 * ======  ================
 * ======  ====   ====   ==
 * ======  ===     ==  =  =
 * ======  ===  =  ==     =
 * =  ===  ===  =  ==  ====
 * =  ===  ===  =  ==  =  =
 * ==     =====   ====   ==
 * ========================
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 2020 Joe
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the organization nor the
 *    names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY JOE ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL JOE BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * unixize: src/c_subst.c
 * 2020-11-08 02:50
 * Joe
 *
 * This is were we subsitute the new names and return them into a
 * new lfiles_s linked list.
 */

#include <sys/param.h>

#include <ctype.h>
#include <stddef.h>
#include <string.h>

#include "c_lfiles.h"
#include "c_subst.h"
#include "c_unixize.h"
#include "u_utils.h"

#include <stdio.h>
#include <unistd.h>

static void
c_ext_subst
(char					filename[],
 const unsigned char	cxx)
{
	const size_t len = strlen(filename);

	if (cxx == 0 || len < 4) {
		return;
	}
	if (
		filename[len - 4] == '.' &&
		filename[len - 3] == 't' &&
		filename[len - 2] == 'x' &&
		filename[len - 1] == 't'
	) {
		filename[len - 4] = 0x00;
	}
	else if (
		filename[len - 4] == '.' &&
		filename[len - 3] == 'a' &&
		filename[len - 2] == 's' &&
		filename[len - 1] == 'm'
	) {
		memcpy(filename + len - 3, "S", 2 * sizeof(char));
	}
	else if (
		filename[len - 4] == '.' &&
		filename[len - 3] == 'c' &&
		filename[len - 2] == 'p' &&
		filename[len - 1] == 'p'
	) {
		if (cxx == 1) {
			memcpy(filename + len - 3, "cc", 3 * sizeof(char));
		}
		else if (cxx == 2) {
			memcpy(filename + len - 3, "cxx", 4 * sizeof(char));
		}
	}
	else if (
		filename[len - 4] == '.' &&
		filename[len - 3] == 'h' &&
		filename[len - 2] == 'p' &&
		filename[len - 1] == 'p'
	) {
		if (cxx == 1) {
			memcpy(filename + len - 3, "hh", 3 * sizeof(char));
		}
		else if (cxx == 2) {
			memcpy(filename + len - 3, "hxx", 4 * sizeof(char));
		}
	}
}

static void
c_classic_subst
(char			filename[],
 const bool_t	hyphen)
{
	char *p;
	const char sep   = (hyphen == FALSE) ? ('_') : ('-');
	const char c_sep = (hyphen == FALSE) ? ('-') : ('_');

	p = (char*)filename;
	while (*p != 0x00) {
		if (*p == c_sep) {
			*p = sep;
			c_classic_subst(filename, hyphen);
		}
		if (*p == sep && *(p + 1) == sep) {
			memmove(p, p + 1, strlen(p + 1) * sizeof(char));
			c_classic_subst(filename, hyphen);
		}
		if (*p == ' ') {
			*p = sep;
			c_classic_subst(filename, hyphen);
		}
		if (
			isalnum(*p) == 0 &&
			u_ischarset(*p, C_CHARSET_VALID) == FALSE
			) {
		}
		p++;
	}
}

static void
c_num_prefix_subst(char filename[])
{
	char *p;
	char *p_probe;

	p = filename;
	while (*p != 0x00) {
		if (isdigit(*p) == 0) {
			if (*p != '.') {
				return;
			}
			else {
				p_probe = p + 1;
				while (*p_probe != 0x00 && *p_probe == '.') {
					p_probe++;
				}
				if (*p_probe != 0x00) {
					*p = '_';
				}
				return;
			}
		}
		p++;
	}
}

static void
c_subst_current
(char					new_fname[],
 const char				og_fname[],
 const bool_t			hyphen,
 const unsigned char	cxx)
{
	unsigned char* p;

	strlcpy(new_fname, og_fname, MAXPATHLEN);
	p = (unsigned char*)new_fname;
	while (*p != 0x00) {
		*p = tolower(*p);
		p++;
	}
	c_ext_subst(new_fname, cxx);
	c_num_prefix_subst(new_fname);
	/* c_exascii_subst(new_fname, hyphen); */
	/* c_unicode_subst(new_fname, hyphen); */
	c_classic_subst(new_fname, hyphen);
}

struct lfiles_s*
c_subst_filenames
(struct lfiles_s*		og_head,
 const bool_t			hyphen,
 const unsigned char	cxx)
{
	struct lfiles_s* dup_head;
	struct lfiles_s* link;
	struct lfiles_s* origin;
	char tmp[MAXPATHLEN];

	dup_head = NULL;
	link = NULL;
	origin = og_head;
	while (origin != NULL) {
		c_subst_current(tmp, origin->filename, hyphen, cxx);
		link = c_lfiles_new(tmp, origin->filetype);
		if (link == NULL) {
			u_dump_errno();
			c_lfiles_clear(&dup_head);
			return (NULL);
		}
		c_lfiles_add_back(&dup_head, link);
		origin = origin->next;
	}
	return (dup_head);
}