+ return value is a tuple now containing the original line as second element
authorfabio <fabio@e1793c9e-67f9-0310-80fc-b846ff1f7b36>
Tue, 3 Jun 2008 08:18:14 +0000 (08:18 +0000)
committerfabio <fabio@e1793c9e-67f9-0310-80fc-b846ff1f7b36>
Tue, 3 Jun 2008 08:18:14 +0000 (08:18 +0000)
git-svn-id: http://svn.tuebingen.mpg.de/ag-raetsch/projects/QPalma@9352 e1793c9e-67f9-0310-80fc-b846ff1f7b36

ParaParser/ParaParser.cpp
ParaParser/ParaParser.h

index f0289c4..ffbe30e 100644 (file)
@@ -1,8 +1,4 @@
 #include "ParaParser.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
 #include <string>
 using namespace std;
 
@@ -143,6 +139,15 @@ ParaParser::ParaParser(const char* fmt, char** _fields, int num_entries, storage
 }
 
 
+
+ParaParser::~ParaParser() {
+   // unmap parsed file
+   int status = munmap(reads_area,reads_filesize);
+   if(status != 0)
+      perror("munmap");
+}
+
+
 /*
  * 
  *
@@ -178,7 +183,10 @@ void ParaParser::create_entry_from_line(const char* current_line, char* format_s
    if ( current_mode == IN_VECTOR ) {
       //printf("size is %zd\n",v_entries->size());
       v_entries->push_back(current_entries);
-      v_ptr_entries->push_back(make_pair(lineBeginPtr,lineEndPtr));
+      pair<char*,char*> p;
+      p.first = lineBeginPtr;
+      p.second = lineEndPtr;
+      v_ptr_entries->push_back(p);
       //printf("size is %zd\n",v_entries->size());
    }
 
@@ -226,17 +234,19 @@ int ParaParser::parseFile(char* reads_filename) {
       exit(EXIT_FAILURE);
    }
 
-   off_t reads_filesize = reads_stat.st_size;
+   reads_filesize = reads_stat.st_size;
    printf("Reads file is of size %lu bytes\n",(unsigned long) reads_filesize);
 
-   if ( current_mode == IN_VECTOR )
+   if ( current_mode == IN_VECTOR ) {
       v_entries = new VECTOR();
+      v_ptr_entries = new PTR_VECTOR();
+   }
 
    if ( current_mode == IN_MAP )
       entries = new MAP();
 
    // try to acquire file using mmap
-   void *reads_area = mmap (NULL,reads_filesize,PROT_READ,MAP_PRIVATE,reads_fid,0);
+   reads_area = mmap (NULL,reads_filesize,PROT_READ,MAP_PRIVATE,reads_fid,0);
    if (reads_area == MAP_FAILED) {
       perror("mmap");
       exit(EXIT_FAILURE);
@@ -285,10 +295,6 @@ int ParaParser::parseFile(char* reads_filename) {
 
    printf("Successfully parsed file !\n");
 
-   // unmap parsed file
-   status = munmap(reads_area,reads_filesize);
-   if(status != 0)
-      perror("munmap");
 
    // free unneeded variables
    free(current_line);
@@ -305,6 +311,7 @@ int ParaParser::parseFile(char* reads_filename) {
 
 PyObject* ParaParser::fetchEntry(map_key_t id) {
    PyObject* line_dict = PyDict_New();
+
    //printf("begin of fetchEntry\n");
    //printf("size of map %d\n",entries->size());
    //printf("keys:\n");
@@ -330,6 +337,9 @@ PyObject* ParaParser::fetchEntry(map_key_t id) {
       lineEndPtr = ptr_pair.second;
    }
 
+   assert (lineBeginPtr != 0);
+   assert (lineEndPtr != 0);
+
    if ( current_mode == IN_MAP ) {
       //printf("IN_MAP mode\n");
       MAP::iterator find_it = entries->find(id);
@@ -379,17 +389,17 @@ PyObject* ParaParser::fetchEntry(map_key_t id) {
    //}
    */
 
-   //printf("end of fetchEntry\n");
    PyObject *return_value = PyTuple_New(2);
    PyTuple_SetItem(return_value,0,line_dict);
 
    char* current_line = (char*) malloc(sizeof(char)*512);
    memset(current_line,0,512);
    unsigned long line_size = lineEndPtr - lineBeginPtr;
-   strncpy(current_line,lineBeginPtr,line_size);
+   strncpy(current_line,lineBeginPtr,line_size-1);
+
 
    PyObject *original_line = PyString_FromString(current_line);
    PyTuple_SetItem(return_value,1,original_line);
 
-   return line_dict;
+   return return_value;
 }
index 8a10955..c7dd6de 100644 (file)
@@ -2,6 +2,10 @@
 #define __PARAPARSER_H__
 
 #include <Python.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
 #include <map>
 #include <vector>
 #include <utility>
@@ -32,6 +36,9 @@ class ParaParser{
       size_t num_columns;
       char** types_list;
 
+      void *reads_area;
+      off_t reads_filesize;
+
       storage_mode current_mode;
 
       MAP *entries;
@@ -52,7 +59,7 @@ class ParaParser{
             return v_entries->size();
       }
 
-      ~ParaParser(){}
+      ~ParaParser();
 };