Пример #1
0
READ_ALN* new_read_aln(hashtable ht,char*read_name) {

  // look for read in HT
  // add to hash table
  READ_ALN* r;
  if((r=get_read_aln(ht,read_name))==NULL) {
    // Memory chunck: |[index_entry]len bytes+1|
    int len=strlen(read_name);
    ulong key=hashit(read_name);
    char *mem_block=(char*)malloc(sizeof(READ_ALN)+len+1);
    index_mem+=sizeof(READ_ALN)+len+1+sizeof(hashnode);
    if (mem_block==NULL) { 
      fprintf(stderr,"Error allocating memory\n");
      exit(1);
    }
    r=(READ_ALN*)&mem_block[0];    
    r->name=(char*)&mem_block[sizeof(READ_ALN)];
    r->name[len]='\0';
    strncpy(r->name,read_name,len+1);
    r->ctr=0;
    if(insere(ht,key,r)<0){
      fprintf(stderr,"Error adding %s to index\n",read_name);
      exit(1);
    }
  } 
  r->ctr++;
  return(r);
}
Пример #2
0
static void hash_del_con(LpsHash* hash, const Con* con)
{
   LpsHElem*    he;
   LpsHElem*    next;
   unsigned int hcode;

   assert(hash_valid(hash));
   assert(con        != NULL);
   assert(hash->type == LHT_CON);
   
   hcode = hashit(con->name) % hash->size;

   for(he = hash->bucket[hcode], next = NULL; he != NULL; next = he, he = he->next)
      if (he->value.con == con)
         break;

   assert(he != NULL);

   if (next == NULL)
      hash->bucket[hcode] = he->next;
   else
      next->next = he->next;

   hash->elems--;

   free(he);

   assert(hash_valid(hash));
}
Пример #3
0
/**
 * The Constructor for VStream takes the video_source, the roborealm host and a file location.
 * Both the host ip and the file loc aren't required.
 */
VStream::VStream (inputVars data)
{
  this->host = data.host;
  this->file_loc = data.file_location;
  this->cameraNumber = data.cameraID;
  _inputFormat = hashit(data.inputSource);
  this->ports = data.ports;

  switch(_inputFormat)
    {
      case VIDEO_FILE:
	  video();
        break;

      case VIDEO_CAMERA:
	  camera();
        break;

      case ROBOREALM:
	  roborealm(host);
        break;

      case STILL_IMAGE:
	  still();
        break;

      default:
        cout << "Invalid input source was provided." << endl;
        show_usage("METR4810.exe");
        break;
    }
}
Пример #4
0
static void hash_del_var(LpsHash* hash, const Var* var)
{
   LpsHElem*    he;
   LpsHElem*    next;
   unsigned int hcode;

   assert(hash_valid(hash));
   assert(var        != NULL);
   assert(hash->type == LHT_VAR);
   
   hcode = hashit(var->name) % hash->size;

   for(he = hash->bucket[hcode], next = NULL; he != NULL; next = he, he = he->next)
      if (he->value.var == var)
         break;

   assert(he != NULL);

   if (next == NULL)
      hash->bucket[hcode] = he->next;
   else
      next->next = he->next;

   hash->elems--;

   free(he);

   assert(hash_valid(hash));
}
Пример #5
0
int hashit(std::vector<T> in){
	std::stringstream ss;
	for(uint i=0;i<in.size();i++){
		ss << in.at(i);
	}
	return hashit(ss.str().c_str());
}
Пример #6
0
static off_t
yankstr(char **cpp)
{
	char *cp = *cpp;
	int c, ch;
	char dbuf[BUFSIZ];
	char *dp = dbuf;
	char *tp;
	static char tmp[] = "b\bt\tr\rn\nf\f\\\\\"\"";

	while ((c = *cp++)) {
		if (dp == dbuf + sizeof(dbuf) - 3)
			errx(1, "message too long");
		switch (c) {

		case '"':
			cp++;
			goto out;

		case '\\':
			c = *cp++;
			if (c == 0)
				break;
			if (c == '\n') {
				if (fgets(linebuf, sizeof linebuf, stdin)
				    == NULL) {
					if (ferror(stdin))
						err(3, "x.c");
					return(-1);
				}
				cp = linebuf;
				continue;
			}
			for (tp = tmp; (ch = *tp++); tp++)
				if (c == ch) {
					c = *tp;
					goto gotc;
				}
			if (!octdigit(c)) {
				*dp++ = '\\';
				break;
			}
			c -= '0';
			if (!octdigit(*cp))
				break;
			c <<= 3, c += *cp++ - '0';
			if (!octdigit(*cp))
				break;
			c <<= 3, c += *cp++ - '0';
			break;
		}
gotc:
		*dp++ = c;
	}
out:
	*cpp = --cp;
	*dp = 0;
	return (hashit(dbuf, 1));
}
Пример #7
0
READ_ALN* get_read_aln(hashtable ht,char*read_name) {
  ulong key=hashit(read_name); 
  READ_ALN* r=(READ_ALN*)get_object(ht,key);
  while (r!=NULL) {      // confirm that hdr are equal
    if (!strcmp(read_name,r->name)) break;
    r=(READ_ALN*)get_next_object(ht,key);
  }
  return r;
}
Пример #8
0
inline static INDEX_ENTRY* lookup_header(hashtable index,char *hdr) {
  // lookup hdr in index
  long key=hashit(hdr);
  INDEX_ENTRY* e=(INDEX_ENTRY*)get_object(index,key);
  while (e!=NULL) {      // confirm that hdr are equal
    if ( !strcmp(hdr,e->hdr)) break;
    e=(INDEX_ENTRY*)get_next_object(index,key);
  }
  return e;
}
Пример #9
0
static INDEX_ENTRY* lookup_header(hashtable sn_index,char *hdr) {
  // lookup hdr in sn_index
  ulong key=hashit(hdr);
  //printf("looking for %s: key=%lu\n",hdr,key);
  INDEX_ENTRY* e=(INDEX_ENTRY*)get_object(sn_index,key);
  while (e!=NULL) {      // confirm that hdr are equal
    if ( !strcmp(hdr,e->hdr)) break;
    e=(INDEX_ENTRY*)get_next_object(sn_index,key);
  }
  return e;
}
Пример #10
0
void
inithash(void)
{
	char buf[512];
	int mesgpt = 0;

	rewind(mesgread);
	while (fgetNUL(buf, sizeof buf, mesgread) != 0) {
		hashit(buf, 0, mesgpt);
		mesgpt += strlen(buf) + 2;
	}
}
Пример #11
0
static void
inithash(void)
{
	char buf[BUFSIZ];
	FILE *mesgread = fopen(strings, "r");

	if (mesgread == NULL)
		return;
	for (;;) {
		mesgpt = tellpt;
		if (fgetNUL(buf, sizeof buf, mesgread) == 0)
			break;
		ignore(hashit(buf, 0));
	}
	ignore(fclose(mesgread));
}
Пример #12
0
/* Liefert NULL wenn nicht gefunden.
 */
static Sos* hash_lookup_sos(const LpsHash* hash, const char* name)
{
   unsigned int hcode;
   LpsHElem*    he;
   
   assert(hash_valid(hash));
   assert(hash->type == LHT_SOS);
   assert(name != NULL);
   
   hcode  = hashit(name) % hash->size;

   for(he = hash->bucket[hcode]; he != NULL; he = he->next)
      if (!strcmp(he->value.sos->name, name))
         break;

   return (he == NULL) ? (Sos*)0 : he->value.sos;
}
Пример #13
0
static void hash_add_con(LpsHash* hash, Con* con)
{
   LpsHElem*    he = calloc(1, sizeof(*he));
   unsigned int hcode;

   assert(hash_valid(hash));
   assert(con        != NULL);
   assert(hash->type == LHT_CON);
   assert(he         != NULL);
   
   hcode               = hashit(con->name) % hash->size;
   he->value.con       = con;
   he->next            = hash->bucket[hcode];
   hash->bucket[hcode] = he;
   hash->elems++;

   assert(hash_lookup_con(hash, con->name) == con);
}
Пример #14
0
static void hash_add_var(LpsHash* hash, Var* var)
{
   LpsHElem*    he = calloc(1, sizeof(*he));
   unsigned int hcode;

   assert(hash_valid(hash));
   assert(var        != NULL);
   assert(hash->type == LHT_VAR);
   assert(he         != NULL);
   
   hcode               = hashit(var->name) % hash->size;
   he->value.var       = var;
   he->next            = hash->bucket[hcode];
   hash->bucket[hcode] = he;
   hash->elems++;

   assert(hash_lookup_var(hash, var->name) == var);
}
Пример #15
0
static void hash_add_sos(LpsHash* hash, Sos* sos)
{
   LpsHElem*    he = calloc(1, sizeof(*he));
   unsigned int hcode;

   assert(hash_valid(hash));
   assert(sos        != NULL);
   assert(hash->type == LHT_SOS);
   assert(he         != NULL);
   
   hcode               = hashit(sos->name) % hash->size;
   he->value.sos       = sos;
   he->next            = hash->bucket[hcode];
   hash->bucket[hcode] = he;
   hash->elems++;

   assert(hash_lookup_sos(hash, sos->name) == sos);
}
Пример #16
0
void prtaawctp(aaw_c *aawc, aaw_c *maawc, snodm **stab, unsigned htsz, char *tc, char *fn)
{
    int i, j, pos, unseencou=0;
    int u2=0;
    unsigned tint;
    snodm *tsnod2;
    boole seen;
    FILE *fo=fopen(fn, "w");

    for(i=2;i<aawc->numl;++i) {
        seen = 0;

        tint=hashit(aawc->aaw[i]->aw[1]->w, htsz); // hash the snpname
        if(stab[tint] == NULL) {
            u2++;
            continue;
        } else {
            tsnod2=stab[tint];
            while( (tsnod2 != NULL) ) {
                if(!(strcmp(tsnod2->aw->aw[0]->w, aawc->aaw[i]->aw[1]->w))) {
                    seen =1;
                    pos=atoi(tsnod2->aw->aw[1]->w);
                    fprintf(fo, "%s ", tc); 
                    fprintf(fo, "%s ", aawc->aaw[i]->aw[1]->w);
                    fprintf(fo, "%c ", '0'); 
                    fprintf(fo, "%i ", pos);
                    for(j=2;j<aawc->aaw[i]->al;++j)
                        fprintf(fo, (j!=aawc->aaw[i]->al-1)?"%s ":"%s\n", aawc->aaw[i]->aw[j]->w);
                    break;
                }
                tsnod2=tsnod2->n;
            }
        }
        if(!seen) {
            unseencou++;
            // printf("%s ", aawc->aaw[i]->aw[1]->w);
        }
    }
    if( u2 | unseencou)
        fprintf(stderr, "WARNING: #u2=%i unseen=%i\n", u2, unseencou); 
    fclose(fo);
    return;
}
Пример #17
0
INDEX_ENTRY* new_indexentry(hashtable ht,char*hdr,int len,long start_pos) {
  
  // Memory chunck: |[index_entry]len bytes+1|
  char *mem_block=(char*)malloc(sizeof(INDEX_ENTRY)+len+1);
  if (mem_block==NULL) { return(NULL);}  
  INDEX_ENTRY *e=(INDEX_ENTRY*)&mem_block[0];

  e->hdr=(char*)&mem_block[sizeof(INDEX_ENTRY)];
  e->entry_start=start_pos;
  
  strncpy(e->hdr,hdr,len);
  
  // add to hash table
  long key=hashit(e->hdr);
  if(insere(ht,key,e)<0) {
    fprintf(stderr,"Error adding %s to index\n",hdr);
    return(NULL);
  }
  index_mem+=sizeof(INDEX_ENTRY)+len+1+sizeof(hashnode);
  return(e);
}
void FactoryHelloWorldWebsite::createWebPage(const std::string newPath, Wt::WContainerWidget* aroot )
{
     // we are using an enum you can  add more pages here but  best implementation with a Map
     // see https://github.com/matiu2/witty-plus/blob/master/wittyPlus/base/URLs.hpp

     Wt::WApplication::instance()->log("info") << "Create web page called: " << newPath;
     switch (hashit(newPath)) {
     case ask:
          new InputWindow(aroot);
          break;
     case say:
          new SayWindow(aroot);
          break;
     case admin:
          new AdminWindow(aroot);
          break;
     case landing:
          new MainWindow(aroot);
          break;
     }
}
Пример #19
0
static off_t
yankstr(char **cpp)
{
	char *cp = *cpp;
	int c, ch;
	char *dbuf, *dp, *edp;
	const char *tp;
	off_t hash;
	size_t bsiz = BUFSIZ;

	if ((dp = dbuf = malloc(bsiz)) == NULL)
		err(1, "malloc");
	edp = dbuf + bsiz;

	while ((c = *cp++) != '\0') {
		switch (c) {

		case '"':
			/* Look for a concatenated string */
			for (;;) {
				while (isspace((unsigned char)*cp))
					cp++;
				if (*cp == '\0') {
					if (fgets(linebuf,
					    sizeof linebuf, stdin) == NULL) {
						if (ferror(stdin))
							err(1,
							"Error reading `x.c'");
						goto out;
					}
					cp = linebuf;
				} else {
					if (*cp == '"') {
						cp++;
						if (*cp == '"') {
							cp++;
							continue;
						} else {
							c = *cp++;
							goto gotc;
						}
					} else {
						cp++;
						goto out;
					}
				}
			}
			/*NOTREACHED*/
		case '\\':
			c = *cp++;
			if (c == 0)
				break;
			if (c == '\n') {
				if (fgets(linebuf, sizeof linebuf, stdin)
				    == NULL) {
					if (ferror(stdin))
						err(1, "Error reading `x.c'");
					return(-1);
				}
				cp = linebuf;
				continue;
			}
			for (tp = "b\bt\tr\rn\nf\f\\\\\"\""; (ch = *tp++); tp++)
				if (c == ch) {
					c = *tp;
					goto gotc;
				}
			if (!octdigit(c)) {
				*dp++ = '\\';
				break;
			}
			c -= '0';
			if (!octdigit(*cp))
				break;
			c <<= 3, c += *cp++ - '0';
			if (!octdigit(*cp))
				break;
			c <<= 3, c += *cp++ - '0';
			break;
		}
gotc:
		if (dp >= edp - 1) {
			char *nbuf;
			bsiz += BUFSIZ;
			if ((nbuf = realloc(dbuf, bsiz)) == NULL) {
				free(dbuf);
				err(1, "realloc");
			}
			dp = nbuf + (dp - dbuf);
			edp = nbuf + bsiz;
			dbuf = nbuf;
		}
		*dp++ = c;
	}
out:
	*cpp = --cp;
	*dp = '\0';
	hash = hashit(dbuf, 1);
	free(dbuf);
	return hash;
}
Пример #20
0
int main(int argc, char **argv ) {
  long paired=0;

  //printf("%d",sizeof(struct index_entry)); 
  
  if (argc!=6) {
    fprintf(stderr,"Usage: fastqinterleaved2pair.c fastq fastq1 fastq2\n");
    //fprintf(stderr,"%d",argc);
    exit(1);
  }

  FILE *fd=open_fastq(argv[1]);
  // ************************************************************
  off_t cur_offset=1;
  unsigned long cline=1;
  hashtable index=new_hashtable(HASHSIZE);
  index_mem+=sizeof(hashtable);

  index_file(argv[1],index,0,-1);
  printf("\n");
  // print some info
  printf("Reads indexed: %ld\n",index->n_entries);
  printf("Memory used in indexing: %ld MB\n",index_mem/1024/1024);  
  // 

  char *p1=argv[3];
  char *p2=argv[4];
  char *p3=argv[5];
  fd1=open_fastq(argv[1]);
  fd2=open_fastq(argv[2]);
  FILE *fdw1=fopen(p1,"w");
  FILE *fdw2=fopen(p2,"w");
  FILE *fdw3=fopen(p3,"w");
  unsigned long up2=0;

  if ( fdw1==NULL || fdw2==NULL || fdw3==NULL ) {
    fprintf(stderr,"Unable to create output files\n");
    exit(1);
  }
  
  // read the entry using another fd
  cline=1;
  while(!feof(fd2)) {
    long start_pos=ftell(fd2);
    char *hdr=READ_LINE(fd2);

    if ( hdr==NULL) break;
    if ( hdr[0]!='@' ) {
      fprintf(stderr,"line %ul: error in header %s",cline,hdr);
      return 1;
    }
    // discard @ (one less byte)
    hdr=&hdr[1];
    int len=strlen(hdr);
    len--;
    hdr[len-1]='\0'; //
    // lookup hdr in index
    INDEX_ENTRY* e=lookup_header(index,hdr);
    if (e==NULL) {
      ++up2;
      copy_read(start_pos,fd2,fdw3);
    } else {
      long key=hashit(hdr);
      // pair found
      ++paired;
      copy_read(start_pos,fd2,fdw2);
      copy_read(e->entry_start,fd1,fdw1);
      // remove entry from index
      if (delete(index,key,e)!=e) {
	fprintf(stderr,"Unable to delete entry from index\n");
	exit(1);
      }
      free_indexentry(e);
    }
    PRINT_READS_PROCESSED(cline/4);

    cline+=4;
  }
  printf("\n");
  printf("Recording %ld unpaired reads from %s\n",index->n_entries,argv[1]);fflush(stdout);
  fclose(fd1);


  // record the unpaired from argv[1]
  fd1=open_fastq(argv[1]); 
#ifndef SEQDISKACCESS
  init_hash_traversal(index);
  INDEX_ENTRY* e;
  cline=1;
  while((e=(INDEX_ENTRY*)next_hash_object(index))!=NULL) {
          copy_read(e->entry_start,fd1,fdw3);
	  PRINT_READS_PROCESSED(cline);
	  ++cline;
  }
  //
#else
  //sequential disk access
  //
  cline=1;
  unsigned long remaining=index->n_entries;
  while(!feof(fd1) && remaining ) {
    //long start_pos=ftell(fd2);
    char *hdr=READ_LINE(fd1);

    if ( hdr==NULL) break;
    if ( hdr[0]!='@' ) {
      fprintf(stderr,"line %ld %s: error in header %s",cline,argv[1],hdr);
      return 1;
    }
    // discard @ (one less byte)
    hdr=&hdr[1];
    int len=strlen(hdr);
    len--;
    hdr[len-1]='\0'; //

    // lookup hdr in index
    INDEX_ENTRY* e=lookup_header(index,hdr);
    if (e!=NULL) {
      copy_read(e->entry_start,fd1,fdw3);
      remaining--;
    } else {
      READ_LINE(fd1);//seq
      READ_LINE(fd1);//qual
      READ_LINE(fd1);//qual
    }
    PRINT_READS_PROCESSED(cline/4);
    cline+=4;
  }
  fclose(fd1);
#endif
  printf("\n");
  printf("Unpaired from %s: %ld\n",argv[1],index->n_entries);
  printf("Unpaired from %s: %ld\n",argv[2],up2);
  printf("Paired: %ld\n",paired);
  /*fseek(fd2,start_pos,SEEK_SET);
    printf("%s",READ_LINE(fd2));
    printf("%s",READ_LINE(fd2));
    printf("%s",READ_LINE(fd2));
    printf("%s",READ_LINE(fd2));
  */
  fclose(fdw1);
  fclose(fdw2);
  fclose(fdw3);
  if ( paired == 0 ) {
    fprintf(stderr,"!!!WARNING!!! 0 paired reads! are the headers ok?\n");
    exit(1);
  }
  exit(0);
}
Пример #21
0
uint String::hash() const {
	return hashit(c_str());
}
Пример #22
0
int
trackfile(int sockfd, char *filename, char *range, uint16_t num_trackers,
	in_addr_t *trackers, uint16_t maxpeers, uint16_t num_pkg_servers,
	in_addr_t *pkg_servers, CURL *curlhandle)
{
#ifdef	TIMEIT
	struct timeval		start_time, end_time;
	unsigned long long	s, e;
#endif
	CURLcode	curlcode;
	uint64_t	hash;
	uint16_t	i;
	tracker_info_t	*tracker_info, *infoptr;
	int		info_count;
	char		success;

#ifdef	TIMEIT
	gettimeofday(&start_time, NULL);
#endif

	hash = hashit(filename);

#ifdef	TIMEIT
	gettimeofday(&end_time, NULL);
	s = (start_time.tv_sec * 1000000) + start_time.tv_usec;
	e = (end_time.tv_sec * 1000000) + end_time.tv_usec;
	logmsg("trackfile:svc time1: %lld usec file (%s)\n", (e - s), filename);
#endif

#ifdef	DEBUG
{
	int		j;
	struct in_addr	in;

	for (j = 0 ; j < num_trackers ; ++j) {
		in.s_addr = trackers[j];
		logmsg("trackfile:trackers[%d] = (%s)\n", j, inet_ntoa(in));
	}

	for (j = 0 ; j < num_pkg_servers ; ++j) {
		in.s_addr = pkg_servers[j];
		logmsg("trackfile:pkg_servers[%d] = (%s)\n", j, inet_ntoa(in));
	}
}
#endif

#ifdef	TIMEIT
	gettimeofday(&end_time, NULL);
	s = (start_time.tv_sec * 1000000) + start_time.tv_usec;
	e = (end_time.tv_sec * 1000000) + end_time.tv_usec;
	logmsg("trackfile:svc time2: %lld usec file (%s)\n", (e - s), filename);
#endif

	/*
	 * see if there is a prediction for this file
	 */
	tracker_info = NULL;
	info_count = getprediction(hash, &tracker_info);

#ifdef	TIMEIT
	gettimeofday(&end_time, NULL);
	s = (start_time.tv_sec * 1000000) + start_time.tv_usec;
	e = (end_time.tv_sec * 1000000) + end_time.tv_usec;
	logmsg("trackfile:svc time3: %lld usec file (%s)\n", (e - s), filename);
#endif

#ifdef	DEBUG
	if (info_count == 0) {
		logmsg("trackfile:pred miss (0x%016llx)\n", hash);
	} else {
		logmsg("trackfile:pred hit (0x%016llx)\n", hash);
	}
#endif

	if (info_count == 0) {
		/*
		 * no prediction. need to ask a tracker for peer info for
		 * this file.
		 */
		for (i = 0 ; i < num_trackers; ++i) {
#ifdef	DEBUG
			struct in_addr	in;

			in.s_addr = trackers[i];
			logmsg("trackfile:sending lookup to tracker (%s)\n",
				inet_ntoa(in));
#endif
			info_count = lookup(sockfd, &trackers[i], hash,
				&tracker_info);

			if (info_count > 0) {
				break;
			}

			/*
			 * lookup() mallocs space for 'tracker_info', so need to
			 * free it here since we'll call lookup() again in the
			 * next iteration
			 */
			if (tracker_info != NULL) {
				free(tracker_info);
				tracker_info = NULL;
			}
		}
	}

#ifdef	TIMEIT
	gettimeofday(&end_time, NULL);
	s = (start_time.tv_sec * 1000000) + start_time.tv_usec;
	e = (end_time.tv_sec * 1000000) + end_time.tv_usec;
	logmsg("trackfile:svc time4: %lld usec file (%s)\n", (e - s), filename);
#endif

#ifdef	DEBUG
	logmsg("trackfile:info_count (%d)\n", info_count);
#endif

	success = 0;
	infoptr = tracker_info;
	if ((info_count > 0) && (infoptr->hash == hash)) {
		/*
		 * write the prediction info to a file	
		 */
		save_prediction_info(tracker_info, info_count);

#ifdef	TIMEIT
		gettimeofday(&end_time, NULL);
		s = (start_time.tv_sec * 1000000) + start_time.tv_usec;
		e = (end_time.tv_sec * 1000000) + end_time.tv_usec;
		logmsg("trackfile:svc time5: %lld usec file (%s)\n", (e - s),
			filename);
#endif

#ifdef	DEBUG
		logmsg("trackfile:hash (0x%llx) : numpeers (%d)\n",
			infoptr->hash, infoptr->numpeers);

		logmsg("trackfile:peers:\n");

		for (i = 0 ; i < infoptr->numpeers; ++i) {
			struct in_addr	in;

			in.s_addr = infoptr->peers[i].ip;
			logmsg("\t%s : %c %d\n", inet_ntoa(in),
				(infoptr->peers[i].state == DOWNLOADING ?
					'd' : 'r'), infoptr->peers[i].state);
		}
#endif

#ifdef	TIMEIT
		gettimeofday(&end_time, NULL);
		s = (start_time.tv_sec * 1000000) + start_time.tv_usec;
		e = (end_time.tv_sec * 1000000) + end_time.tv_usec;
		logmsg("trackfile:svc time6: %lld usec file (%s)\n", (e - s),
			filename);
#endif

		for (i = 0 ; i < infoptr->numpeers; ++i) {
#ifdef	DEBUG
			{
			struct in_addr	in;
			int		k;

			in.s_addr = infoptr->peers[i].ip;

			if (strncmp(inet_ntoa(in), "10.", 3)) {
				for (k = 0 ; i < num_trackers; ++k) {
					send_msg(sockfd, &trackers[k],
						STOP_SERVER);
				}

				logmsg("trackfile:bogus IP (%s)\n",
					inet_ntoa(in));
				exit(-1);
			}
			}
#endif
			if (getremote(filename, &infoptr->peers[i], range,
					curlhandle) == 0) {
				/*
				 * successful download, exit this loop
				 */
				success = 1;
				break;
			} else {
				/*
				 * mark the peer as 'bad'. we do this by
				 * telling the tracker server to 'unregister'
				 * this hash.
				 */

				tracker_info_t	*info;
				int		len;
				int		j;

				len = sizeof(*info) + sizeof(peer_t);

				if ((info = (tracker_info_t *)malloc(len))
						!= NULL) {

					bzero(info, len);
					info->hash = hash;
					info->numpeers = 1;
					info->peers[0].ip =
						infoptr->peers[i].ip;

					for (j = 0 ; j < num_trackers; ++j) {
						unregister_hash(sockfd,
							&trackers[j], 1, info);
					}

					free(info);
				}
			}
		}
	}

#ifdef	TIMEIT
	gettimeofday(&end_time, NULL);
	s = (start_time.tv_sec * 1000000) + start_time.tv_usec;
	e = (end_time.tv_sec * 1000000) + end_time.tv_usec;
	logmsg("trackfile:svc time7: %lld usec file (%s)\n", (e - s), filename);
#endif

	if (!success) {
		/*
		 * unable to download the file from a peer, need to
		 * get it from one of the package servers
		 */

		for (i = 0 ; i < num_pkg_servers ; ++i) {
			peer_t	pkgpeer;

			pkgpeer.ip = pkg_servers[i];
			pkgpeer.state = READY;

			/*
			 * if this is the last package server, then
			 * disable the connection timeout -- this is our
			 * last hope of getting the package.
			 */
			if (i == (num_pkg_servers - 1)) { 
				if ((curlcode = curl_easy_setopt(curlhandle,
					CURLOPT_CONNECTTIMEOUT, 0)) !=
					CURLE_OK) {

					logmsg("getremote:curl_easy_setopt():failed:(%d)\n", curlcode);
				}
			}

			if (getremote(filename, &pkgpeer, range,
					curlhandle) == 0) {
				success = 1;
			}

			/*
			 * reset the connection timeout
			 */
			if (i == (num_pkg_servers - 1)) { 
				if ((curlcode = curl_easy_setopt(curlhandle,
					CURLOPT_CONNECTTIMEOUT, 2)) !=
					CURLE_OK) {

					logmsg("getremote:curl_easy_setopt():failed:(%d)\n", curlcode);
				}
			}

			if (success) {
				break;
			}
		}
	}

#ifdef	TIMEIT
	gettimeofday(&end_time, NULL);
	s = (start_time.tv_sec * 1000000) + start_time.tv_usec;
	e = (end_time.tv_sec * 1000000) + end_time.tv_usec;
	logmsg("trackfile:svc time8: %lld usec file (%s)\n", (e - s), filename);
#endif

	if (success) {
		tracker_info_t	info[1];

		bzero(info, sizeof(info));

		info[0].hash = hash;
		info[0].numpeers = 0;

		for (i = 0 ; i < num_trackers; ++i) {
			register_hash(sockfd, &trackers[i], 1, info);
		}
	}

	/*
	 * lookup() and getprediction() mallocs tracker_info
	 */
	if (tracker_info != NULL) {
		free(tracker_info);
	}	

#ifdef	TIMEIT
	gettimeofday(&end_time, NULL);
	s = (start_time.tv_sec * 1000000) + start_time.tv_usec;
	e = (end_time.tv_sec * 1000000) + end_time.tv_usec;
	logmsg("trackfile:svc time: %lld usec file (%s)\n", (e - s), filename);
#endif

	if (success) {
		return(0);
	}

	return(-1);
}
Пример #23
0
void
copystr(void)
{
	int c, ch;
	char buf[512];
	char *cp = buf;

	for (;;) {
		if (cp == buf + sizeof(buf) - 2)
			errx(1, "message too long");
		c = getchar();
		if (c == EOF)
			break;
		switch (c) {

		case '"':
			*cp++ = 0;
			goto out;
		case '\\':
			c = getchar();
			switch (c) {

			case 'b':
				c = '\b';
				break;
			case 't':
				c = '\t';
				break;
			case 'r':
				c = '\r';
				break;
			case 'n':
				c = '\n';
				break;
			case '\n':
				continue;
			case 'f':
				c = '\f';
				break;
			case '0':
				c = 0;
				break;
			case '\\':
				break;
			default:
				if (!octdigit(c))
					break;
				c -= '0';
				ch = getchar();
				if (!octdigit(ch))
					break;
				c <<= 7, c += ch - '0';
				ch = getchar();
				if (!octdigit(ch))
					break;
				c <<= 3, c+= ch - '0', ch = -1;
				break;
			}
		}
		*cp++ = c;
	}
out:
	*cp = 0;
	printf("%d", hashit(buf, 1, 0));
}
Пример #24
0
int main ( int argc, char** argv )
{
	if(argc !=2)
		std::cout <<"Usage ./server <DbFilename>\n";

	FileStorage Db(static_cast<std::string>(argv[1]));

	// register the storage system
	database.registerStorage(&Db);

	// load the data from storage if any
	database.loadDataFromStorage();

	// register for signal handler
	registerSignal();

	try
	{
		std::cout << "Server starting on port 15000....\n";
		ServerSocket server ( 15000 );

		while ( true )
		{
			ServerSocket server_sock;
			server.accept ( server_sock );
			try
			{
				while ( true )
				{
					std::string input;
					server_sock >> input;

					std::string key,value;
					std::string result;

					// remove the last \r\n from input string
					input.erase(input.size()-2);

					// space for storing the tokens from input string
					std::vector<std::string> token;
					std::fill(token.begin(), token.end(), "");

					// Parse the input and tokenize it and store in vector for further use
					std::stringstream ss(input);
					std::string s;

					while (getline(ss, s, ' ')) {
						token.push_back(s);
					}

					// Parese rest of input based on first token {SET,GET,SAVE}  
					switch(hashit(token[0]))
					{
						case GET:
							if (2 != token.size())
								goto INVALID;

							key = token[1];
							result = database.get(key);

							// Return the result of GET query
							server_sock <<"$3\n";
							server_sock << result <<"\n";
							break;

						case SET:
							if (3 != token.size())
								goto INVALID;

							key = token[1];
							value = token[2];

							// Store the SET query in memory
							database.add(key,value);
							server_sock <<"+OK\n";

							break;

						case SAVE:
							if (1 != token.size())
								goto INVALID;
							database.persistData();
							server_sock <<"+OK\n";
							break;
INVALID:
						default:
							server_sock << "-INVALID\n";
							break;

					}
				}

			}
			catch ( SocketException& ) {}
		}
	}
	catch ( SocketException& e )
	{
		std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
	}


	return 0;
}
Пример #25
0
void prtaawc00(aaw_c *aawc, aaw_c *maawc, snodm **stab, aaw_c *vaawc, unsigned htsz, char *tc)
{
    /* prints the BGL but with VCF interpolations ... a debugging function */
    int i, j, pos, unseencou=0;
    int u2=0;
    unsigned tint;
    snodm *tsnod2;
    boole seen;
    boole switchvgt;
    int vi, vj;
    char va1, va2; // these are a1 and a2 in the VCF file

    for(i=2;i<aawc->numl;++i) {
        vi=i+5;
        seen = 0;

        tint=hashit(aawc->aaw[i]->aw[1]->w, htsz); // hash the snpname
        if(stab[tint] == NULL) {
            u2++;
            continue;
        } else {
            tsnod2=stab[tint];
            while( (tsnod2 != NULL) ) {
                if(!(strcmp(tsnod2->aw->aw[0]->w, aawc->aaw[i]->aw[1]->w))) {
                    seen =1;
                    pos=atoi(tsnod2->aw->aw[1]->w);
                    printf("%s ", tc); 
                    printf("%s ", aawc->aaw[i]->aw[1]->w);
                    printf("%c ", '0'); 
                    printf("%i ", pos);
                    va1 = vaawc->aaw[vi]->aw[3]->w[0];
                    va2 = vaawc->aaw[vi]->aw[4]->w[0];
                    printf("%s ", vaawc->aaw[vi]->aw[3]->w);
                    printf("%s ", vaawc->aaw[vi]->aw[4]->w);
                    for(j=2;j<aawc->aaw[i]->al;++j) {
                        printf((j!=aawc->aaw[i]->al-1)?"%s ":"%s\n", aawc->aaw[i]->aw[j]->w);
                        vj=j/2+8;
                        switchvgt=0; // default don't switch VFCs GT
                        if(j%2==1) {
                            if(aawc->aaw[i]->aw[j]->w[0] == va1)
                                switchvgt=1;
                            if(switchvgt) 
                                printf((j!=aawc->aaw[i]->al-1)?"%s ":"%s ", "1/0");
                            else
                                printf((j!=aawc->aaw[i]->al-1)?"%s ":"%s ", vaawc->aaw[vi]->aw[vj]->w);
                        }
                    }
                    break;
                }
                tsnod2=tsnod2->n;
            }
        }
        if(!seen) {
            unseencou++;
            // printf("%s ", aawc->aaw[i]->aw[1]->w);
        }
    }
    if( u2 | unseencou)
        printf("WARNING: #u2=%i unseen=%i\n", u2, unseencou); 
    return;
}
Пример #26
0
// String:
int hashit(const String &str, int hashsize)
  {
  return hashit(str.as_char(), hashsize);
  }
Пример #27
0
int main(int argc, char **argv ) {
  //long paired=0;
  is_paired_data=0;
  is_interleaved=0;
  printf("Version iRAP %s\n",VERSION);
  if (argc<2 || argc>3) {
    fprintf(stderr,"Usage: fastq_validator fastq1 [fastq2 file|pe]\n");
    //fprintf(stderr,"%d",argc);
    exit(1);
  }

  FILE *fd1=NULL;
  FILE *fd2=NULL;
  // open & close
  fd1=open_fastq(argv[1]);
  fclose(fd1);
  //fprintf(stderr,"%d\n",argc);
  //bin/fprintf(stderr,"%s\n",argv[0]);
  if (argc ==3) {
    is_paired_data=1;
    if ( !strncmp(argv[2],"pe",2) ) {
      is_interleaved=1;
    } else  {
      fd2=open_fastq(argv[2]);
      fclose(fd2);
    }
  }
  // ************************************************************
  // casava 1.8?
  is_casava_18=is_casava_1_8(argv[1]);
  if (is_casava_18) fprintf(stderr,"CASAVA=1.8\n");
  // ************************************************************
  //off_t cur_offset=1;
  // interleaved
  if ( is_interleaved ) {
    exit(validate_interleaved(argv[1]));
  }
  unsigned long cline=1;
  fprintf(stderr,"HASHSIZE=%lu\n",(long unsigned int)HASHSIZE);
  //memset(&collisions[0],0,HASHSIZE+1);
  hashtable sn_index=new_hashtable(HASHSIZE);
  index_mem+=sizeof(hashtable);

  index_file(argv[1],sn_index,0,-1);
  fprintf(stderr,"\n");
  // print some info
  fprintf(stderr,"Reads processed: %ld\n",sn_index->n_entries);
  fprintf(stderr,"Memory used in indexing: ~%ld MB\n",index_mem/1024/1024);  
  // pair-end
  if (argc ==3 ) {
    fprintf(stderr,"File %s processed\n",argv[1]);  
    fprintf(stderr,"Next file %s\n",argv[2]);  
    // validate the second file and check if all reads are paired
    fd2=open_fastq(argv[2]);
    INDEX_ENTRY* e;
    // read the entry using another fd
    cline=1;
    // TODO: improve code - mostly duplicated:(
    while(!feof(fd2)) {
      //long start_pos=ftell(fd2);
      char *hdr=READ_LINE_HDR(fd2);
      if ( hdr==NULL) break;
      int len;
      char *seq=READ_LINE_SEQ(fd2);
      char *hdr2=READ_LINE_HDR2(fd2);
      char *qual=READ_LINE_QUAL(fd2);
      char* readname=get_readname(hdr,&len,cline,argv[2]);
      if (seq==NULL || hdr2==NULL || qual==NULL ) {
	fprintf(stderr,"Error in file %s, line %lu: file truncated?\n",argv[2],cline);
	exit(1);
      }
      if (validate_entry(hdr,hdr2,seq,qual,cline,argv[2])!=0) {
	exit(1);
      }
      //fprintf(stderr,"Reads processed: %ld\n",sn_index->n_entries);
      // check for duplicates
      if ( (e=lookup_header(sn_index,readname))==NULL ) {
	fprintf(stderr,"Error in file %s, line %lu: unpaired read - %s\n",argv[2],cline,readname);
	exit(1);
      } else {
	ulong key=hashit(readname);
	// remove entry from sn_index
	if (delete(sn_index,key,e)!=e) {
	  fprintf(stderr,"Error in file %s, line %lu: unable to delete entry from sn_index - %s\n",argv[2],cline,readname);
	  exit(1);
	}
	free_indexentry(e);
      }
      PRINT_READS_PROCESSED(cline/4);
      //
      cline+=4;
    }
    printf("\n");
    if (sn_index->n_entries>0 ) {
      fprintf(stderr,"Error in file %s: found %lu unpaired reads\n",argv[1],sn_index->n_entries);
      exit(1);
    }
  }
  printf("OK\n");  
  exit(0);
}
Пример #28
0
void prtvaawcvcfp(aaw_c *aawc, aaw_c *maawc, snodm **stab, aaw_c *vaawc, unsigned htsz, char *tc, char *fname)
{
    /* phased version */
    int i, j;
    int u2=0;
    unsigned tint;
    unsigned commlines=0; // number of comments lines
    snodm *tsnod2;
    int bi, bj, bj2;
    char va1, va2; // these are a1 and a2 in the VCF file
    char ba1, ba2; // these are a1 and a2 in the BGL file
    char fc, sc;

    FILE *fvp=fopen(fname, "w");
    for(i=0;i<vaawc->numl;++i) {
        fc = vaawc->aaw[i]->aw[0]->w[0];
        sc = vaawc->aaw[i]->aw[0]->w[1];
        if(fc == '#') { // a comment, so print VCF as is.
            commlines++;
            for(j=0;j<vaawc->aaw[i]->al;++j) {
                if(sc != '#')
                    fprintf(fvp, (j!=vaawc->aaw[i]->al-1)?"%s\t":"%s\n", vaawc->aaw[i]->aw[j]->w);
                else
                    fprintf(fvp, (j!=vaawc->aaw[i]->al-1)?"%s ":"%s\n", vaawc->aaw[i]->aw[j]->w);
            }
        } else {
            bi=i+2-commlines;
            // seen = 0;
            tint=hashit(aawc->aaw[bi]->aw[1]->w, htsz); // hash the snpname in the BGL file
            // printf("BGL hash on %s\n", aawc->aaw[bi]->aw[1]->w);
            if(stab[tint] == NULL) {
                u2++;
                continue;
            } else {
                tsnod2=stab[tint];
                while( (tsnod2 != NULL) ) {
                    if(!(strcmp(tsnod2->aw->aw[0]->w, aawc->aaw[bi]->aw[1]->w))) {
                        // seen =1;
                        va1 = vaawc->aaw[i]->aw[3]->w[0];
                        va2 = vaawc->aaw[i]->aw[4]->w[0];
                        // fprintf(fvp, "VGT:%c%c ", va1, va2); 
                        for(j=0;j<9;++j) // print straight VCF for first 9
                            fprintf(fvp, "%s\t", vaawc->aaw[i]->aw[j]->w);

                        for(j=9;j<vaawc->aaw[i]->al;++j) {
                            bj=2*(j-8);
                            bj2=bj+1;
                            ba1 = aawc->aaw[bi]->aw[bj]->w[0];
                            ba2 = aawc->aaw[bi]->aw[bj2]->w[0];
                            // fprintf(fvp, "BGT:%c%c ", ba1, ba2); 
                            // if((ba1 == va1) & (ba2 == va2)) // heterozygous in order of va1 and va2
                            //     fprintf(fvp, (j!=vaawc->aaw[i]->al-1)?"%s\t":"%s\n", vaawc->aaw[i]->aw[j]->w);
                            // else if( (ba2 == va1) & (ba1 == va2) ) // heterzygous in opposite order to va1, va2
                            if( (ba2 == va1) & (ba1 == va2) ) // heterzygous in opposite order to va1, va2
                                fprintf(fvp, (j!=vaawc->aaw[i]->al-1)?"%s\t":"%s\n", "1|0");
                            else if( (ba1 == va1) & (ba2 == va1) ) // homoz on a1
                                fprintf(fvp, (j!=vaawc->aaw[i]->al-1)?"%s\t":"%s\n", "0|0");
                            else if( (ba1 == va2) & (ba2 == va2) ) // homoz on a1
                                fprintf(fvp, (j!=vaawc->aaw[i]->al-1)?"%s\t":"%s\n", "1|1");
                            else if( (ba1 == va1) & (ba2 == va2) ) // heteroz on a1
                                fprintf(fvp, (j!=vaawc->aaw[i]->al-1)?"%s\t":"%s\n", "0|1");
                        }
                        break;
                    }
                    tsnod2=tsnod2->n;
                } // given an occupied location, go through all located SNPnames.
            } // finished seeing if there's a hash location for marker on that SNP name
        } // finished distinguishing between VCF comments and data
    } // finished groing through all the lines in VCF
    fclose(fvp);
    return;
}
Пример #29
0
int main(int argc, char **argv ) {
  //long paired=0;
  unsigned long num_reads1=0,
    num_reads2=0;
  
  is_paired_data=FALSE;
  is_interleaved=FALSE;
  fix_dot=FALSE;
  
  int nopt=0;
  int c;
  opterr = 0;

  fprintf(stderr,"Version iRAP %s\n",VERSION);
  
  while ((c = getopt (argc, argv, "f")) != -1)
    switch (c)
      {
      case 'f':
        fix_dot = TRUE;
	fprintf(stderr,"Fixing (-f) enabled: Replacing . by N (creating .fix.gz files)\n");
	++nopt;
        break;
      default:
	++nopt;
        fprintf(stderr,"ERROR: Option -%c invalid\n",optopt);
	exit(1);
      }
  
  if (argc-nopt<2 || argc-nopt>3) {
    fprintf(stderr,"Usage: fastq_info [-f] fastq1 [fastq2 file|pe]\n");
    //fprintf(stderr,"%d",argc);
    exit(1);
  }

  //gzFile fd1=NULL;
  gzFile fd2=NULL;

  if (argc-nopt ==3) {
    is_paired_data=TRUE;
    //fprintf(stderr,"%d %d %d %s\n",argc,nopt,argc-nopt,argv[2+nopt]);
    if ( !strncmp(argv[2+nopt],"pe",2) ) {
      is_interleaved=FALSE;
    } 
    //else  {
    //  fd2=open_fastq(argv[2+nopt]);
    //  gzclose(fd2);
    //
  }

  // ************************************************************
  if ( is_interleaved ) {
    // interleaved    
    num_reads1=validate_interleaved(argv[1+nopt]);
  } else {
    // single or pair of fastq file(s)
    unsigned long cline=1;
    fprintf(stderr,"HASHSIZE=%lu\n",(long unsigned int)HASHSIZE);
    //memset(&collisions[0],0,HASHSIZE+1);
    hashtable sn_index=new_hashtable(HASHSIZE);
    index_mem+=sizeof(hashtable);
    
    index_file(argv[1+nopt],sn_index,0,-1);
    num_reads1=sn_index->n_entries;
    fprintf(stderr,"\n");
    // print some info
    fprintf(stderr,"Reads processed: %ld\n",sn_index->n_entries);    
    fprintf(stderr,"Memory used in indexing: ~%ld MB\n",index_mem/1024/1024);  
    // pair-end
    if (argc-nopt ==3 ) {
      fprintf(stderr,"File %s processed\n",argv[1+nopt]);  
      fprintf(stderr,"Next file %s\n",argv[2+nopt]);  
      // validate the second file and check if all reads are paired
      fd2=open_fastq(argv[2+nopt]);
      gzFile fdf=open_fixed_fastq(argv[2+nopt]);  
      INDEX_ENTRY* e;
      // read the entry using another fd
      cline=1;
      // TODO: improve code - mostly duplicated:(
      while(!gzeof(fd2)) {
	long long start_pos=gztell(fd2);
	char *hdr=READ_LINE_HDR(fd2);
	if ( hdr==NULL) break;
	int len;
	char *seq=READ_LINE_SEQ(fd2);
	char *hdr2=READ_LINE_HDR2(fd2);
	char *qual=READ_LINE_QUAL(fd2);
	char* readname=get_readname(hdr,&len,cline,argv[2+nopt]);
	if (seq==NULL || hdr2==NULL || qual==NULL ) {
	  fprintf(stderr,"\nError in file %s, line %lu: file truncated?\n",argv[2+nopt],cline);
	  exit(1);
	}
	if (validate_entry(hdr,hdr2,seq,qual,cline,argv[2+nopt])!=0) {
	  exit(1);
	}
	//fprintf(stderr,"Reads processed: %ld\n",sn_index->n_entries);
	// check for duplicates
	if ( (e=lookup_header(sn_index,readname))==NULL ) {
	  fprintf(stderr,"\nError in file %s, line %lu: unpaired read - %s\n",argv[2+nopt],cline,readname);
	  exit(1);
	} else {
	  ulong key=hashit(readname);
	  // remove entry from sn_index
	  if (delete(sn_index,key,e)!=e) {
	    fprintf(stderr,"\nError in file %s, line %lu: unable to delete entry from sn_index - %s\n",argv[2+nopt],cline,readname);
	    exit(1);
	  }
	  free_indexentry(e);
	}
	PRINT_READS_PROCESSED(cline/4);
	++num_reads2;
	//
	replace_dots(start_pos,seq,hdr,hdr2,qual,fdf);
	cline+=4;
      }
      printf("\n");
      close_fixed_fastq(fdf);
      if (sn_index->n_entries>0 ) {
	fprintf(stderr,"\nError in file %s: found %lu unpaired reads\n",argv[1+nopt],sn_index->n_entries);
	exit(1);
      }
    }
  }
  FILE* out;  
  if (fix_dot) {
    out=stderr;
  } else {
    out=stdout;
  }
  fprintf(out,"------------------------------------\n");
  if ( num_reads2>0 ) {
    fprintf(out,"Number of reads: %lu %lu\n",num_reads1,num_reads2);
  } else {
    fprintf(out,"Number of reads: %lu\n",num_reads1);
  }
  fprintf(out,"Quality encoding range: %lu %lu\n",min_qual,max_qual);
  char *enc=qualRange2enc(min_qual,max_qual);
  if ( enc == NULL ) {
    fprintf(stderr,"\nERROR: Unable to determine quality encoding - unknown range [%lu,%lu]\n",min_qual,max_qual);
    exit(1);    
  }
  fprintf(out,"Quality encoding: %s\n",qualRange2enc(min_qual,max_qual));
  fprintf(out,"Read length: %lu %lu\n",min_rl,max_rl);
  fprintf(out,"OK\n");  
  exit(0);
}