unoidl2

Unnamed repository; edit this file to name it for gitweb.
git clone https://logand.com/git/unoidl2.git/
Log | Files | Refs

javasplit.c (2634B)


      1 /* javasplit -- split output of unoidl2java into individual files */
      2 /*
      3    This file is part of unoidl2.
      4 
      5    unoidl2 is free software: you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation, either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    unoidl2 is distributed in the hope that it will be useful,
     11    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with unoidl2.  If not, see <http://www.gnu.org/licenses/>.
     17 */
     18 
     19 #include <unistd.h>
     20 #include <stdlib.h>
     21 #include <string.h>
     22 #include <fcntl.h>
     23 
     24 enum {
     25   BLEN = 10240
     26 };
     27 
     28 static void write_char(char x) {
     29   write(1, &x, sizeof(char));
     30 }
     31 
     32 static void write_string(char *x) {
     33   write(1, x, strlen(x));
     34 }
     35 
     36 static void panic(int code, char *msg) {
     37   write(1, msg, strlen(msg));
     38   exit(code);
     39 }
     40 
     41 static int line(char *buf, char *end) {
     42   int n = 0;
     43   while(buf < end) {
     44     char c;
     45     int m = read(0, &c, sizeof(char));
     46     if(m <= 0) break;
     47     if(m != sizeof(char)) panic(1, "error reading line");
     48     if('\n' == c) break;
     49     *buf++ = c;
     50     n++;
     51   }
     52   *buf = 0;
     53   return n;
     54 }
     55 
     56 static int head(char *what, char *buf, char *end) {
     57   while(*what && buf < end) {
     58     if(*what++ != *buf++) return 0;
     59   }
     60   return !*what;
     61 }
     62 
     63 static char *find(char what, char *buf, char *end) {
     64   for(; buf < end; buf++)
     65     if(what == *buf) return buf;
     66   return NULL;
     67 }
     68 
     69 static void concat2(char *buf, char *end, char *a, char *b) {
     70   while(*a) {
     71     if(end <= buf) goto panic;
     72     *buf++ = *a++;
     73   }
     74   while(*b) {
     75     if(end <= buf) goto panic;
     76     *buf++ = *b++;
     77   }
     78   if(end <= buf) goto panic;
     79   *buf = 0;
     80   return;
     81  panic:
     82   panic(2, "buffer too small");
     83 }
     84 
     85 char *marker = "///--- ";
     86 
     87 int main() {
     88   char buf[BLEN];
     89   char *end = buf + BLEN;
     90   int fd = 1;
     91   int n;
     92   while(0 < (n = line(buf, end))) {
     93     if(BLEN <= n + 1)
     94       panic(3, "line too long");
     95     char *e = buf + n;
     96     if(head(marker, buf, e)) {
     97       char buf2[BLEN];
     98       char *end2 = buf2 + BLEN;
     99       char *path = buf + 7; // strlen(marker)
    100       concat2(buf2, end2, "mkdir -p ", path);
    101       char *name = find(' ', buf2 + 9, end2); // strlen("mkdir -p ")
    102       *name++ = 0;
    103       system(buf2);
    104       if(1 != fd)
    105         close(fd);
    106       char *space = find(' ', buf + 7, end);
    107       *space = '/';
    108       fd = creat(path, 0644);
    109     } else {
    110       *e = '\n';
    111       write(fd, buf, n + 1);
    112     }
    113   }
    114   return 0;
    115 }