-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
147 lines (122 loc) · 3.81 KB
/
Copy pathmain.c
File metadata and controls
147 lines (122 loc) · 3.81 KB
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
/**
* @file main.c
* @author Amaury Couste <ben@bantertrain.com>
* @since 0.0
*
* @section LICENSE
*
* This file is part of diffr-c.
*
* diffr-c 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.
* diffr-c 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 diffr-c. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "diffr/util/instruction/Instruction.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
instructions_foreach(gpointer data, gpointer user_data) {
instruction *i = (instruction*) data;
GPtrArray *origlines = (GPtrArray*) user_data;
if (i->type == COPY) {
long count = i->start - 1;
long stop = i->end - 1;
while (count <= stop) {
gchar *line = g_ptr_array_index(origlines, count);
if (!line) {
fprintf(stderr, "Malformed patch file, aborting!\n");
exit(-3);
}
fprintf(stdout, "%s", line);
count++;
}
} else if (i->type == INSERT) {
gchar *line = i->text->str;
++line; // FIXME: find another way to get rid of '> '
++line;
fprintf(stdout, "%s", line);
} else {
printf("not sure what's going on, exiting!");
exit(-4);
}
}
GIOChannel*
open_channel(char *filename) {
GIOChannel *channel = NULL;
GError *err = NULL;
channel = g_io_channel_new_file(filename, "r", &err);
if (err) {
fprintf(stderr, "Could not open file %s, aborting!\n", filename);
exit(-1);
}
return channel;
}
GSList*
parse_instructions(char *filename) {
GIOChannel *channel = open_channel(filename);
GString *newstr = g_string_new("dummy"); // FIXME
gsize terminator_pos;
GSList *l = NULL;
GError *err = NULL;
while (g_io_channel_read_line_string(
channel, newstr, &terminator_pos, &err) != G_IO_STATUS_EOF) {
instruction *i = NULL;
if (*(newstr->str) == '>') {
i = create_insert_instruction(newstr);
} else {
const gchar delimiter = ',';
gchar **nums = g_strsplit(newstr->str, &delimiter, 2);
gchar *start = *nums;
gchar *end = *(++nums);
g_strstrip(start);
g_strstrip(end);
i = create_copy_instruction((long) atoi(start), (long) atoi(end));
g_free(start);
g_free(end);
}
l = g_slist_append(l, i);
}
return l;
}
GPtrArray*
parse_input(char *filename) {
GIOChannel *channel = open_channel(filename);
GPtrArray *lines = g_ptr_array_new();
GString *tmp = g_string_new("tmp");
gsize terminator_pos;
GError *err = NULL;
while (g_io_channel_read_line_string(
channel, tmp, &terminator_pos, &err) != G_IO_STATUS_EOF) {
gchar *line = g_strdup(tmp->str);
g_ptr_array_add(lines, line);
}
return lines;
}
void
do_patch(GSList *instructions, GPtrArray *origlines) {
g_slist_foreach(instructions, instructions_foreach, origlines);
}
int
main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: patch <file> <patchfile>\n");
exit(-1);
}
argv++;
char *origfile = *argv;
GPtrArray *origlines = parse_input(origfile);
argv++;
char *patchfile = *argv;
GSList *instructions = parse_instructions(patchfile);
do_patch(instructions, origlines);
return 0;
}