Example #1
0
LBXGL_Entity *BSP_ParseEntity(char **str)
{
	LBXGL_Entity *tmp;
	char lbuf[256];
	char **as, **as2, *s, *t;
	double *fa;
	void *p;
	int i, n;

	tmp=LBXGL_Entity_New();

	s=*str;
	while(*s)
	{
		t=lbuf;
		while(*s && (*s<=' '))s++;
		while(*s && *s!='\n')*t++=*s++;
		while(*s && (*s<=' '))s++;
		*t++=0;

		as=ksplit(lbuf);

		if(!as[0])continue;
		if(!strcmp(as[0], "//"))continue;
		if(!strcmp(as[0], "}"))break;

		t=as[0];
		if(!(((*t>='a') && (*t<='z')) || ((*t>='A') && (*t<='Z')) ||
			(*t=='_')))
			continue;

		//property
		t=as[1];
		while(*t && (*t<=' '))t++;
		if(((*t>='0') && (*t<='9')) || (*t=='-'))
		{
			as2=ksplit(as[1]);
			for(n=0; as2[n]; n++);

			if(n>1)
			{
				fa=gctalloc("bggl2_numarray_t",
					n*sizeof(double));
				for(i=0; i<n; i++)
					fa[i]=atof(as2[i]);
				p=fa;
			}else p=NetVal_WrapFloat(atof(as2[0]));
		}else p=NetVal_WrapString(as[1]);

		LBXGL_Entity_SetProperty(tmp, as[0], p);
	}

	*str=s;
	return(tmp);
}
Example #2
0
void convert_string_tsv(struct beds_anno_tsv *tsv)
{
    tsv->fields = ksplit(&tsv->string, '\t', &tsv->nfields);
    assert(tsv->nfields > 3);
    tsv->start = atoi(tsv->string.s + tsv->fields[1]);
    tsv->end = atoi(tsv->string.s + tsv->fields[2]);
}
Example #3
0
int main_load_model(char *name)
{
	char buf[256];
	float pos[16];
	char **a;
	char *base, *s;
	FILE *fd;
	int i, j;

	strcpy(buf, name);
	s=buf+strlen(buf);
	while((s>buf) && (*s!='/') && (*s!='\\'))s--;
	*s=0;
	base=strdup(buf);

//	sprintf(buf, "resource/%s", name);
	fd=fopen(name, "rt");
	if(!fd)
	{
		printf("can't open models %s\n", name);
		return(-1);
	}

	printf("loading models %s\n", name);

	while(!feof(fd))
	{
		memset(buf, 0, 256);
		fgets(buf, 255, fd);
		a=ksplit(buf);

		if(!a[0])continue;
		if(!strncmp(a[0], "//", 2))continue;

		if(!strcmp(a[0], "bones"))
		{
			sprintf(buf, "%s/%s", base, a[1]);
			main_load_bones(buf);
//			main_load_bones(a[1]);
		}

		if(!strcmp(a[0], "mesh"))
		{
			for(i=0; i<main_bones; i++)
				if(!strcmp(main_bone_name[i], a[1]))
					break;
			if(i>=main_bones)continue;

			for(j=0; j<16; j++)pos[j]=0;
			for(j=0; j<4; j++)pos[j*4+j]=1;

			for(j=0; j<12; j++)pos[((j/3)*4)+(j%3)]=atof(a[4+j]);

			sprintf(buf, "%s/%s", base, a[2]);
			main_load_mesh(i, buf, pos);
//			main_load_mesh(i, a[2], pos);
		}
	}
	fclose(fd);
}
Example #4
0
int get_lane_id(const bam1_t *b) {
    if(b==NULL) die("get_lane_id: input error");
    kstring_t *name = mycalloc(1, kstring_t);
    name->s = get_pair_name(b);
    name->l = strlen(name->s);
    int *fields, n, i;
    fields = ksplit(name, (int)':', &n);
    if(n==1) return 0;
    int lane_id = 0;
    sscanf(name->s + fields[1], "%d", &lane_id);
    return lane_id;
}
Example #5
0
LBXGL_Entity *BSP_ParseEntity(char **str)
{
	LBXGL_Entity *tmp;
	char lbuf[256];
	char *kbuf[64], *vbuf[64];
	char *cn;
	char **as, **as2, *s, *t;
	double *fa;
	void *p;
	int i, n;

	n=0;
	s=*str;
	while(*s)
	{
		t=lbuf;
		while(*s && (*s<=' '))s++;
		while(*s && *s!='\n')*t++=*s++;
		while(*s && (*s<=' '))s++;
		*t++=0;

		as=ksplit(lbuf);

		if(!as[0])continue;
		if(!strcmp(as[0], "//"))continue;
		if(!strcmp(as[0], "}"))break;

		t=as[0];
		if(!(((*t>='a') && (*t<='z')) || ((*t>='A') && (*t<='Z')) ||
			(*t=='_')))
			continue;

		if(!strcmp(as[0], "classname"))
			cn=kstrdup(as[1]);

		kbuf[n]=kstrdup(as[0]);
		vbuf[n]=kstrdup(as[1]);
		n++;
	}
	*str=s;

	tmp=LBXGL_Entity_NewClass(cn);
	if(!tmp)return(NULL);

	for(i=0; i<n; i++)
	{
		LBXGL_Entity_SetPropertyAsString(tmp, kbuf[i], vbuf[i]);
		gcfree(kbuf[i]);
		gcfree(vbuf[i]);
	}

	return(tmp);
}
Example #6
0
int main_load_anim(char *name)
{
	char buf[256];
	float pos[16];
	char **a;
	FILE *fd;
	int i, j, k;

//	sprintf(buf, "resource/%s", name);
	fd=fopen(name, "rt");
	if(!fd)
	{
		printf("can't open anim %s\n", name);
		return(-1);
	}

	printf("loading anim %s\n", name);

	memset(frame_mask, 0, 256*64/8);
	n_frames=0;

	while(!feof(fd))
	{
		memset(buf, 0, 256);
		fgets(buf, 255, fd);
		a=ksplit(buf);

		if(!a[0])continue;
		if(!strncmp(a[0], "//", 2))continue;

		if(!strcmp(a[0], "frame"))
		{
			frame_cur=atoi(a[1]);
			if((frame_cur+1)>n_frames)n_frames=frame_cur+1;
		}

		for(i=0; i<main_bones; i++)
			if(main_bone_name[i] &&
				!strcmp(main_bone_name[i], a[0]))
					break;
		if(i>=main_bones)continue;

		j=frame_cur*64+i;
		for(k=0; k<3; k++)frame_org[j*3+k]=atof(a[2+k]);
		for(k=0; k<9; k++)frame_rot[j*9+k]=atof(a[7+k]);
		frame_mask[j>>3]|=1<<(j&7);
	}
	fclose(fd);

//	sprintf(buf, "%s.bak", name);
//	main_save_anim(buf);
}
Example #7
0
void main_readtris(char *name)
{
    char buf[256];
    float tv[3];
    FILE *fd;
    char **a;
    int n;

    main_gear_vecs=malloc(65536*sizeof(float));
    main_gear_tris=malloc(65536*sizeof(int));
    main_gear_nvecs=0;
    main_gear_ntris=0;

    fd=fopen(name, "rt");
    if(!fd)
    {
        printf("can't read '%s'\n", name);
        return;
    }

    n=0;
    while(!feof(fd))
    {
        memset(buf, 0, 256);
        fgets(buf, 255, fd);

        a=ksplit(buf);
        if(!a[0])continue;

        tv[0]=atof(a[0]);
        tv[1]=atof(a[1]);
        tv[2]=atof(a[2]);
        main_gear_tris[n*3+0]=main_indexvec(tv);

        tv[0]=atof(a[3]);
        tv[1]=atof(a[4]);
        tv[2]=atof(a[5]);
        main_gear_tris[n*3+1]=main_indexvec(tv);

        tv[0]=atof(a[6]);
        tv[1]=atof(a[7]);
        tv[2]=atof(a[8]);
        main_gear_tris[n*3+2]=main_indexvec(tv);

        n++;
    }

    main_gear_ntris=n;
}
Example #8
0
LBXGL_Entity *BSP_ReadEnts(VFILE *fd)
{
	char buf[256];
	char *tbuf;
	LBXGL_Entity *ents, *etmp, *elst;
	char *str, *s, *t, **a;
	int i, j;

//	BSP_ReadFourcc(fd, FCC_eNts);
	i=BSP_ReadInt32(fd);

	tbuf=gcatomic(i+1);
	memset(tbuf, 0, i+1);
	vfread(tbuf, 1, i, fd);


	ents=NULL; elst=NULL;
	s=tbuf;
	while(*s)
	{
		t=buf;
		while(*s && (*s<=' '))s++;
		while(*s && *s!='\n')*t++=*s++;
		while(*s && (*s<=' '))s++;
		*t++=0;

		a=ksplit(buf);
		if(!a[0])continue;

		if(!strcmp(a[0], "{"))
		{
			etmp=BSP_ParseEntity(&s);
			if(elst)
			{
				elst->next=etmp;
				elst=etmp;
			}else
			{
				ents=etmp;
				elst=etmp;
			}
		}
	}
	gcfree(tbuf);

	return(ents);

}
Example #9
0
void* event_loop(void* args) {

  struct hash_list hash;
  while (1) {
  char command[256];
//    sprintf( command, "grep VmSize /proc/%d/status", getpid() );
//      system( command ); 
    int i;
    for (i=0;i<received_data_queue_tail;++i) {
      char t;
      memcpy(&t,&received_data_queue[received_data_queue_head++],512);

      t_split *directives = ksplit(&t," ");

      if      (strcmp(directives->splited_ary[0],"GET") == 0) {
        char *datagram;
        *datagram = search(directives->splited_ary[1]);


        //memcpy(&send_data_queue[send_data_queue_tail++],datagram,strsize(datagram));
        strcpy(&send_data_queue[send_data_queue_tail++],datagram);
        if (send_data_queue_tail > MAX_SEND_QUEUE) send_data_queue_tail = 0;
      }
      else if (strcmp(directives->splited_ary[0],"SET") == 0) {
        create_hashstruct(&hash,directives->splited_ary[1],directives->splited_ary[2]);
        insert(&hash);
      }
      else {
        printf("purge is not implemented.");
      }
      dump_all();

      printf("%d : %d : %s\n",received_data_queue_head,received_data_queue_tail,&t);
      if (received_data_queue_head>MAX_RECEIVE_QUEUE) received_data_queue_head = 0;
    }
    received_data_queue_head = received_data_queue_tail = 0;
    usleep(1);
  }
}
Example #10
0
void*
sock_boot (void *v_options)
{
  int sfd, s;
  int efd;
  struct epoll_event event;
  struct epoll_event *events;

  s_options* options = (s_options *) v_options;
  sfd = create_and_bind (options->port);
  if (sfd == -1)
    abort ();

  s = make_socket_non_blocking (sfd);
  if (s == -1)
    abort ();

  s = listen (sfd, SOMAXCONN);
  if (s == -1)
    {
      perror ("listen");
      abort ();
    }

  efd = epoll_create (1);
  if (efd == -1)
    {
      perror ("epoll_create");
      abort ();
    }

  event.data.fd = sfd;
  event.events = EPOLLIN | EPOLLET;
  s = epoll_ctl (efd, EPOLL_CTL_ADD, sfd, &event);
  if (s == -1)
    {
      perror ("epoll_ctl");
      abort ();
    }

  /* Buffer where events are returned */
  events = calloc (MAXEVENTS, sizeof event);

  /* The event loop */
  while (1)
    {
      int n, i;

      n = epoll_wait (efd, events, MAXEVENTS, -1);
      for (i = 0; i < n; i++)
	{
	  if ((events[i].events & EPOLLERR) ||
              (events[i].events & EPOLLHUP) ||
              (!(events[i].events & EPOLLIN)))
	    {
              /* An error has occured on this fd, or the socket is not
                 ready for reading (why were we notified then?) */
	      fprintf (stderr, "epoll error\n");
	      close (events[i].data.fd);
	      continue;
	    }

	  else if (sfd == events[i].data.fd)
	    {
              /* We have a notification on the listening socket, which
                 means one or more incoming connections. */
              while (1)
                {
                  struct sockaddr in_addr;
                  socklen_t in_len;
                  int infd;
                  char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];

                  in_len = sizeof in_addr;
                  infd = accept (sfd, &in_addr, &in_len);
                  if (infd == -1)
                    {
                      if ((errno == EAGAIN) ||
                          (errno == EWOULDBLOCK))
                        {
                          /* We have processed all incoming
                             connections. */
                          break;
                        }
                      else
                        {
                          perror ("accept");
                          break;
                        }
                    }

                  s = getnameinfo (&in_addr, in_len,
                                   hbuf, sizeof hbuf,
                                   sbuf, sizeof sbuf,
                                   NI_NUMERICHOST | NI_NUMERICSERV);
                  if (s == 0)
                    {
                      printf("Accepted connection on descriptor %d "
                             "(host=%s, port=%s)\n", infd, hbuf, sbuf);
                    }

                  /* Make the incoming socket non-blocking and add it to the
                     list of fds to monitor. */
                  s = make_socket_non_blocking (infd);
                  if (s == -1)
                    abort ();

                  event.data.fd = infd;
                  event.events = EPOLLIN | EPOLLET;
                  s = epoll_ctl (efd, EPOLL_CTL_ADD, infd, &event);
                  if (s == -1)
                    {
                      perror ("epoll_ctl");
                      abort ();
                    }
                }
              continue;
            }
          else
            {
              /* We have data on the fd waiting to be read. Read and
                 display it. We must read whatever data is available
                 completely, as we are running in edge-triggered mode
                 and won't get a notification again for the same
                 data. */
              int done = 0;

              while (1)
                {
                  ssize_t count;
                  char buf[512];

                  count = read (events[i].data.fd, buf, sizeof buf);
                  if (count == -1)
                    {
                      /* If errno == EAGAIN, that means we have read all
                         data. So go back to the main loop. */
                      if (errno != EAGAIN)
                        {
                          perror ("read");
                          done = 1;
                        }
                      break;
                    }
                  else if (count == 0)
                    {
                      /* End of file. The remote has closed the
                         connection. */
                      done = 1;
                      break;
                    }

                  char c_payload_size[9];
                  memcpy(c_payload_size,&buf[0],8);
                  c_payload_size[8] = '\0';
                  char *e;
                  unsigned long int payload_size = strtoul(c_payload_size,&e,16);
                  clients_data[i].payload_size = payload_size;
                  memcpy(clients_data[i].buffer,&buf[8],payload_size);


                  t_split *directives = ksplit(clients_data[i].buffer,"\x0d\x0a");
                  int j;
                  for (j=0; j < directives->count; ++j) {
  
                    memcpy(&received_data_queue[received_data_queue_tail++],directives->splited_ary[j],payload_size);
                    if (received_data_queue_tail > MAX_RECEIVE_QUEUE) received_data_queue_tail = 0;
                    //strcpy(clients_data[i].buffer,&buf[8]);
  #ifdef DEBUG
                    //printf("payload_size:[%d] | ",payload_size);
                    //printf("clients_data[%d].payload_size:[%lu] | ",i,payload_size);
                    printf("clients_data[%d].buffer:[%s]\n",i,directives->splited_ary[j]);
  #endif
                  }
                }

              if (done)
                {
                  printf ("Closed connection on descriptor %d\n",
                          events[i].data.fd);

                  /* Closing the descriptor will make epoll remove it
                     from the set of descriptors which are monitored. */
                  close (events[i].data.fd);
                }
            }
        }
    }

  free (events);

  close (sfd);

  return EXIT_SUCCESS;
}
Example #11
0
int main()
{
	char buf[256];
	FILE *fd;
	char *s, *t, **a;
	int i, j, k, n;

	fd=fopen(in_txt, "rt");
	if(!fd)
	{
		printf("fail open %s\n", in_txt);
		return(-1);
	}

	nops=0; nregs=0; n=0;
	while(!feof(fd))
	{
		memset(buf, 0, 256);
		fgets(buf, 255, fd);

		s=buf;
		while(*s && (*s<=' '))s++;
		if(!*s)continue;
		if(*s==';')continue;
		if(*s=='#')continue;
		if(*s=='/')continue;

		a=ksplit(s);
		if(!a[0])continue;

		//locate opcode number
		if(buf[0]>' ')
		{
			if(!strcmp(a[0], ".reg"))
			{
				i=nregs++;
				regs[i]=strdup(a[1]);
				regidx[i]=atoi(a[2]);
				continue;
			}

			for(n=0; n<nops; n++)
				if(!strcmp(a[0], ops[n]))
					break;
			if(n>=nops)
			{
				n=nops++;
				ops[n]=strdup(a[0]);
				opidx[n]=ndecls;
			}else if(n<(nops-1))
			{
				printf("discontinuity for op '%s'\n", a[0]);
			}

			a++;
			if(!a[0])continue;
		}

		i=ndecls++;
		opnum[i]=n;
		opdecl[i]=strdup(a[0]);

		opcat[i*16+ 0]=0; opcat[i*16+ 1]=0;
		opcat[i*16+ 2]=0; opcat[i*16+ 3]=0;
		opcat[i*16+ 4]=0; opcat[i*16+ 5]=0;
		opcat[i*16+ 6]=0; opcat[i*16+ 7]=0;
		opcat[i*16+ 8]=0; opcat[i*16+ 9]=0;
		opcat[i*16+10]=0; opcat[i*16+11]=0;
		opcat[i*16+12]=0; opcat[i*16+13]=0;
		opcat[i*16+14]=0; opcat[i*16+15]=0;

		if(a[1] && (a[1][0]!='-'))
		{
			j=0; k=0;

			s=a[1];
			while(*s)
			{
				if(*s==',') { j++; s++; }

				k=get_opcat(&s);
				opcat[i*16+j]=k;
				if(k>=0)continue;

				printf("parse problem op '%s'\n", ops[n]);
				break;
			}

//			opcat[i*4+3]=k;
//			opcat[i*16+7]=0;
		}

		if(a[2] && (a[2][0]!='-'))
		{
			s=a[2]; j=0;
			while(*s)
			{
				if(*s==',') { s++; continue; }
				if(!strncmp(s, "aleph", 5))
					{ j|=OPFL_ALEPH; s+=5; continue; }
				if(!strncmp(s, "long", 4))
					{ j|=OPFL_LONG; s+=4; continue; }
				if(!strncmp(s, "leg", 3))
					{ j|=OPFL_LEG; s+=3; continue; }
				if(!strncmp(s, "vex", 3))
					{ j|=OPFL_VEX; s+=3; continue; }
				if(!strncmp(s, "bet", 3))
					{ j|=OPFL_BET; s+=3; continue; }

				printf("parse problem op '%s' fl '%s' \n", ops[n], a[2]);
				break;
			}

			opcat[i*16+7]=j;
		}
	}
	fclose(fd);


	fd=fopen(out_c, "wt");
	fprintf(fd, "/* Autogenerated source */\n\n");

	fprintf(fd, "#include <stdio.h>\n\n");
//	fprintf(fd, "#ifndef NULL\n");
//	fprintf(fd, "#define NULL ((void *)0)\n");
//	fprintf(fd, "#endif\n\n");

	fprintf(fd, "char *basm_ops[]={\n");
	j=0;
	for(i=0; i<nops; i++)
	{
		strcpy(buf, ops[i]);
		s=buf;
		while(*s)
		{
			if((*s>='A') && (*s<='Z'))
				*s=*s-'A'+'a';
			s++;
		}


		fprintf(fd, "\"%s\", ", buf);
		j+=strlen(buf)+4;

		if(j>64)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "NULL};\n\n");

	fprintf(fd, "int basm_opidx[]={\n");
	j=0;
	for(i=0; i<nops; i++)
	{
		fprintf(fd, "%d, ", opidx[i]);
		j+=3;
		if(opidx[i]>=10)j++;
		if(opidx[i]>=100)j++;

		if(j>64)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "-1};\n\n");


	fprintf(fd, "int basm_opnums[]={\n");
	j=0;
	for(i=0; i<ndecls; i++)
	{
		fprintf(fd, "%d, ", opnum[i]);
		j+=3;
		if(opnum[i]>=10)j++;
		if(opnum[i]>=100)j++;

		if(j>64)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "-1};\n\n");


	fprintf(fd, "char basm_opcat[]={\n");
	j=0;
	for(i=0; i<(16*ndecls); i++)
	{
		fprintf(fd, "%d, ", opcat[i]);
		j+=3;
		if(opcat[i]>=10)j++;
		if(opcat[i]>=100)j++;

		if(j>64)
//		if(1)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "0};\n\n");


	fprintf(fd, "char *basm_opdecl[]={\n");
	j=0;
	for(i=0; i<ndecls; i++)
	{
		s=opdecl[i];
		if(!s)s="";

		fprintf(fd, "\"%s\", ", s);
		j+=strlen(s)+4;

		if(j>64)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "NULL};\n\n");

	fprintf(fd, "char *basm_regs[]={\n");
	j=0;
	for(i=0; i<nregs; i++)
	{
		strcpy(buf, regs[i]);
		s=buf;
		while(*s)
		{
			if((*s>='A') && (*s<='Z'))
				*s=*s-'A'+'a';
			s++;
		}


		fprintf(fd, "\"%s\", ", buf);
		j+=strlen(buf)+4;

		if(j>64)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "NULL};\n\n");

	fprintf(fd, "int basm_regidx[]={\n");
	j=0;
	for(i=0; i<nregs; i++)
	{
		fprintf(fd, "%d, ", regidx[i]);
		j+=3;
		if(regidx[i]>=10)j++;
		if(regidx[i]>=100)j++;

		if(j>64)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "-1};\n\n");

	fclose(fd);



	fd=fopen(out_h, "wt");
	fprintf(fd, "/* Autogenerated header */\n\n");

	for(i=0; i<nops; i++)
	{
		strcpy(buf, ops[i]);
		s=buf;
		while(*s)
		{
			if((*s>='a') && (*s<='z'))
				*s=*s-'a'+'A';
			s++;
		}

//		j=(31-strlen(buf))/8;
		j=(63-(strlen(buf)+strlen("#define BASM_OP_")))/8;
		fprintf(fd, "#define BASM_OP_%s", buf, i);
		while((j--)>0)fprintf(fd, "\t");
		fprintf(fd, "%d\n", i);
	}

	for(i=0; i<nregs; i++)
	{
		strcpy(buf, regs[i]);
		s=buf;
		while(*s)
		{
			if((*s>='a') && (*s<='z'))
				*s=*s-'a'+'A';
			s++;
		}

		j=(63-(strlen(buf)+strlen("#define BASM_REG_")))/8;
		fprintf(fd, "#define BASM_REG_%s", buf);
		while((j--)>0)fprintf(fd, "\t");
		fprintf(fd, "%d\n", regidx[i]);
	}

	fclose(fd);

	return(0);
}
Example #12
0
int main_load_bones(char *name)
{
	char buf[256];
	char **a;
	FILE *fd;
	int i, j;
	float *v;

//	sprintf(buf, "resource/%s", name);
	fd=fopen(name, "rt");
	if(!fd)
	{
		printf("can't open bones %s\n", name);
		return(-1);
	}

	printf("loading bones %s\n", name);

	for(i=0; i<64; i++)
		main_bone_name[i]=NULL;

	main_bones=0;
	while(!feof(fd))
	{
		memset(buf, 0, 256);
		fgets(buf, 255, fd);
		a=ksplit(buf);

		if(!a[0])continue;
		if(!strncmp(a[0], "//", 2))continue;

		if(!strcmp(a[0], "bbox"))
		{
			bone_solid[bone_solids].type=BONE_SOLID_BBOX;
			bone_solid[bone_solids].bone=atoi(a[1]);

			v=main_bone_rorg+bone_solid[bone_solids].bone*3;
			bone_solid[bone_solids].m[0]=atof(a[3])-v[0];
			bone_solid[bone_solids].m[1]=atof(a[4])-v[1];
			bone_solid[bone_solids].m[2]=atof(a[5])-v[2];
			bone_solid[bone_solids].n[0]=atof(a[8])-v[0];
			bone_solid[bone_solids].n[1]=atof(a[9])-v[1];
			bone_solid[bone_solids].n[2]=atof(a[10])-v[2];

			bone_solid[bone_solids].r0=atof(a[12]);
			bone_solid[bone_solids].w=atof(a[13]);

			bone_solids++;
		}

		if(!strcmp(a[0], "capsule"))
		{
			bone_solid[bone_solids].type=BONE_SOLID_CAPSULE;
			bone_solid[bone_solids].bone=atoi(a[1]);

			v=main_bone_rorg+bone_solid[bone_solids].bone*3;
			bone_solid[bone_solids].m[0]=atof(a[3])-v[0];
			bone_solid[bone_solids].m[1]=atof(a[4])-v[1];
			bone_solid[bone_solids].m[2]=atof(a[5])-v[2];
			bone_solid[bone_solids].n[0]=atof(a[8])-v[0];
			bone_solid[bone_solids].n[1]=atof(a[9])-v[1];
			bone_solid[bone_solids].n[2]=atof(a[10])-v[2];

			bone_solid[bone_solids].r0=atof(a[12]);
			bone_solid[bone_solids].r1=atof(a[13]);
			bone_solid[bone_solids].w=atof(a[14]);

			bone_solids++;
		}

		if(!strcmp(a[0], "sphere"))
		{
			bone_solid[bone_solids].type=BONE_SOLID_SPHERE;
			bone_solid[bone_solids].bone=atoi(a[1]);

			v=main_bone_rorg+bone_solid[bone_solids].bone*3;
			bone_solid[bone_solids].m[0]=atof(a[3])-v[0];
			bone_solid[bone_solids].m[1]=atof(a[4])-v[1];
			bone_solid[bone_solids].m[2]=atof(a[5])-v[2];

			bone_solid[bone_solids].r0=atof(a[7]);
			bone_solid[bone_solids].r1=atof(a[8]);
			bone_solid[bone_solids].w=atof(a[9]);

			bone_solids++;
		}

		if((*a[0]<'0') || (*a[0]>'9'))
			continue;

		i=atoi(a[0]);
		if(i>=main_bones)main_bones=i+1;

		j=atoi(a[1]);
		main_bone_up[i]=j;
		main_bone_name[i]=strdup(a[2]);

		main_bone_rorg[i*3+0]=atof(a[4]);
		main_bone_rorg[i*3+1]=atof(a[5]);
		main_bone_rorg[i*3+2]=atof(a[6]);

		if(j>=0)
		{
			main_bone_org[i*3+0]=main_bone_rorg[i*3+0]-
				main_bone_rorg[j*3+0];
			main_bone_org[i*3+1]=main_bone_rorg[i*3+1]-
				main_bone_rorg[j*3+1];
			main_bone_org[i*3+2]=main_bone_rorg[i*3+2]-
				main_bone_rorg[j*3+2];
		}else
		{
			main_bone_org[i*3+0]=main_bone_rorg[i*3+0];
			main_bone_org[i*3+1]=main_bone_rorg[i*3+1];
			main_bone_org[i*3+2]=main_bone_rorg[i*3+2];
		}

		for(j=0; j<9; j++)main_bone_rot[i*9+j]=0;
		for(j=0; j<3; j++)main_bone_rot[i*9+j*3+j]=1;

		for(j=0; j<9; j++)main_bone_rmat[i*9+j]=0;
		for(j=0; j<3; j++)main_bone_rmat[i*9+j*3+j]=1;
	}
	fclose(fd);

	printf("skel loaded, %d bones, %d solids\n", main_bones, bone_solids);
}
Example #13
0
int main_load_mesh(int num, char *name, float *pos)
{
	static float vecs[4096*3];
	static int tris[4096*3];
	char buf[256];
	float tv[16];
	char **a;
	FILE *fd;
	int i, j, nv, nt;

	if(!stricmp(vfgetext(name), "ac"))
	{
		Mat4F_Copy(pos, tv);
		V4F_COPY(pos+0*4, tv+1*4);
		V4F_COPY(pos+1*4, tv+0*4);

		mesh_grp[num]=AC3D_LoadModelPos(name, tv);
		return(0);
	}

//	sprintf(buf, "resource/%s", name);
	fd=fopen(name, "rt");
	if(!fd)
	{
		printf("can't open mesh %s\n", name);
		return(-1);
	}

	printf("loading mesh %s\n", name);

	nv=0; nt=0;
	while(!feof(fd))
	{
		memset(buf, 0, 256);
		fgets(buf, 255, fd);
		a=ksplit(buf);

		if(!a[0])continue;
		if(!strncmp(a[0], "//", 2))continue;

		for(i=0; i<3; i++)
		{
			tv[i*3+0]=atof(a[i*3+2]);
			tv[i*3+1]=atof(a[i*3+0]);
			tv[i*3+2]=atof(a[i*3+1]);
		}

		for(i=0; i<3; i++)
		{
			for(j=0; j<nv; j++)
				if(V3F_DIST(tv+i*3, vecs+j*3)<0.001)
					break;
			if(j==nv)
			{
				V3F_COPY(tv+i*3, vecs+j*3);
				nv++;
			}

			tris[nt*3+i]=j;
		}
		nt++;
	}

	printf("mesh %d vecs, %d tris\n", nv, nt);

	mesh_vecs[num]=malloc(nv*3*sizeof(float));
	mesh_tris[num]=malloc(nt*3*sizeof(int));
	mesh_nvecs[num]=nv;
	mesh_ntris[num]=nt;

	for(i=0; i<nv; i++)
	{
		mesh_vecs[num][i*3+0]=vecs[i*3+0]*pos[0]+vecs[i*3+1]*pos[4]+
			vecs[i*3+2]*pos[8]+pos[12];
		mesh_vecs[num][i*3+1]=vecs[i*3+0]*pos[1]+vecs[i*3+1]*pos[5]+
			vecs[i*3+2]*pos[9]+pos[13];
		mesh_vecs[num][i*3+2]=vecs[i*3+0]*pos[2]+vecs[i*3+1]*pos[6]+
			vecs[i*3+2]*pos[10]+pos[14];
	}

	for(i=0; i<nt; i++)
	{
		mesh_tris[num][i*3+0]=tris[i*3+0];
		mesh_tris[num][i*3+1]=tris[i*3+1];
		mesh_tris[num][i*3+2]=tris[i*3+2];
	}

	fclose(fd);
}
Example #14
0
int main()
{
	char buf[256];
	FILE *fd;
	char *s, *t, **a;
	int i, j, k, n;

	fd=fopen(in_txt, "rt");
	if(!fd)
	{
		printf("fail open %s\n", in_txt);
		return(-1);
	}

	nops=0; n=0;
	while(!feof(fd))
	{
		memset(buf, 0, 256);
		fgets(buf, 255, fd);

		s=buf;
		while(*s && (*s<=' '))s++;
		if(!*s)continue;
		if(*s==';')continue;
		if(*s=='#')continue;
		if(*s=='/')continue;

		a=ksplit(s);
		if(!a[0])continue;

		//locate opcode number
		if(buf[0]>' ')
		{
			for(n=0; n<nops; n++)
				if(!strcmp(a[0], ops[n]))
					break;
			if(n>=nops)
			{
				n=nops++;
				ops[n]=strdup(a[0]);
				opidx[n]=ndecls;
			}

			a++;
			if(!a[0])continue;
		}

		i=ndecls++;
		opnum[i]=n;
		opdecl[i]=strdup(a[0]);
		opcat[i*4+0]=0; opcat[i*4+1]=0;
		opcat[i*4+2]=0; opcat[i*4+3]=0;

		if(a[1])
		{
			j=0; k=0;

			s=a[1];
			while(*s)
			{
				if(*s==',') { j++; s++; }
				if(!strncmp(s, "i8", 2))
					{ opcat[i*4+j]=OPCAT_IMM; s+=2; 
						if(j && (k!=8))k|=1;
							else k=8;
						continue; }
				if(!strncmp(s, "i16", 3))
					{ opcat[i*4+j]=OPCAT_IMM; s+=3;
						if(j && (k!=16))k|=2;
							else k=16;
						continue; }
				if(!strncmp(s, "i32", 3))
					{ opcat[i*4+j]=OPCAT_IMM; s+=3;
//					if(!j)k=32;
					if(j && (k!=32))k|=4;
						else k=32;
					continue; }
				if(!strncmp(s, "i64", 3))
					{ opcat[i*4+j]=OPCAT_IMM; s+=3; k=64;
						continue; }

				if(!strncmp(s, "r8", 2))
					{ opcat[i*4+j]=OPCAT_REG; s+=2; k=8;
						continue; }
				if(!strncmp(s, "r16", 3))
					{ opcat[i*4+j]=OPCAT_REG; s+=3; k=16;
						continue; }
				if(!strncmp(s, "r32", 3))
					{ opcat[i*4+j]=OPCAT_REG; s+=3; k=32;
						continue; }
				if(!strncmp(s, "r64", 3))
					{ opcat[i*4+j]=OPCAT_REG; s+=3; k=64;
						continue; }

				if(!strncmp(s, "rm8", 3))
					{ opcat[i*4+j]=OPCAT_RM; s+=3; k=8;
						continue; }
				if(!strncmp(s, "rm16", 4))
					{ opcat[i*4+j]=OPCAT_RM; s+=4; k=16;
						continue; }
				if(!strncmp(s, "rm32", 4))
					{ opcat[i*4+j]=OPCAT_RM; s+=4; k=32;
						continue; }
				if(!strncmp(s, "rm64", 4))
					{ opcat[i*4+j]=OPCAT_RM; s+=4; k=64;
						continue; }

				if(!strncmp(s, "al", 2))
					{ opcat[i*4+j]=OPCAT_EAX; s+=2; k=8;
						continue; }
				if(!strncmp(s, "ax", 2))
					{ opcat[i*4+j]=OPCAT_EAX; s+=2; k=16;
						continue; }
				if(!strncmp(s, "eax", 3))
					{ opcat[i*4+j]=OPCAT_EAX; s+=3; k=32;
						continue; }
				if(!strncmp(s, "rax", 3))
					{ opcat[i*4+j]=OPCAT_EAX; s+=3; k=64;
						continue; }

				if(!strncmp(s, "ar8", 3))
					{ opcat[i*4+j]=OPCAT_REL; s+=3; k=8;
						continue; }
				if(!strncmp(s, "ar16", 4))
					{ opcat[i*4+j]=OPCAT_REL; s+=4; k=16;
						continue; }
				if(!strncmp(s, "ar32", 4))
					{ opcat[i*4+j]=OPCAT_REL; s+=4; k=32;
						continue; }
				if(!strncmp(s, "ar64", 4))
					{ opcat[i*4+j]=OPCAT_REL; s+=4; k=64;
						continue; }

				if(!strncmp(s, "sr", 2))
					{ opcat[i*4+j]=OPCAT_SR; s+=2; k=16;
						continue; }
				if(!strncmp(s, "cr", 2))
					{ opcat[i*4+j]=OPCAT_CR; s+=2;
						continue; }
				if(!strncmp(s, "dr", 2))
					{ opcat[i*4+j]=OPCAT_DR; s+=2;
						continue; }

				if(!strncmp(s, "mo16", 4))
					{ opcat[i*4+j]=OPCAT_MOFFS16; s+=4;
						continue; }
				if(!strncmp(s, "mo32", 4))
					{ opcat[i*4+j]=OPCAT_MOFFS32; s+=4;
						continue; }
				if(!strncmp(s, "mo64", 4))
					{ opcat[i*4+j]=OPCAT_MOFFS64; s+=4;
						continue; }

				if(!strncmp(s, "m", 1))
					{ opcat[i*4+j]=OPCAT_MEM; s+=1; k=0;
						continue; }

				if(!strncmp(s, "frm", 3))
					{ opcat[i*4+j]=OPCAT_FRM; s+=3;
						continue; }
				if(!strncmp(s, "fr2", 3))
					{ opcat[i*4+j]=OPCAT_FRM; s+=3;
						continue; }
				if(!strncmp(s, "fr", 2))
					{ opcat[i*4+j]=OPCAT_FREG; s+=2;
						continue; }
				if(!strncmp(s, "xrm", 3))
					{ opcat[i*4+j]=OPCAT_XRM; s+=3;
						continue; }
				if(!strncmp(s, "xr2", 3))
					{ opcat[i*4+j]=OPCAT_XRM; s+=3;
						continue; }
				if(!strncmp(s, "xr", 2))
					{ opcat[i*4+j]=OPCAT_XREG; s+=2;
						continue; }

				printf("parse problem op %s\n", ops[n]);
				break;
			}

			opcat[i*4+2]=k;
		}
	}
	fclose(fd);


	fd=fopen(out_c, "wt");
	fprintf(fd, "/* Autogenerated source */\n\n");

	fprintf(fd, "char *basm_ops[]={\n");
	j=0;
	for(i=0; i<nops; i++)
	{
		strcpy(buf, ops[i]);
		s=buf;
		while(*s)
		{
			if((*s>='A') && (*s<='Z'))
				*s=*s-'A'+'a';
			s++;
		}


		fprintf(fd, "\"%s\", ", buf);
		j+=strlen(buf)+4;

		if(j>64)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "\"\"};\n\n");

	fprintf(fd, "int basm_opidx[]={\n");
	j=0;
	for(i=0; i<nops; i++)
	{
		fprintf(fd, "%d, ", opidx[i]);
		j+=3;
		if(opidx[i]>=10)j++;
		if(opidx[i]>=100)j++;

		if(j>64)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "-1};\n\n");


	fprintf(fd, "int basm_opnums[]={\n");
	j=0;
	for(i=0; i<ndecls; i++)
	{
		fprintf(fd, "%d, ", opnum[i]);
		j+=3;
		if(opnum[i]>=10)j++;
		if(opnum[i]>=100)j++;

		if(j>64)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "-1};\n\n");


	fprintf(fd, "char basm_opcat[]={\n");
	j=0;
	for(i=0; i<(4*ndecls); i++)
	{
		fprintf(fd, "%d, ", opcat[i]);
		j+=3;
		if(opcat[i]>=10)j++;
		if(opcat[i]>=100)j++;

		if(j>64)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "0};\n\n");


	fprintf(fd, "char *basm_opdecl[]={\n");
	j=0;
	for(i=0; i<ndecls; i++)
	{
		s=opdecl[i];
		if(!s)s="";

		fprintf(fd, "\"%s\", ", s);
		j+=strlen(s)+4;

		if(j>64)
		{
			fprintf(fd, "\n");
			j=0;
		}
	}
	fprintf(fd, "\"\"};\n\n");

	fclose(fd);



	fd=fopen(out_h, "wt");
	fprintf(fd, "/* Autogenerated header */\n\n");

	for(i=0; i<nops; i++)
	{
		strcpy(buf, ops[i]);
		s=buf;
		while(*s)
		{
			if((*s>='a') && (*s<='z'))
				*s=*s-'a'+'A';
			s++;
		}

		j=(31-strlen(buf))/8;
		fprintf(fd, "#define BASM_OP_%s", buf, i);
		while(j--)fprintf(fd, "\t");
		fprintf(fd, "%d\n", i);
	}
	fclose(fd);

	return(0);
}
Example #15
0
int main()
{
	char buf[256];
	FILE *fd;
	char *s, *t, **a;
	int i, j, k, n;

	memset(ops, 0, 16384*sizeof(char *));
	memset(args, 0, 16384*sizeof(char *));

	fd=fopen(in_txt, "rt");
	if(!fd)
	{
		printf("fail open %s\n", in_txt);
		return(-1);
	}

	n=0;
	while(!feof(fd))
	{
		memset(buf, 0, 256);
		fgets(buf, 255, fd);

		s=buf;
		while(*s && (*s<=' '))s++;
		if(!*s)continue;
		if(*s==';')continue;
		if(*s=='#')continue;
		if(*s=='/')continue;

		a=ksplit(s);
		if(!a[0])continue;

		if(!strcmp(a[1], "ignore"))
			continue;
		if(!strcmp(a[1], "void"))
			a[1]="";

		ops[n]=strdup(a[0]);
		args[n]=strdup(a[1]);
		decl[n]=strdup(a[2]);
		n++;
	}
	fclose(fd);

	fd=fopen(out_c, "wt");
	fprintf(fd, "/* Autogenerated source */\n\n");

	fprintf(fd, "char *bs1_jit_strs[]={\n");
	j=0;
	for(i=0; i<n; i++)
	{
		s=ops[i];
		if(!s)s="";

		strcpy(buf, s);
		s=buf;
		while(*s)
		{
			if((*s>='A') && (*s<='Z'))
				*s=*s-'A'+'a';
			s++;
		}


		fprintf(fd, "\"%s\", \"%s\", \"%s\",\n",
			buf, args[i], decl[i]);

	}
	fprintf(fd, "\"\"};\n\n");
	fclose(fd);

	return(0);
}
Example #16
0
int beds_database_add(struct beds_options *opts, const char *fname, char *columns)
{
    if ( opts->n_files == opts->m_files ) {
	opts->m_files = opts->m_files == 0 ? 2 : opts->m_files +2;
	opts->files = (struct beds_anno_file*)realloc(opts->files, opts->m_files*sizeof(struct beds_anno_file));	
    }
    struct beds_anno_file *file = &opts->files[opts->n_files];
    memset(file, 0, sizeof(struct beds_anno_file));
    file->id = opts->n_files;
    file->fname = strdup(fname);
    file->fp = hts_open(fname, "r");
    if (file->fp == NULL)
	error("Failed to open %s : %s", fname, strerror(errno));
    // int n;
    file->idx = tbx_index_load(fname);
    if ( file->idx == NULL)
	error("Failed to load index of %s.", fname);
    opts->n_files++;
    
    file->last_id = -1;
    file->last_start = -1;
    file->last_end = -1;
    kstring_t string = KSTRING_INIT;
    int no_columns = 0;
    int i;
    if ( columns == NULL && file->no_such_chrom == 0) {
	warnings("No columns string specified for %s. Will annotate all tags in this data.", fname);
        file->no_such_chrom = 1;
	no_columns = 1;
    } else {
	int *splits = NULL;
	kputs(columns, &string);
	int nfields;
	splits = ksplit(&string, ',', &nfields);
	file->m_cols = nfields;
	file->cols = (struct anno_col*)malloc(sizeof(struct anno_col) * file->m_cols);

	for ( i = 0; i < nfields; ++i ) {
	    char *ss = string.s + splits[i];
	    struct anno_col *col = &file->cols[file->n_cols];
	    col->icol = i;
	    col->replace = REPLACE_MISSING;
	    if (*ss == '+') {
		col->replace = REPLACE_MISSING;
		ss++;
	    } else if ( *ss == '-' ) {
		col->replace = REPLACE_EXISTING;
		ss++;
	    }
	    if (ss[0] == '\0')
		continue;
	    if ( strncmp(ss, "INFO/", 5) == 0)
		ss += 5;
	    col->hdr_key = strdup(ss);	    
	    col->icol = -1;
	    // debug_print("%s, %d", col->hdr_key, file->n_cols);
	    file->n_cols++;	    
	}
	string.l = 0;	    
    }

    while (1) {
	string.l =0;
	if ( hts_getline(file->fp, KS_SEP_LINE, &string) < 0 )
	    break;
	// only accept header line in the beginning for file
	if ( string.s[0] != '#' )
	    break;
	if ( strncmp(string.s, "##INFO=", 7) == 0) {
	    char *ss = string.s + 11;
	    char *se = ss;
	    while (se && *se != ',') se++;
	    struct anno_col *col = NULL;
	    // if no column string specified, init all header lines
	    if ( no_columns ) {
		if ( file->n_cols == file->m_cols ) {
		    file->m_cols = file->m_cols == 0 ? 2 : file->m_cols + 2;
		    file->cols = (struct anno_col *) realloc(file->cols, file->m_cols*sizeof(struct anno_col));
		}
		col = &file->cols[file->n_cols++];
		col->icol = -1;
		col->hdr_key = strndup(ss, se-ss+1);
		col->hdr_key[se-ss] = '\0';
	    } else {
		for ( i = 0; i < file->n_cols; ++i ) {		    
		    if ( strncmp(file->cols[i].hdr_key, ss, se-ss) == 0)
			break;
		}
		// if header line is not set in the column string, skip
		if ( i == file->n_cols )
		    continue;
		col = &file->cols[i];
	    }

	    // specify setter functions here
	    col->setter.bed = beds_setter_info_string;
	    
	    bcf_hdr_append(opts->hdr_out, string.s);
	    bcf_hdr_sync(opts->hdr_out);
	    int hdr_id = bcf_hdr_id2int(opts->hdr_out, BCF_DT_ID,col->hdr_key);
	    assert ( bcf_hdr_idinfo_exists(opts->hdr_out, BCF_HL_INFO, hdr_id) );
	}
	string.l = 0;
	// set column number for each col
	if ( strncasecmp(string.s, "#chr", 4) == 0) {
	    int nfields;	    
	    int *splits = ksplit(&string, '\t', &nfields);

	    if (nfields < 4) {
		fprintf(stderr, "[error] Bad header of bed database : %s. n_fields : %d, %s", fname, nfields, string.s);
		fprintf(stderr, "[notice] this error usually happened because the header line is seperated by spaces but not tab!");
		exit(1);
	    }
	    int k;
	    for ( k = 3; k < nfields; ++k ) {
		char *ss = string.s + splits[k];
		for (i = 0; i < file->n_cols; ++i ) {
		    struct anno_col *col = &file->cols[i];
		    if ( strcmp(col->hdr_key, ss) == 0)
			break;
		}
		// if name line specify more names than column string or header, skip
		if ( i == file->n_cols )
		    continue;

		struct anno_col *col = &file->cols[i];
		col->icol = k;
	    }
	}
    }
    for ( i = 0; i < file->n_cols; ++i ) {
	struct anno_col *col = &file->cols[i];
	if ( col->hdr_key && col->icol == -1 )
	    error("No column %s found in bed database : %s", col->hdr_key, fname);

	int hdr_id = bcf_hdr_id2int(opts->hdr_out, BCF_DT_ID, col->hdr_key);
        assert(hdr_id>-1);
	col->number = bcf_hdr_id2length(opts->hdr_out, BCF_HL_INFO, hdr_id);
	if ( col->number == BCF_VL_A || col->number == BCF_VL_R || col->number == BCF_VL_G)
	    error("Only support fixed INFO number for bed database. %s", col->hdr_key);
	col->ifile = file->id;
    }
    if ( string.m )
	free(string.s);
    if ( opts->beds_is_inited == 0 )
	opts->beds_is_inited = 1;
    return 0;
}