Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactored. Auto-rebuild feature #1

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
40 changes: 28 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
CC=gcc
CFLAGS=-O3 -lz
TARGETS=seekgzip
PYTHON_TARGETS=export_python.cpp seekgzip.py
SWIG=swig
PYTHON=python

all: $(TARGETS)
LDFLAGS=-lz

USR_BIN_TARGETS=seekgzip
USR_LIB_TARGETS=libseekgzip.so
USR_INC_TARGETS=seekgzip.h
PHONY_TARGETS=.python

TARGETS=$(USR_BIN_TARGETS) $(USR_LIB_TARGETS) $(PHONY_TARGETS)

all: $(TARGETS)
clean:
rm $(TARGETS)
rm -rf $(TARGETS)
rm -rf export_python.cpp

install:
mkdir -p $(DESTDIR)/$(EPREFIX)/usr/bin/ $(DESTDIR)/$(EPREFIX)/usr/lib/ $(DESTDIR)/$(EPREFIX)/usr/include/seekgzip/
cp $(USR_BIN_TARGETS) $(DESTDIR)/$(EPREFIX)/usr/bin/
cp $(USR_LIB_TARGETS) $(DESTDIR)/$(EPREFIX)/usr/lib/
cp $(USR_INC_TARGETS) $(DESTDIR)/$(EPREFIX)/usr/include/seekgzip/
test -f .python && $(PYTHON) setup.py install || exit 0

python: $(PYTHON_TARGETS)
seekgzip: seekgzip.c main.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ seekgzip.c main.c

clean-python:
rm $(PYTHON_TARGETS)
libseekgzip.so: seekgzip.c
$(CC) $(CFLAGS) $(LDFLAGS) -fPIC -shared -o $@ $<

seekgzip: seekgzip.c
$(CC) $(CFLAGS) -o $@ -DBUILD_UTILITY $<
.python: swig.i export_cpp.h export_cpp.cpp setup.py
$(SWIG) -c++ -python -o export_python.cpp swig.i
$(PYTHON) setup.py build
touch $@

$(PYTHON_TARGETS): export.h export.i
swig -c++ -python -o export_python.cpp export.i
12 changes: 4 additions & 8 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ streams.


* HOW TO BUILD THE UTILITY
$ make
$ make all

* HOW TO INSTALL THE UTILITY
$ make install

* HOW TO USE THE UTILITY

Expand All @@ -29,17 +31,11 @@ This builds an index file for the specified gzip file ${FILE}. This
utility creates an index file ${FILE}.idx

(2) Reading the data in the specified range
$ seekgzip <FILE> [BEGIN:END]
$ seekgzip <FILE> [BEGIN-END]
This reads the data in the gzip file ${FILE} from the offset ${BEGIN}
to ${END}, and outputs the data to STDOUT.


* HOW TO BUILD PYTHON MODULE
$ make python
$ python setup.py --build_ext
$ python setup.py install


* COPYRIGHT AND LICENSING INFORMATION

This program is distributed under the zlib license.
Expand Down
6 changes: 3 additions & 3 deletions export.cpp → export_cpp.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <string>
#include <stdexcept>
#include "seekgzip.h"
#include "export.h"
#include "export_cpp.h"

static std::string error_string(int errorcode)
{
Expand Down Expand Up @@ -31,9 +31,9 @@ static std::string error_string(int errorcode)
reader::reader(const char *filename)
{
int err = 0;
seekgzip_t* sgz = seekgzip_open(filename, &err);
seekgzip_t* sgz = seekgzip_open(filename, 0);
m_obj = sgz;
if (sgz == NULL) {
if ( (err = seekgzip_error(sgz)) != SEEKGZIP_SUCCESS){
throw std::invalid_argument(error_string(err));
}
}
Expand Down
File renamed without changes.
135 changes: 135 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* SeekGzip utility/library.
*
* Copyright (c) 2010-2011, Naoaki Okazaki
* All rights reserved.
*
* For conditions of distribution and use, see copyright notice in README
* or zlib.h.
*
* The core algorithm for random access originates from zran.c in zlib/gzip
* distribution. This code simply implements a data structure and algorithm
* for indices, wraps the functionality of random access as a library, and
* provides a command-line utility.
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "seekgzip.h"

#define CHUNK 16384 /* file input buffer size */

static void seekgzip_perror(int ret)
{
switch (ret) {
case SEEKGZIP_ERROR:
fprintf(stderr, "ERROR: An unknown error occurred.\n");
break;
case SEEKGZIP_OPENERROR:
fprintf(stderr, "ERROR: Failed to open a file.\n");
break;
case SEEKGZIP_READERROR:
fprintf(stderr, "ERROR: Failed to read a file.\n");
break;
case SEEKGZIP_WRITEERROR:
fprintf(stderr, "ERROR: Failed to write a file.\n");
break;
case SEEKGZIP_DATAERROR:
fprintf(stderr, "ERROR: The file is corrupted.\n");
break;
case SEEKGZIP_OUTOFMEMORY:
fprintf(stderr, "ERROR: Out of memory.\n");
break;
case SEEKGZIP_IMCOMPATIBLE:
fprintf(stderr, "ERROR: The imcompatible file.\n");
break;
case SEEKGZIP_ZLIBERROR:
fprintf(stderr, "ERROR: An error occurred in zlib.\n");
break;
}
}

int main(int argc, char *argv[])
{
int ret = 0;

if (argc != 3) {
printf("This utility manages an index for random (seekable) access to a gzip file.\n");
printf("USAGE:\n");
printf(" %s -b <FILE>\n", argv[0]);
printf(" Build an index file \"$FILE.idx\" for the gzip file $FILE.\n");
printf(" %s <FILE> [BEGIN-END]\n", argv[0]);
printf(" Output the content of the gzip file $FILE of offset range [BEGIN-END].\n");
return 0;

} else if (strcmp(argv[1], "-b") == 0) {
const char *target = argv[2];

printf("Building an index: %s.idx\n", target);
printf("Filesize up to: %d bit\n", (int)sizeof(off_t) * 8);
printf("WARNING: if program fail to write index to file, it would silently ignore that\n");

seekgzip_t* zs = seekgzip_open(target, 0);
if ((ret = seekgzip_error(zs)) != SEEKGZIP_SUCCESS) {
seekgzip_perror(ret);
return 1;
}
seekgzip_close(zs);
return 0;

} else {
char *arg = argv[2], *p = NULL;
off_t begin = 0, end = (off_t)-1;
seekgzip_t* zs = seekgzip_open(argv[1], 0);
if (zs == NULL || seekgzip_error(zs) != SEEKGZIP_SUCCESS) {
fprintf(stderr, "ERROR: Failed to open the index file.\n");
return 1;
}

p = strchr(arg, '-');
if (p == NULL) {
begin =(off_t)strtoull(arg, NULL, 10);
end = begin+1;
} else if (p == arg) {
begin = 0;
end = (off_t)strtoull(p+1, NULL, 10);
} else if (p == arg + strlen(arg) - 1) {
*p = 0;
begin = (off_t)strtoull(arg, NULL, 10);
} else {
*p++ = 0;
begin =(off_t)strtoull(arg, NULL, 10);
end =(off_t)strtoull(p, NULL, 10);
}

seekgzip_seek(zs, begin);

while (begin < end) {
int read;
char buffer[CHUNK];
off_t size = (end - begin);
if (CHUNK < size) {
size = CHUNK;
}
read = seekgzip_read(zs, buffer, (int)size);
if (0 < read) {
begin += read;

if(fwrite(buffer, read, sizeof(char), stdout) == 0)
continue;
} else if (read == 0) {
break;
} else {
fprintf(stderr, "ERROR: An error occurred while reading the gzip file.\n");
ret = 1;
break;
}
}

seekgzip_close(zs);
return ret;
}
}

Loading