]> git.tdb.fi Git - gldbg.git/blobdiff - source/gldump.c
Packetize the command stream for more robustness
[gldbg.git] / source / gldump.c
index 3a6707b962abd6e7cf8b058809067b28a781602a..79896c2fa4bad9c902726e949d03015b899160ae 100644 (file)
@@ -7,43 +7,54 @@ Distributed under the GPL
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
 #include "glprint.h"
 
 int main(int argc, char **argv)
 {
-       FILE *in;
+       int fd;
+       struct stat st;
        GlDecoder *dec;
-       char *buf;
-       unsigned size;
-       unsigned start;
-       unsigned end;
+       char *addr;
+       char *end;
+       char *ptr;
 
-       in = fopen(argv[1], "r");
+       if(argc<2)
+       {
+               fprintf(stderr, "Usage: %s <dumpfile>\n", argv[0]);
+               return 1;
+       }
+
+       fd = open(argv[1], O_RDONLY);
+       if(fd==-1)
+       {
+               perror("open");
+               return 1;
+       }
+       fstat(fd, &st);
+       addr = (char *)mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+       end = addr+st.st_size;
+       ptr = addr;
 
        dec = glprint_new(NULL, 0);
 
-       size = 16384;
-       buf = (char *)malloc(size);
-       start = 0;
-       end = 0;
-       while(1)
+       while(ptr<end)
        {
-               if(start>size/2)
+               int ret;
+               ret = gldecoder_decode(dec, ptr, end-ptr);
+               if(ret<0)
                {
-                       memmove(buf, buf+start, end-start);
-                       end -= start;
-                       start = 0;
-               }
-               if(end<size && !feof(in))
-                       end += fread(buf+end, 1, size-end, in);
-               else if(start==end)
+                       fprintf(stderr, "Decode error\n");
                        break;
-               start += gldecoder_decode(dec, buf+start, end-start);
+               }
+               ptr += ret;
                printf("%s\n", glprint_get_buffer(dec));
        }
-       fclose(in);
-       free(buf);
+       munmap(addr, st.st_size);
+       close(fd);
        gldecoder_delete(dec);
 
        return 0;