[Bio] / FastaParse / SimParse.xs Repository:
ViewVC logotype

View of /FastaParse/SimParse.xs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (download) (annotate) (vendor branch)
Tue Nov 20 22:35:20 2007 UTC (12 years ago) by olson
Branch: MAIN, xx
CVS Tags: xxy, HEAD
Changes since 1.1: +0 -0 lines
Import of C-based fasta parser

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"

#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>

static char fbuf[10485760];
static char idbuf[1024];

static int fd;
static char *ptr, *end;

MODULE = SimParse		PACKAGE = SimParse		

int open(file)
	const char *	file
CODE:
{
    fd = open(file, O_RDONLY);
    if (fd < 0)
    {
	perror("open");
	RETVAL = 0;
	goto end;
    }

    struct stat st;
    if (fstat(fd, &st) < 0)
    {
	perror("stat");
	RETVAL = 0;
	goto end;
    }

    ptr = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
    if (ptr == 0)
    {
	perror("mmap");
	RETVAL = 0;
	goto end;
    }
    
    if (madvise(ptr, st.st_size, MADV_SEQUENTIAL) < 0)
    {
	perror("madvise");
	RETVAL = 0;
	goto end;
    }

    end = ptr + st.st_size;

    RETVAL = 1;

    end:
;
}
OUTPUT:
RETVAL

SV *
next()
CODE:
{
    if (ptr >= end)
    {
	RETVAL = PL_sv_undef;
	goto end;
    }

    char *val_begin = ptr;
    int col = 0;
    static int numeric_cols[] = { 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 0, 0, 0 };

    AV *ret = newAV();
    while (*ptr && ptr < end && *ptr != '\n')
    {
	while (*ptr && ptr < end && *ptr != '\t')
	{
	    ptr++;
	}

	SV *sv;
	if (numeric_cols[col] == 1) /* Integer */
	{
	    sv = newSViv(atoi(val_begin));
	}
	if (numeric_cols[col] == 2) /* Float */
	{
	    sv = newSVnv(atof(val_begin));
	}
	else
	{
	    sv = newSVpvn(val_begin, ptr - val_begin);
	}
	av_push(ret, sv);

	ptr++;
	val_begin = ptr;
    }
    RETVAL = ret;
 end:
    
;
}
OUTPUT:
RETVAL


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3