Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

brm util, work in progress

  • Loading branch information...
commit 316b8192ec958c3e2d240a3569e0c5e5b6400dc7 1 parent 1b1afc6
Erik Frey authored
64 brp/brp.c
View
@@ -1,64 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <ctype.h>
-
-void showusage() {
- fprintf(stderr, "usage: brp [-i input] output1 output2 output3...\n");
- exit(1);
-}
-
-FILE * try_open(const char * path, const char * flags) {
- FILE * p = fopen(path, flags);
- if (!p) {
- fprintf(stderr, "could not open %s: %s\n", path, strerror(errno));
- exit(1);
- }
- return p;
-}
-
-unsigned int fnv_hash(const char *p, const char *end) {
- unsigned int h = 2166136261UL;
- for (; p != end; ++p)
- h = (h * 16777619) ^ *p;
- return h;
-}
-
-int main(int argc, char * argv[])
-{
- FILE * pin = stdin;
- int i, num_out = 0, out_argc = 1;
- char buf[8192];
- char * delim;
-
- if (argc < 2)
- showusage();
- if (strcmp(argv[1], "-i") == 0) {
- out_argc = 3;
- if (argc < 4)
- showusage();
- pin = try_open(argv[2], "rb");
- }
-
- num_out = argc - out_argc;
- FILE ** pouts = (FILE **) malloc( num_out * sizeof(FILE *) );
- for (i = 0; i != num_out; ++i)
- pouts[i] = try_open(argv[i + out_argc], "wb");
-
- while (fgets(buf, sizeof(buf), pin)) {
- // find the delimiter
- for (delim = buf; !isspace(*delim); ++delim) {}
- // write it to the correct file
- fputs(buf, pouts[fnv_hash(buf, delim) % num_out]);
- }
-
- if (pin != stdin)
- fclose(pin);
-
- for (i = 0; i != num_out; ++i)
- fclose(pouts[i]);
-
- return 0;
-}
-
0  brp/Makefile → brutils/Makefile
View
File renamed without changes
0  brp/README → brutils/README
View
File renamed without changes
50 brutils/brm.c
View
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <ctype.h>
+
+#include "brutils.h"
+
+void showusage() {
+ fprintf(stderr, "usage: brm output column-index input1 input2 input3...\n");
+ fprintf(stderr, " can specify '-' for output, to write to stdout\n");
+ exit(1);
+}
+
+int main(int argc, char * argv[])
+{
+ FILE * pout = stdout;
+ int i, col_index;
+
+ if (argc < 4)
+ showusage();
+ if (strcmp(argv[1], "-") != 0)
+ pout = try_open(argv[1], "wb");
+ col_index = atoi(argv[2]);
+
+ int pins_len = argc - 3;
+ FILE ** pins = (FILE **) malloc( pins_len * sizeof(FILE *) );
+ for (i = 0; i != pins_len; ++i)
+ pins[i] = try_open(argv[i + 3], "rb");
+
+ line_t * plines = (line_t *) malloc( pins_len * sizeof( line_t ) );
+
+ char buf[8192];
+ const char * col_beg, * col_end;
+ while (fgets(buf, sizeof(buf), pin)) {
+ // find the correct column
+ col_beg = buf;
+ find_col(col_index, &col_beg, &col_end);
+ // write it to the correct file
+ fputs(buf, pouts[fnv_hash(col_beg, col_end) % pouts_len]);
+ }
+
+ if (pin != stdin)
+ fclose(pin);
+
+ for (i = 0; i != pouts_len; ++i)
+ fclose(pouts[i]);
+
+ return 0;
+}
45 brutils/brp.c
View
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <ctype.h>
+
+#include "brutils.h"
+
+void showusage() {
+ fprintf(stderr, "usage: brp input column-index output1 output2 output3...\n");
+ fprintf(stderr, " can specify '-' for input, to read from stdin\n");
+ exit(1);
+}
+
+int main(int argc, char * argv[])
+{
+ FILE * pin = stdin;
+ int i, col_index;
+
+ if (argc < 4)
+ showusage();
+ if (strcmp(argv[1], "-") != 0)
+ pin = try_open(argv[1], "rb");
+ col_index = atoi(argv[2]);
+
+ int pouts_len = argc - 3;
+ FILE ** pouts = (FILE **) malloc( pouts_len * sizeof(FILE *) );
+ for (i = 0; i != pouts_len; ++i)
+ pouts[i] = try_open(argv[i + 3], "wb");
+
+ line_t line;
+ while (fgets(line.buf, sizeof(line.buf), pin)) {
+ if ( find_col(col_index, &line) ) // if this string has the requisite number of columns
+ fputs(line.buf, pouts[fnv_hash(line.col_beg, line.col_end) % pouts_len]); // write it to the correct file
+ }
+
+ if (pin != stdin)
+ fclose(pin);
+
+ for (i = 0; i != pouts_len; ++i)
+ fclose(pouts[i]);
+
+ return 0;
+}
+
38 brutils/brutils.h
View
@@ -0,0 +1,38 @@
+#ifndef __BR_UTILS_H__
+#define __BR_UTILS_H__
+
+FILE * try_open(const char * path, const char * flags) {
+ FILE * p = fopen(path, flags);
+ if (!p) {
+ fprintf(stderr, "could not open %s: %s\n", path, strerror(errno));
+ exit(1);
+ }
+ return p;
+}
+
+unsigned int fnv_hash(const char *p, const char *end) {
+ unsigned int h = 2166136261UL;
+ for (; p != end; ++p)
+ h = (h * 16777619) ^ *p;
+ return h;
+}
+
+typedef struct
+{
+ char buf[8192];
+ char * col_beg;
+ char * col_end;
+} line_t;
+
+bool find_col(int col, line_t * pline) {
+ for (pline->col_beg = pline->buf; col != 0; ++*pline->col_beg) {
+ if ( isspace(*pline->col_beg) )
+ --col;
+ if ( *pline->col_beg == '\n' )
+ return false;
+ }
+ for (pline->col_end = pline->col_beg; !isspace(*pline->col_end); ++*pline->col_end) {}
+ return true;
+}
+
+#endif // __BR_UTILS_H__
Please sign in to comment.
Something went wrong with that request. Please try again.