Debian Codegraph @

src/ffindex_from_tsv.c

raw
1 /*
2 * FFindex
3 * written by Andreas Hauser <[email protected]>.
4 * Please add your name here if you distribute modified versions.
5 *
6 * FFindex is provided under the Create Commons license "Attribution-ShareAlike
7 * 4.0", which basically captures the spirit of the Gnu Public License (GPL).
8 *
9 * See:
10 * http://creativecommons.org/licenses/by-sa/4.0/
11 */
12
13 #define _GNU_SOURCE 1
14 #define _LARGEFILE64_SOURCE 1
15 #define _FILE_OFFSET_BITS 64
16
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25
26 #include "ffindex.h"
27 #include "ffutil.h"
28
29 #define MAX_FILENAME_LIST_FILES 4096
30
31
32 void usage(char *program_name)
33 {
34 fprintf(stderr, "USAGE: %s -v | [-s] [-k INDEX] data_filename index_filename fasta_filename\n"
35 "\t-k INDEX\tfield index from 1 which is used as key (default = 1)\n"
36 "\t-s\tsort index file (usually you want this)\n"
37 FFINDEX_COPYRIGHT,
38 program_name);
39 }
40
41 char* tsv_scan_line_for_field(char *tsv_current, int field_selected_index, char **field_start, size_t *field_length)
42 {
43 *field_start = tsv_current;
44 *field_length = 0;
45 int field_index = 1;
46 for(int c = *tsv_current; c != '\n'; c = *(tsv_current++))
47 {
48 if(c == '\t')
49 {
50 if(field_index == field_selected_index)
51 *field_length = tsv_current - *field_start - 1;
52 field_index++;
53 if(field_index == field_selected_index)
54 *field_start = tsv_current;
55 }
56 }
57 if(*field_length == 0)
58 *field_length = tsv_current - *field_start - 1;
59 return tsv_current;
60 }
61
62 int main(int argn, char **argv)
63 {
64 int sort = 0, version = 0;
65 int opt, err = EXIT_SUCCESS;
66 int user_selected_field_index = 1;
67 while ((opt = getopt(argn, argv, "svk:")) != -1)
68 {
69 switch (opt)
70 {
71 case 'k':
72 user_selected_field_index = optind;
73 break;
74 case 's':
75 sort = 1;
76 break;
77 case 'v':
78 version = 1;
79 break;
80 default:
81 usage(argv[0]);
82 return EXIT_FAILURE;
83 }
84 }
85
86 if(version == 1)
87 {
88 /* Don't you dare running it on a platform where byte != 8 bits */
89 printf("%s version %.2f, off_t = %zd bits\n", argv[0], FFINDEX_VERSION, sizeof(off_t) * 8);
90 return EXIT_SUCCESS;
91 }
92
93 if(argn - optind < 3)
94 {
95 usage(argv[0]);
96 return EXIT_FAILURE;
97 }
98
99
100 char *data_filename = argv[optind++];
101 char *index_filename = argv[optind++];
102 char *tsv_filename = argv[optind++];
103 FILE *data_file, *index_file;
104 size_t offset = 0;
105
106 /* open ffindex */
107 err = ffindex_index_open(data_filename, index_filename, "w", &data_file, &index_file, &offset);
108 if(err != EXIT_SUCCESS)
109 return err;
110
111 FILE* tsv_file = fopen(tsv_filename, "r");
112 if(tsv_file == NULL) { perror(tsv_filename); return EXIT_FAILURE; }
113
114 size_t tsv_size;
115 char* tsv_data = ffindex_mmap_data(tsv_file, &tsv_size);
116 char* tsv_current = tsv_data;
117 char* tsv_part_begin = tsv_data;
118 char* tsv_last = tsv_data + tsv_size;
119
120 char field_current[FFINDEX_MAX_ENTRY_NAME_LENTH + 1]; // + seperator
121 size_t field_current_length = 0; // + seperator
122
123 char* tsv_selected_field_start = NULL;
124 size_t tsv_selected_field_length = 0;
125 tsv_current = tsv_scan_line_for_field(tsv_current, user_selected_field_index, &tsv_selected_field_start, &tsv_selected_field_length);
126 strncpy(field_current, tsv_selected_field_start, tsv_selected_field_length); //XXX
127 field_current_length = tsv_selected_field_length;
128 field_current[field_current_length] = '\0';
129 while(tsv_current < tsv_last)
130 {
131 char* tsv_next;
132 tsv_next = tsv_scan_line_for_field(tsv_current, user_selected_field_index, &tsv_selected_field_start, &tsv_selected_field_length);
133 if((tsv_selected_field_length != field_current_length ||
134 strncmp(field_current, tsv_selected_field_start, tsv_selected_field_length) != 0)) // XXX got a new field value
135 {
136 ffindex_insert_memory(data_file, index_file, &offset, tsv_part_begin, tsv_current - tsv_part_begin, field_current);
137 strncpy(field_current, tsv_selected_field_start, tsv_selected_field_length); //XXX
138 field_current_length = tsv_selected_field_length;
139 field_current[field_current_length] = '\0';
140 tsv_part_begin = tsv_current;
141 }
142 tsv_current = tsv_next;
143 }
144 ffindex_insert_memory(data_file, index_file, &offset, tsv_part_begin, tsv_current - tsv_part_begin, field_current);
145 strncpy(field_current, tsv_selected_field_start, tsv_selected_field_length); //XXX
146 field_current_length = tsv_selected_field_length;
147 field_current[field_current_length] = '\0';
148
149 fclose(data_file);
150
151 /* Sort the index entries and write back */
152 if(sort)
153 {
154 rewind(index_file);
155 ffindex_index_t* index = ffindex_index_parse(index_file, 0);
156 if(index == NULL)
157 {
158 perror("ffindex_index_parse failed");
159 exit(EXIT_FAILURE);
160 }
161 fclose(index_file);
162 ffindex_sort_index_file(index);
163 index_file = fopen(index_filename, "w");
164 if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; }
165 err += ffindex_write(index, index_file);
166 }
167
168 return err;
169 }
170
171 /* vim: ts=2 sw=2 et: */