Example #1
0
ifq_codes_t
ifq_query_index(ifq_index_t *index, char *query, ifq_record_t *record)
{
    // Find key
    unsigned int id = cmph_search( index->hash, query, (cmph_uint32) strlen( query ) );
    uint64_t pos = index->table[ id ];
    if( bgzf_seek( index->fastq_file, pos, SEEK_SET ) < 0 )
    {
        return IFQ_NOT_FOUND;
    }

    cmph_uint32 length;
    read_one_line( &record->name, &length, index->fastq_file );
    if( strncmp( record->name, query, length ) == 0 )
    {
        read_one_line( &record->sequence, &length, index->fastq_file );
        read_one_line( &record->quality, &length, index->fastq_file );
        read_one_line( &record->quality, &length, index->fastq_file );
    }
    else
    {
        return IFQ_NOT_FOUND;
    }

    return IFQ_OK;
}
Example #2
0
isc_result_t
perf_datafile_next(perf_datafile_t *dfile, isc_buffer_t *lines,
		   isc_boolean_t is_update)
{
	const char *current;
	isc_result_t result;

	LOCK(&dfile->lock);

	if (dfile->maxruns > 0 && dfile->maxruns == dfile->nruns) {
		result = ISC_R_EOF;
		goto done;
	}

	result = read_one_line(dfile, lines);
	if (result == ISC_R_EOF) {
		if (!dfile->read_any) {
			result = ISC_R_INVALIDFILE;
			goto done;
		}
		if (dfile->maxruns != dfile->nruns) {
			reopen_file(dfile);
			result = read_one_line(dfile, lines);

		}
	}
	if (result != ISC_R_SUCCESS) {
		goto done;
	}
	dfile->read_any = ISC_TRUE;

	if (is_update) {
		while (ISC_TRUE) {
			current = isc_buffer_used(lines);
			result = read_one_line(dfile, lines);
			if (result == ISC_R_EOF &&
			    dfile->maxruns != dfile->nruns) {
				reopen_file(dfile);
			}
			if (result != ISC_R_SUCCESS ||
			    strcasecmp(current, "send") == 0)
				break;
		};
	}

	result = ISC_R_SUCCESS;
 done:
	UNLOCK(&dfile->lock);
	return (result);
}
Example #3
0
void
populate_index(uint64_t *table, cmph_t *hash, BGZF *fastq_file)
{
    while( 1 )
    {
        /* Find @ */
        char c;
        while( ( c = bgzf_getc( fastq_file ) ) != '@' && c >= 0 )
        {
        }
        
        long pos = bgzf_tell( fastq_file );
        if( pos == -1 )
        {
            break;
        }

        char *accession = NULL;
        cmph_uint32 accession_length;
        if( read_one_line( &accession, &accession_length, fastq_file ) != 1 )
        {
            break;
        }

        /* Next char is sequence, save pos */
        unsigned int id = cmph_search( hash, accession, accession_length );
        table[ id ] = (uint64_t) pos;
    }
}
Example #4
0
char		*my_read(int fd)
{
  static int	i = -1;
  static char	buff[BUFF_SIZE + 1];
  int		ret;

  if (i == -1 || buff[i] == '\0')
    {
      i = 0;
      while (buff[i + 1])
        buff[i++] = '\0';
      ret = read(fd, buff, BUFF_SIZE);
      if (ret < 0)
        {
          msg_error("read error.\n");
          return (NULL);
        }
      if (ret == 0)
        return (NULL);
      buff[ret] = '\0';
    }
  ret = i;
  i = end_of_line(buff + i) + i + 1;
  return (read_one_line(buff, ret));
}
Example #5
0
char		*my_read(int fd, int ret)
{
  static int	i = 0;
  static char	buff[4097];

  if (i == 0 || buff[i] == '\0')
    {
      i = 0;
      if ((ret = read(fd, buff, 4096)) > 0 && !my_str_isprintable(buff))
	ret = my_fread(buff, fd);
      if (ret <= 0)
        {
	  if (ret)
	    my_warn(NULL, fd);
          return (NULL);
        }
      buff[ret] = '\0';
    }
  ret = i;
  while (buff[i] && buff[i] != '\n' && buff[i] != ';')
    jump_inhibitors(buff, &i);
  if (buff[i])
    i = i + 1;
  return (read_one_line(buff + ret));
}
Example #6
0
/*
**		While it can read something:
**		1 => Treats datas line by line.
**		2 => Read again
*/
char		*my_read(int fd)
{
  static int	i = 0;
  static char	buff[BUFF_SIZE + 1];
  int		ret;

  if (i == 0 || buff[i] == '\0')
    {
      i = 0;
      while (buff[i] && i < BUFF_SIZE)
        buff[i++] = '\0';
      i = 0;
      ret = read(fd, buff, BUFF_SIZE);
      if (ret < 0 && msg_error("read error.\n"))
	return (NULL);
      if (ret == 0)
        return (NULL);
      buff[ret] = '\0';
    }
  ret = i;
  while (buff[i] && buff[i] != '\n')
    i = i + 1;
  if (buff[i])
    i = i + 1;
  return (read_one_line(buff + ret));
}
void read_obj_func(input_file *input, FILE *fp){   //目的関数の読み込み
	int i;
	read_one_line(fp,input->line);    /* ファイルからデータを1行読み込む */
	/* 目的関数の’Z’および’=’を読み込む */
	input->string = strtok(input->line," ");
	assert(input->string[0]=='z'||input->string[0]=='Z');
	/* 1文字目がZでなければプログラムを終了する */
	printf("%c ",input->string[0]);
	input->string=strtok(NULL," ");   /* 引き続きスペースの前まで読み込む */
	assert(input->string[0]=='=');
	/* 次の1文字目が=でなければプログラムを終了する */
	printf("%c ",input->string[0]);
	/* 目的関数の係数を読み込む */
	for(i=1; i<=n; i++){
      input->string=strtok(NULL," ");		 /* トークンを読み込む */
      input->x_sub = read_one_token(input->string, &input->coef);
      /* 読み込んだトークンを構文解析する */
      c[input->x_sub]= input->coef;
      /* 目的関数の係数Cに代入する */
      printf("%6.2fX%d ",input->coef,input->x_sub);
   }
	/* 目的関数の符号(最大化or最小化)を読み込む*/
	input->string=strtok(NULL," ");
	if((input->string[1]=='a')||(input->string[1]=='A')){
      max_min = max;
      printf(" MAX\n");
   }else if((input->string[1]=='i')||(input->string[1]=='I')){
      max_min = min;
      printf(" MIN\n");
   }else{
		/* エラーコードの出力 */
      printf("目的関数の最大化or最小化のデータが不正です!!\n");
      assert((input->string[1]=='a')&&(input->string[1]=='A')&&(input->string[1]=='i')&&(input->string[1]=='I'));
   }
}
Example #8
0
void read_user_data() {
	FILE* fp = fopen("database/user.txt", "r");

	if (fp != NULL) {
		char *id, *register_date, *name;

		while (1) {
			id = read_one_line(fp);
			register_date = read_one_line(fp);
			name = read_one_line(fp);

			if (id == NULL || register_date == NULL || name == NULL)
				break;

			user_insert_node(id, register_date, name);
		}
		fclose(fp);
	}
}
Example #9
0
void read_frineds_data() {
	char* previous_user1 = "";
	char* previous_user2 = "";

	total_friendship = 0;

	FILE* fp = fopen("database/friend.txt", "r");

	if (fp != NULL) {
		char *user1, *user2;

		while (1) {
			user1 = read_one_line(fp);
			user2 = read_one_line(fp);

			if (user1 == NULL || user2 == NULL)
				break;

			if (strcmp(previous_user1, user1) != 0 || strcmp(previous_user2, user2) != 0) {
				User_Node user1_node = user_search_node(user1);
				User_Node user2_node = user_search_node(user2);

				if (user1_node == NULL || user2_node == NULL)
					continue;

				//add_friend(user1_node->friends, user2_node->friends);
				add_friend(user2_node->follower, user1_node->follower);
				total_friendship++;

				previous_user1 = user1;
				previous_user2 = user2;
			}
		}
		fclose(fp);
	}
}
Example #10
0
int key_fastq_read(void *data, char **key, cmph_uint32 *keylen)
{
    BGZF *fp = (BGZF *) data;

    char c;
    *keylen = 0;
    /* Find header start */
    while( ( c = bgzf_getc( fp ) ) != '@' && c >= 0 )
    { }

    *key = NULL;
    if( read_one_line( key, keylen, fp ) == 1 )
    {
        return (int) *keylen;
    }
    else
    {
        return -1;
    }
}
Example #11
0
int main(int argc, char *argv[])
{
    int sockfd;

    if (argc < 3) {
       usage();
       exit(0);
    }

    if( (sockfd = open_connection_to( argv[1], atoi(argv[2]))) == 0) {
      report_error( "couldn't open connection to destination" );
      return -1;
    }

    recv_message( sockfd );
    read_one_line( sockfd );

    disconnect_server( sockfd );
    return 0;
}
void read_constrain(input_file *input, FILE *fp){   //制約条件の読み込み
	int i;
	for(i=1; i<=m; i++){		        /* 制約条件式の数だけ繰り返す */
      printf("第%2d式  ",i);
      read_one_line(fp,input->line);    /* ファイルからデータを1行読み込む */
/*--------------------------------------
	制約条件式の解析
--------------------------------------*/
      /* 1:左辺値の読み込み */
      input->string=strtok(input->line," ");
      /* 最初は先頭からトークンを読み込む */
      while((input->string[0]!='<')&&(input->string[0]!='=')&&(input->string[0]!='>')&&(input->string[0]!='\n')){
			/* トークンが不等号か改行コードを含まない間繰り返す */
			input->x_sub = read_one_token(input->string, &input->coef);
			/* 読み込んだトークンを構文解析する */
			a[i][input->x_sub] = input->coef;
			/* 制約条件式の係数aに代入する */
			printf("%6.2fX%d ",input->coef,input->x_sub);
			input->string=strtok(NULL," ");	/* 新しいトークンを読み込む */
		}
      /* 2:不等号の読み込み */
      sign[i]=input->string[0];
      printf("%c= ",sign[i]);
      if(input->string[0]=='\n'){
			/* エラーチェック(不等号でなく改行コードだったとき) */
			printf("データに正しい不等号がありません\n");
			assert(input->string[0]!='\n');
		}
      /* 3:右辺の定数の読み込み */
      input->string=strtok(NULL," ");	        /* 定数のトークンを読み込む */
      b[i]=atof(input->string);	       	        /* 右辺の定数bに代入する */
      printf("%6.2f\n",b[i]);
   	/* if(b[i]==(0.0)){		   エラーチェック(0.0)=EROOR
			printf("EROOR!!定数データが不正です\n");
			assert(b[i]!=0.0);	   プログラムを終了する
      } */
   }
}
Example #13
0
struct p_tree *read_tree(char *filename) 
{
	FILE *tree_file = fopen(filename, "r");
	struct p_tree *root = NULL;
	char *str;
	char *one_line;

	str = (char *) ckalloc(MAX_LEN*sizeof(char));
	one_line = (char *) ckalloc(MAX_NEWICK*sizeof(char));
	str[0] = '\0';
	one_line[0] = '\0';
	while( fgets(str, MAX_LEN, tree_file) ) {
		strcat(one_line, str);
	}
	
	leave_only_taxons(one_line);
// 	count_node = 0;
	root = read_one_line(one_line); // in here, it is assumed that the last taxon is an out-group and the branch of the last taxon and others is a root. So re-rooting might be necessary.

	free(str);
	free(one_line);
	return root;
}
Example #14
0
/* Reads a single line after joining lines with trailing \ characters.  Fails if
 * line buffer overflows or fgets fails, sets *eof on end of file. */
static bool read_line(
    FILE *input, char *line_buffer, size_t line_length,
    int *line_number, bool *eof)
{
    bool ok = true;
    bool want_line = true;
    while (ok  &&  !*eof  &&  want_line)
    {
        size_t length_read = 0;
        *line_number += 1;
        ok = read_one_line(
            input, line_buffer, line_length, *line_number, &length_read, eof);
        want_line = ok  &&  !*eof  &&
            length_read > 0  &&  line_buffer[length_read - 1] == '\\';
        if (want_line)
        {
            line_buffer += length_read - 1;
            line_length -= length_read - 1;
            ok = TEST_OK_(line_length > 2,
                "Run out of read buffer on line %d", *line_number);
        }
    }
    return ok;
}
Example #15
0
int main(int argc, char **argv)
{
	FILE *cv_f;
	int *bid;
 	struct cv_list *cv; // the conversion events detected by Chih-Hao's program
	int num_cv;
 	struct cv_list *init_cv; // the conversion events detected by Chih-Hao's program
	int num_init_cv;
	char buf[1000];
	char tree_line[1000];
	char name_line[1000];
	int i = 0, j = 0, k = 0;
//	char species1[100], species2[100];
//	int res = 0;
	char name1[100], name2[100];
//	struct I src1, dst1, src2, dst2;
	int event_number = 0;
	struct p_tree *sp_tree = NULL;
	struct p_tree *lca = NULL;
	struct p_tree *t = NULL;
	int nid = 0;
	int root_id = 0;
	struct sp_list *sp_id; 	
	int num_sp = 0, temp_num = 0;
	int *primary_sp;
	int num_primary = 0;
	bool is_in = false;
	int temp_bid[50];
	int num_bid = 0;

	debug_mode = FALSE;
	strcpy(name_line, "");
	strcpy(tree_line, "");
	if( argc == 5 ) debug_mode = TRUE;
	else if( argc == 4 ) {
		event_number = atoi(argv[3]);
	}
	else if( argc != 3 ) {
		fatal("args: all.gc all.remove_redundancy.gc [event_id]\n");
	}
	else {
		event_number = -1;
	}

	bid = (int *) ckalloc(sizeof(int));
	cv_f = fopen(argv[1], "r");
	if( cv_f == NULL ) {
		fatalf("conv-file open error %s!\n", argv[1]);
	}
	else {
		num_init_cv = 0;
		while( fgets(buf, 1000, cv_f) ) num_init_cv++;

		init_cv = (struct cv_list *) ckalloc(num_init_cv * sizeof(struct cv_list));
		fseek(cv_f, 0, SEEK_SET);
		i = 0;
		while( fgets(buf, 1000, cv_f) ) {
			if( (buf[0] == '(' ) || (buf[0] == '#') )  {
				if( buf[0] == '(' ) {
					strcpy(tree_line, buf);
					leave_only_taxons(tree_line);
					num_sp = 0;
					j = 0;
					while( tree_line[j] != '\0') {
						if( tree_line[j] == ',' ) num_sp++;
						j++;
					}
					num_sp++;
					if( num_sp > 0 ) {
						sp_id = (struct sp_list *) ckalloc(num_sp * sizeof(struct sp_list));
						for( j = 0; j < num_sp; j++ ) {
							strcpy(sp_id[j].name, "");
							sp_id[j].id = -1;
						}
						temp_num = assign_sp_code(tree_line, sp_id, num_sp);
						if( temp_num != num_sp ) {
							fatalf("Number of species mismatched in %d and %d", num_sp, temp_num);
						}
						sp_tree = read_one_line(tree_line);
						root_id = sp_tree->nid;
						assign_sp_id(sp_tree, sp_id, 0, num_sp);
						if( debug_mode == TRUE ) {
							print_tree(sp_tree, TREE_PRINT);
							printf("\n");
						}
					}
				}
				else if( buf[0] == '#' ) {
					strcpy(name_line, buf);
				}
			}
			else {
				if( event_number == -1 ) {
					sscanf(buf, "%d %s %d %d %*s %d %d %c %d %f %d %s %d %d %d %d %d %s %d %d %c %s %d %d %c %d %d %s %s %d", &init_cv[i].oid, init_cv[i].name1, &init_cv[i].s1, &init_cv[i].s2, &init_cv[i].t1, &init_cv[i].t2, &init_cv[i].ori, &init_cv[i].len1, &init_cv[i].pid, &init_cv[i].len2, init_cv[i].pval, &init_cv[i].a1, &init_cv[i].a2, &init_cv[i].b1, &init_cv[i].b2, &init_cv[i].dir, init_cv[i].name2, &init_cv[i].c1, &init_cv[i].c2, &init_cv[i].ori1, init_cv[i].name3, &init_cv[i].d1, &init_cv[i].d2, &init_cv[i].ori2, &init_cv[i].fid, &init_cv[i].bid1, init_cv[i].ortho1, init_cv[i].ortho2, &init_cv[i].status);
					init_cv[i].num_bid2 = 0; // marking unvisited
					i++;
				}
				else {
					sscanf(buf, "%d %s %d %d %*s %d %d %c %d %f %d %s %d %d %d %d %d %s %d %d %c %s %d %d %c %d %*s %s %s %d", &init_cv[i].oid, init_cv[i].name1, &init_cv[i].s1, &init_cv[i].s2, &init_cv[i].t1, &init_cv[i].t2, &init_cv[i].ori, &init_cv[i].len1, &init_cv[i].pid, &init_cv[i].len2, init_cv[i].pval, &init_cv[i].a1, &init_cv[i].a2, &init_cv[i].b1, &init_cv[i].b2, &init_cv[i].dir, init_cv[i].name2, &init_cv[i].c1, &init_cv[i].c2, &init_cv[i].ori1, init_cv[i].name3, &init_cv[i].d1, &init_cv[i].d2, &init_cv[i].ori2, &init_cv[i].fid, init_cv[i].ortho1, init_cv[i].ortho2, &init_cv[i].status);
					if( init_cv[i].fid == event_number ) {
						printf("%s", buf);
					}
					i++;
				}
			}
		}
	}
	fclose(cv_f);
	num_init_cv = i;

	primary_sp = ckalloc(sizeof(int) * num_sp);

	if( event_number == -1 ) {
		for( i = 0; i < num_init_cv; i++ ) {
			if( init_cv[i].num_bid2 == 0 ) { // init_cv[i] unvisited
				strcpy( name1, init_cv[i].name1 );
				num_primary = 0;
				primary_sp[num_primary] = get_id_in_list(name1, sp_id, num_sp);
				num_primary++;
			}

			for( j = (i+1); j < num_init_cv; j++ ) {
				if( (init_cv[j].num_bid2 == 0) && (init_cv[j].fid == init_cv[i].fid) ) {
					strcpy(name2, init_cv[j].name1);
					temp_num = get_id_in_list(name2, sp_id, num_sp);
					is_in = false;
					for(k = 0;  k < num_primary; k++) {
						if( temp_num == primary_sp[k] ) is_in = true;
					}
					if( is_in == false ) {
						if( num_primary >= num_sp ) {
							fatalf("overflow in primary_sp[], %s\n", name2);
						}
						else {
							primary_sp[num_primary] = temp_num;
							num_primary++;
						}
					}
				}
			}
			lca = find_lca(primary_sp, num_primary, sp_tree, &nid);

			if( (lca->nid) > init_cv[i].bid1 ) {
				num_bid = 1;
				temp_bid[k] = -1;
			}
			else if( lca != NULL ) 
			{
				t = lca;
				k = 0;
				temp_num = t->nid;
				temp_bid[k] = t->nid;
				k++;
				while( (k < 50) && (t != sp_tree) && ((t->nid) < root_id) && ((t->nid) <init_cv[i].bid1) ) 
				{
					t = t->parent;
					temp_bid[k] = t->nid;
					k++;
				}
				num_bid = k;
			}
			else {
				fatal("lca not found\n");	
			}

			if( k >= 50 ) {
				printf("Warning: exceed limit 50 branch numbers\n");
			}

			for( j = i; j < num_init_cv; j++ ) {
				if( (init_cv[j].num_bid2 == 0) && (init_cv[j].fid == init_cv[i].fid) ) 
				{
					init_cv[j].num_bid2 = num_bid;
					for( k = 0; k < num_bid; k++ ) {
						init_cv[j].bid2[k] = temp_bid[k];
					}
				}
			}
		}
	}

	cv_f = fopen(argv[2], "r");
	if( cv_f == NULL ) {
		fatalf("conv-file open error %s!\n", argv[2]);
	}
	else {
		num_cv = 0;
		while( fgets(buf, 1000, cv_f) ) num_cv++;

		cv = (struct cv_list *) ckalloc(num_cv * sizeof(struct cv_list));
		fseek(cv_f, 0, SEEK_SET);
		i = 0;
/*
		while( fgets(buf, 1000, cv_f) ) {
			if( (buf[0] == '(' ) || (buf[0] == '#') )  {}
			else {
				sscanf(buf, "%d %s %d %d %*s %d %d %c %*s %*s %*s %*s %d %d %d %d %d %s", &cv[i].fid, species1, &cv[i].s1, &cv[i].s2, &cv[i].t1, &cv[i].t2, &cv[i].ori, &cv[i].a1, &cv[i].a2, &cv[i].b1, &cv[i].b2, &cv[i].dir, species2);
				res = find_status(cv[i], init_cv, num_init_cv, species1, species2);

				printf("%d\t%s\t%d\t%d\t%s\t%d\t%d\t%c\t%d\t%.2f\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%s\t%d\t%d\t%c\t%s\t%d\t%d\t%c\t%d\t%d\t%s\t%s\t%d\n", init_cv[res].oid, init_cv[res].name1, init_cv[res].s1, init_cv[res].s2, init_cv[res].name1, init_cv[res].t1, init_cv[res].t2, init_cv[res].ori, init_cv[res].len1, init_cv[res].pid, init_cv[res].len2, init_cv[res].pval, init_cv[res].a1, init_cv[res].a2, init_cv[res].b1, init_cv[res].b2, init_cv[res].dir, init_cv[res].name2, init_cv[res].c1, init_cv[res].c2, init_cv[res].ori1, init_cv[res].name3, init_cv[res].d1, init_cv[res].d2, init_cv[res].ori2, init_cv[res].fid, init_cv[res].bid1, init_cv[res].ortho1, init_cv[res].ortho2, init_cv[res].status);
				i++;
			}
		}
*/
	}
	fclose(cv_f);

	if( event_number == -1 ) {
		printf("%s\n", tree_line);
		printf("%s", name_line);
		for( i = 0; i < num_init_cv; i++ ) {
			if( (init_cv[i].num_bid2 == 0) || ((init_cv[i].num_bid2 == 1) && (init_cv[i].bid2[0] == -1)) )  
			{
				printf("%d\t%s\t%d\t%d\t%s\t%d\t%d\t%c\t%d\t%.2f\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%s\t%d\t%d\t%c\t%s\t%d\t%d\t%c\t%d\t%d,?\t%s\t%s\t%d\n", init_cv[i].oid, init_cv[i].name1, init_cv[i].s1, init_cv[i].s2, init_cv[i].name1, init_cv[i].t1, init_cv[i].t2, init_cv[i].ori, init_cv[i].len1, init_cv[i].pid, init_cv[i].len2, init_cv[i].pval, init_cv[i].a1, init_cv[i].a2, init_cv[i].b1, init_cv[i].b2, init_cv[i].dir, init_cv[i].name2, init_cv[i].c1, init_cv[i].c2, init_cv[i].ori1, init_cv[i].name3, init_cv[i].d1, init_cv[i].d2, init_cv[i].ori2, init_cv[i].fid, init_cv[i].bid1, init_cv[i].ortho1, init_cv[i].ortho2, init_cv[i].status);
			}
			else {
				printf("%d\t%s\t%d\t%d\t%s\t%d\t%d\t%c\t%d\t%.2f\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%s\t%d\t%d\t%c\t%s\t%d\t%d\t%c\t%d\t", init_cv[i].oid, init_cv[i].name1, init_cv[i].s1, init_cv[i].s2, init_cv[i].name1, init_cv[i].t1, init_cv[i].t2, init_cv[i].ori, init_cv[i].len1, init_cv[i].pid, init_cv[i].len2, init_cv[i].pval, init_cv[i].a1, init_cv[i].a2, init_cv[i].b1, init_cv[i].b2, init_cv[i].dir, init_cv[i].name2, init_cv[i].c1, init_cv[i].c2, init_cv[i].ori1, init_cv[i].name3, init_cv[i].d1, init_cv[i].d2, init_cv[i].ori2, init_cv[i].fid);
				printf("%d", init_cv[i].bid2[0]);
				for( k = 1; k < init_cv[i].num_bid2; k++ ) {
					printf(",%d", init_cv[i].bid2[k]);
				}
				printf("\t%s\t%s\t%d\n", init_cv[i].ortho1, init_cv[i].ortho2, init_cv[i].status);
			}
		}
	}

	free_p_tree(sp_tree);
	free(primary_sp);
	free(sp_id);
	free(bid);
	free(init_cv);
	return EXIT_SUCCESS;
}
Example #16
0
InitFile *load_init_file(wchar_t *filename)
{
    HANDLE infile;
    InitFile *inif;
    InitSection *inis;
    InitEntry *inie;
    FileContext fc;
    char *line;
    char **lines;
    int size_lines;
    int num_lines;

    int i;

    if ( (infile = CreateFileW(filename, 
			       GENERIC_READ,
			       FILE_SHARE_READ,
			       NULL,
			       OPEN_EXISTING,
			       FILE_ATTRIBUTE_NORMAL,
			       NULL)) == INVALID_HANDLE_VALUE) {
	return NULL;
    }
    
    size_lines = 10;
    num_lines = 0;
    lines = ALLOC(size_lines * sizeof(char *));

    fc.fd = infile;
    fc.eof = 0;
    fc.num = 0;
    fc.pos = 0;
    while ((line = read_one_line(&fc)) != NULL) {
	if (num_lines == size_lines) {
	    size_lines += 10;
	    lines = REALLOC(lines,size_lines * sizeof(char *));
	}
	lines[num_lines] = line;
	++num_lines;
    }
    CloseHandle(infile);
    /* Now check the lines before doing anything else, so that 
       we don't need any error handling while creating the data
       structures */
    /* 
       The file should contain:
       [section]
       Key=Value
       ...
       [section]
       ...
    */
    i = 0;
    while (i < num_lines && is_section_header(lines[i])) {
	++i;
	while (i < num_lines && is_key_value(lines[i])) {
	    ++i;
	}
    }
    if (i < num_lines) {
	for (i = 0; i < num_lines; ++i) {
	    FREE(lines[i]);
	}
	FREE(lines);
	return NULL;
    }
    
    /* So, now we know it's consistent... */
    i = 0;
    inif = ALLOC(sizeof(InitFile));
    inif->num_sections = 0;
    inif->size_sections = 10;
    inif->sections = ALLOC(sizeof(InitSection *) * 10); 
    while (i < num_lines) {
	inis = ALLOC(sizeof(InitSection));
	inis->num_entries = 0;
	inis->size_entries = 10;
	inis->section_name = digout_section_name(lines[i]);
	inis->entries = ALLOC(sizeof(InitEntry *) * 10);
	++i;
	while (i < num_lines && is_key_value(lines[i])) {
	    inie = ALLOC(sizeof(InitEntry));
	    digout_key_value(lines[i], &(inie->key), &(inie->value));
	    if (inis->num_entries == inis->size_entries) {
		inis->size_entries += 10;
		inis->entries = 
		    REALLOC(inis->entries,
			    sizeof(InitEntry *) * inis->size_entries);
	    }
	    inis->entries[inis->num_entries] = inie;
	    ++(inis->num_entries);
	    ++i;
	}
	if (inif->num_sections == inif->size_sections) {
	    inif->size_sections += 10;
	    inif->sections = 
		REALLOC(inif->sections,
			sizeof(InitSection *) * inif->size_sections);
	}
	inif->sections[inif->num_sections] = inis;
	++(inif->num_sections);
    }
    FREE(lines); /* Only the array of strings, not the actual strings, they
		    are kept in the data structures. */
    return inif;
}
Example #17
0
static int
edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
{
    FILE *g = NULL;
    char *fg, *bg, *attrs;
    char last_fg[32] = "", last_bg[32] = "", last_attrs[64] = "";
    char whole_right[512];
    char whole_left[512];
    char *l = NULL;
    int save_line = 0, line = 0;
    context_rule_t *c = NULL;
    gboolean no_words = TRUE;
    int result = 0;

    args[0] = NULL;
    edit->is_case_insensitive = FALSE;

    strcpy (whole_left, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
    strcpy (whole_right, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");

    edit->rules = g_ptr_array_new ();

    if (edit->defines == NULL)
        edit->defines = g_tree_new ((GCompareFunc) strcmp);

    while (TRUE)
    {
        char **a;
        size_t len;
        int argc;

        line++;
        l = NULL;

        len = read_one_line (&l, f);
        if (len != 0)
            xx_lowerize_line (edit, l, len);
        else
        {
            if (g == NULL)
                break;

            fclose (f);
            f = g;
            g = NULL;
            line = save_line + 1;
            MC_PTR_FREE (error_file_name);
            MC_PTR_FREE (l);
            len = read_one_line (&l, f);
            if (len == 0)
                break;
            xx_lowerize_line (edit, l, len);
        }

        argc = get_args (l, args, args_size);
        a = args + 1;
        if (args[0] == NULL)
        {
            /* do nothing */
        }
        else if (strcmp (args[0], "include") == 0)
        {
            if (g != NULL || argc != 2)
            {
                result = line;
                break;
            }
            g = f;
            f = open_include_file (args[1]);
            if (f == NULL)
            {
                MC_PTR_FREE (error_file_name);
                result = line;
                break;
            }
            save_line = line;
            line = 0;
        }
        else if (strcmp (args[0], "caseinsensitive") == 0)
        {
            edit->is_case_insensitive = TRUE;
        }
        else if (strcmp (args[0], "wholechars") == 0)
        {
            check_a;
            if (strcmp (*a, "left") == 0)
            {
                a++;
                g_strlcpy (whole_left, *a, sizeof (whole_left));
            }
            else if (strcmp (*a, "right") == 0)
            {
                a++;
                g_strlcpy (whole_right, *a, sizeof (whole_right));
            }
            else
            {
                g_strlcpy (whole_left, *a, sizeof (whole_left));
                g_strlcpy (whole_right, *a, sizeof (whole_right));
            }
            a++;
            check_not_a;
        }
        else if (strcmp (args[0], "context") == 0)
        {
            syntax_keyword_t *k;

            check_a;
            if (edit->rules->len == 0)
            {
                /* first context is the default */
                if (strcmp (*a, "default") != 0)
                    break_a;

                a++;
                c = g_new0 (context_rule_t, 1);
                g_ptr_array_add (edit->rules, c);
                c->left = g_strdup (" ");
                c->right = g_strdup (" ");
            }
            else
            {
                /* Start new context.  */
                c = g_new0 (context_rule_t, 1);
                g_ptr_array_add (edit->rules, c);
                if (strcmp (*a, "exclusive") == 0)
                {
                    a++;
                    c->between_delimiters = TRUE;
                }
                check_a;
                if (strcmp (*a, "whole") == 0)
                {
                    a++;
                    c->whole_word_chars_left = g_strdup (whole_left);
                    c->whole_word_chars_right = g_strdup (whole_right);
                }
                else if (strcmp (*a, "wholeleft") == 0)
                {
                    a++;
                    c->whole_word_chars_left = g_strdup (whole_left);
                }
                else if (strcmp (*a, "wholeright") == 0)
                {
                    a++;
                    c->whole_word_chars_right = g_strdup (whole_right);
                }
                check_a;
                if (strcmp (*a, "linestart") == 0)
                {
                    a++;
                    c->line_start_left = TRUE;
                }
                check_a;
                c->left = g_strdup (*a++);
                check_a;
                if (strcmp (*a, "linestart") == 0)
                {
                    a++;
                    c->line_start_right = TRUE;
                }
                check_a;
                c->right = g_strdup (*a++);
                c->first_left = *c->left;
                c->first_right = *c->right;
            }
            c->keyword = g_ptr_array_new ();
            k = g_new0 (syntax_keyword_t, 1);
            g_ptr_array_add (c->keyword, k);
            no_words = FALSE;
            subst_defines (edit->defines, a, &args[ARGS_LEN]);
            fg = *a;
            if (*a != NULL)
                a++;
            bg = *a;
            if (*a != NULL)
                a++;
            attrs = *a;
            if (*a != NULL)
                a++;
            g_strlcpy (last_fg, fg != NULL ? fg : "", sizeof (last_fg));
            g_strlcpy (last_bg, bg != NULL ? bg : "", sizeof (last_bg));
            g_strlcpy (last_attrs, attrs != NULL ? attrs : "", sizeof (last_attrs));
            k->color = this_try_alloc_color_pair (fg, bg, attrs);
            k->keyword = g_strdup (" ");
            check_not_a;
        }
        else if (strcmp (args[0], "spellcheck") == 0)
        {
            if (c == NULL)
            {
                result = line;
                break;
            }
            c->spelling = TRUE;
        }
        else if (strcmp (args[0], "keyword") == 0)
        {
            context_rule_t *last_rule;
            syntax_keyword_t *k;

            if (no_words)
                break_a;
            check_a;
            last_rule = CONTEXT_RULE (g_ptr_array_index (edit->rules, edit->rules->len - 1));
            k = g_new0 (syntax_keyword_t, 1);
            g_ptr_array_add (last_rule->keyword, k);
            if (strcmp (*a, "whole") == 0)
            {
                a++;
                k->whole_word_chars_left = g_strdup (whole_left);
                k->whole_word_chars_right = g_strdup (whole_right);
            }
            else if (strcmp (*a, "wholeleft") == 0)
            {
                a++;
                k->whole_word_chars_left = g_strdup (whole_left);
            }
            else if (strcmp (*a, "wholeright") == 0)
            {
                a++;
                k->whole_word_chars_right = g_strdup (whole_right);
            }
            check_a;
            if (strcmp (*a, "linestart") == 0)
            {
                a++;
                k->line_start = TRUE;
            }
            check_a;
            if (strcmp (*a, "whole") == 0)
                break_a;

            k->keyword = g_strdup (*a++);
            subst_defines (edit->defines, a, &args[ARGS_LEN]);
            fg = *a;
            if (*a != NULL)
                a++;
            bg = *a;
            if (*a != NULL)
                a++;
            attrs = *a;
            if (*a != NULL)
                a++;
            if (fg == NULL)
                fg = last_fg;
            if (bg == NULL)
                bg = last_bg;
            if (attrs == NULL)
                attrs = last_attrs;
            k->color = this_try_alloc_color_pair (fg, bg, attrs);
            check_not_a;
        }
        else if (*(args[0]) == '#')
        {
            /* do nothing for comment */
        }
        else if (strcmp (args[0], "file") == 0)
        {
            break;
        }
        else if (strcmp (args[0], "define") == 0)
        {
            char *key = *a++;
            char **argv;

            if (argc < 3)
                break_a;
            argv = g_tree_lookup (edit->defines, key);
            if (argv != NULL)
                mc_defines_destroy (NULL, argv, NULL);
            else
                key = g_strdup (key);

            argv = g_new (char *, argc - 1);
            g_tree_insert (edit->defines, key, argv);
            while (*a != NULL)
                *argv++ = g_strdup (*a++);
            *argv = NULL;
        }
        else
        {
Example #18
0
Package*
parse_package_file (const char *path, gboolean ignore_requires,
		    gboolean ignore_private_libs,
		    gboolean ignore_requires_private)
{
  FILE *f;
  Package *pkg;
  GString *str;
  gboolean one_line = FALSE;
  
  f = fopen (path, "r");

  if (f == NULL)
    {
      verbose_error ("Failed to open '%s': %s\n",
                     path, strerror (errno));
      
      return NULL;
    }

  debug_spew ("Parsing package file '%s'\n", path);
  
  pkg = g_new0 (Package, 1);

  if (path)
    {
      pkg->pcfiledir = g_dirname (path);
    }
  else
    {
      debug_spew ("No pcfiledir determined for package\n");
      pkg->pcfiledir = g_strdup ("???????");
    }
  
  str = g_string_new ("");

  while (read_one_line (f, str))
    {
      one_line = TRUE;
      
      parse_line (pkg, str->str, path, ignore_requires, ignore_private_libs,
		  ignore_requires_private);

      g_string_truncate (str, 0);
    }

  if (!one_line)
    verbose_error ("Package file '%s' appears to be empty\n",
                   path);
  g_string_free (str, TRUE);
  fclose(f);

  /* make ->requires_private include a copy of the public requires too */
  pkg->requires_private = g_slist_concat(g_slist_copy (pkg->requires),
					 pkg->requires_private);
  
  pkg->requires = g_slist_reverse (pkg->requires);
  
  pkg->requires_private = g_slist_reverse (pkg->requires_private);

  pkg->I_cflags = g_slist_reverse (pkg->I_cflags);
  pkg->other_cflags = g_slist_reverse (pkg->other_cflags);

  pkg->l_libs = g_slist_reverse (pkg->l_libs);
  pkg->L_libs = g_slist_reverse (pkg->L_libs);
  pkg->other_libs = g_slist_reverse (pkg->other_libs);
  
  return pkg;
}
Example #19
0
File: syntax.c Project: artzub/mc
static int
edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
{
    FILE *g = NULL;
    char *fg, *bg, *attrs;
    char last_fg[32] = "", last_bg[32] = "", last_attrs[64] = "";
    char whole_right[512];
    char whole_left[512];
    char *l = 0;
    int save_line = 0, line = 0;
    struct context_rule **r, *c = NULL;
    int num_words = -1, num_contexts = -1;
    int result = 0;
    int argc;
    int i, j;
    int alloc_contexts = MAX_CONTEXTS,
        alloc_words_per_context = MAX_WORDS_PER_CONTEXT,
        max_alloc_words_per_context = MAX_WORDS_PER_CONTEXT;

    args[0] = NULL;
    edit->is_case_insensitive = FALSE;

    strcpy (whole_left, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
    strcpy (whole_right, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");

    r = edit->rules = g_malloc0 (alloc_contexts * sizeof (struct context_rule *));

    if (!edit->defines)
        edit->defines = g_tree_new ((GCompareFunc) strcmp);

    for (;;)
    {
        char **a;
        size_t len;

        line++;
        l = 0;

        len = read_one_line (&l, f);
        if (len == 0)
        {
            if (g)
            {
                fclose (f);
                f = g;
                g = 0;
                line = save_line + 1;
                MC_PTR_FREE (error_file_name);
                MC_PTR_FREE (l);
                len = read_one_line (&l, f);
                if (len == 0)
                    break;
                else
                    xx_lowerize_line (edit, l, len);
            }
            else
            {
                break;
            }
        }
        else
        {
            xx_lowerize_line (edit, l, len);
        }
        argc = get_args (l, args, args_size);
        a = args + 1;
        if (!args[0])
        {
            /* do nothing */
        }
        else if (!strcmp (args[0], "include"))
        {
            if (g || argc != 2)
            {
                result = line;
                break;
            }
            g = f;
            f = open_include_file (args[1]);
            if (!f)
            {
                MC_PTR_FREE (error_file_name);
                result = line;
                break;
            }
            save_line = line;
            line = 0;
        }
        else if (!strcmp (args[0], "caseinsensitive"))
        {
            edit->is_case_insensitive = TRUE;
        }
        else if (!strcmp (args[0], "wholechars"))
        {
            check_a;
            if (!strcmp (*a, "left"))
            {
                a++;
                g_strlcpy (whole_left, *a, sizeof (whole_left));
            }
            else if (!strcmp (*a, "right"))
            {
                a++;
                g_strlcpy (whole_right, *a, sizeof (whole_right));
            }
            else
            {
                g_strlcpy (whole_left, *a, sizeof (whole_left));
                g_strlcpy (whole_right, *a, sizeof (whole_right));
            }
            a++;
            check_not_a;
        }
        else if (!strcmp (args[0], "context"))
        {
            check_a;
            if (num_contexts == -1)
            {
                if (strcmp (*a, "default"))
                {               /* first context is the default */
                    break_a;
                }
                a++;
                c = r[0] = g_malloc0 (sizeof (struct context_rule));
                c->left = g_strdup (" ");
                c->right = g_strdup (" ");
                num_contexts = 0;
            }
            else
            {
                /* Terminate previous context.  */
                r[num_contexts - 1]->keyword[num_words] = NULL;
                c = r[num_contexts] = g_malloc0 (sizeof (struct context_rule));
                if (!strcmp (*a, "exclusive"))
                {
                    a++;
                    c->between_delimiters = 1;
                }
                check_a;
                if (!strcmp (*a, "whole"))
                {
                    a++;
                    c->whole_word_chars_left = g_strdup (whole_left);
                    c->whole_word_chars_right = g_strdup (whole_right);
                }
                else if (!strcmp (*a, "wholeleft"))
                {
                    a++;
                    c->whole_word_chars_left = g_strdup (whole_left);
                }
                else if (!strcmp (*a, "wholeright"))
                {
                    a++;
                    c->whole_word_chars_right = g_strdup (whole_right);
                }
                check_a;
                if (!strcmp (*a, "linestart"))
                {
                    a++;
                    c->line_start_left = 1;
                }
                check_a;
                c->left = g_strdup (*a++);
                check_a;
                if (!strcmp (*a, "linestart"))
                {
                    a++;
                    c->line_start_right = 1;
                }
                check_a;
                c->right = g_strdup (*a++);
                c->first_left = *c->left;
                c->first_right = *c->right;
            }
            c->keyword = g_malloc (alloc_words_per_context * sizeof (struct key_word *));
            num_words = 1;
            c->keyword[0] = g_malloc0 (sizeof (struct key_word));
            subst_defines (edit->defines, a, &args[1024]);
            fg = *a;
            if (*a)
                a++;
            bg = *a;
            if (*a)
                a++;
            attrs = *a;
            if (*a)
                a++;
            g_strlcpy (last_fg, fg ? fg : "", sizeof (last_fg));
            g_strlcpy (last_bg, bg ? bg : "", sizeof (last_bg));
            g_strlcpy (last_attrs, attrs ? attrs : "", sizeof (last_attrs));
            c->keyword[0]->color = this_try_alloc_color_pair (fg, bg, attrs);
            c->keyword[0]->keyword = g_strdup (" ");
            check_not_a;

            alloc_words_per_context = MAX_WORDS_PER_CONTEXT;
            if (++num_contexts >= alloc_contexts)
            {
                struct context_rule **tmp;

                alloc_contexts += 128;
                tmp = g_realloc (r, alloc_contexts * sizeof (struct context_rule *));
                r = tmp;
            }
        }
        else if (!strcmp (args[0], "spellcheck"))
        {
            if (!c)
            {
                result = line;
                break;
            }
            c->spelling = 1;
        }
        else if (!strcmp (args[0], "keyword"))
        {
            struct key_word *k;

            if (num_words == -1)
                break_a;
            check_a;
            k = r[num_contexts - 1]->keyword[num_words] = g_malloc0 (sizeof (struct key_word));
            if (!strcmp (*a, "whole"))
            {
                a++;
                k->whole_word_chars_left = g_strdup (whole_left);
                k->whole_word_chars_right = g_strdup (whole_right);
            }
            else if (!strcmp (*a, "wholeleft"))
            {
                a++;
                k->whole_word_chars_left = g_strdup (whole_left);
            }
            else if (!strcmp (*a, "wholeright"))
            {
                a++;
                k->whole_word_chars_right = g_strdup (whole_right);
            }
            check_a;
            if (!strcmp (*a, "linestart"))
            {
                a++;
                k->line_start = 1;
            }
            check_a;
            if (!strcmp (*a, "whole"))
            {
                break_a;
            }
            k->keyword = g_strdup (*a++);
            k->first = *k->keyword;
            subst_defines (edit->defines, a, &args[1024]);
            fg = *a;
            if (*a)
                a++;
            bg = *a;
            if (*a)
                a++;
            attrs = *a;
            if (*a)
                a++;
            if (!fg)
                fg = last_fg;
            if (!bg)
                bg = last_bg;
            if (!attrs)
                attrs = last_attrs;
            k->color = this_try_alloc_color_pair (fg, bg, attrs);
            check_not_a;

            if (++num_words >= alloc_words_per_context)
            {
                struct key_word **tmp;

                alloc_words_per_context += 1024;

                if (alloc_words_per_context > max_alloc_words_per_context)
                    max_alloc_words_per_context = alloc_words_per_context;

                tmp = g_realloc (c->keyword, alloc_words_per_context * sizeof (struct key_word *));
                c->keyword = tmp;
            }
        }
        else if (*(args[0]) == '#')
        {
            /* do nothing for comment */
        }
        else if (!strcmp (args[0], "file"))
        {
            break;
        }
        else if (!strcmp (args[0], "define"))
        {
            char *key = *a++;
            char **argv;

            if (argc < 3)
                break_a;
            argv = g_tree_lookup (edit->defines, key);
            if (argv != NULL)
                mc_defines_destroy (NULL, argv, NULL);
            else
                key = g_strdup (key);

            argv = g_new (char *, argc - 1);
            g_tree_insert (edit->defines, key, argv);
            while (*a != NULL)
            {
                *argv++ = g_strdup (*a++);
            }
            *argv = NULL;
        }
        else
        {                       /* anything else is an error */
Example #20
0
void load_settings(struct settings *settings) {
	struct uci_context *ctx = uci_alloc_context();
	if (!ctx) {
		fprintf(stderr, "autoupdater: error: failed to allocate UCI context\n");
		abort();
	}

	ctx->flags &= ~UCI_FLAG_STRICT;

	struct uci_package *p;
	struct uci_section *s;

	if (uci_load(ctx, "autoupdater", &p) != UCI_OK) {
		fputs("autoupdater: error: unable to load UCI package\n", stderr);
		exit(1);
	}

	s = uci_lookup_section(ctx, p, "settings");
	if (!s || strcmp(s->type, "autoupdater")) {
		fputs("autoupdater: error: unable to load UCI settings\n", stderr);
		exit(1);
	}

	const char *enabled = uci_lookup_option_string(ctx, s, "enabled");
	if ((!enabled || strcmp(enabled, "1")) && !settings->force) {
		fputs("autoupdater is disabled\n", stderr);
		exit(0);
	}

	const char *version_file = uci_lookup_option_string(ctx, s, "version_file");
	if (version_file)
		settings->old_version = read_one_line(version_file);

	if (!settings->branch)
		settings->branch = uci_lookup_option_string(ctx, s, "branch");

	if (!settings->branch) {
		fputs("autoupdater: error: no branch given in settings or command line\n", stderr);
		exit(1);
	}

	struct uci_section *branch = uci_lookup_section(ctx, p, settings->branch);
	if (!branch || strcmp(branch->type, "branch")) {
		fprintf(stderr, "autoupdater: error: unable to load branch configuration for branch '%s'\n", settings->branch);
		exit(1);
	}

	settings->good_signatures = load_positive_number(ctx, branch, "good_signatures");
	if (settings->n_mirrors == 0)
		settings->mirrors = load_string_list(ctx, branch, "mirror", &settings->n_mirrors);

	const char **pubkeys_str = load_string_list(ctx, branch, "pubkey", &settings->n_pubkeys);
	settings->pubkeys = safe_malloc(settings->n_pubkeys * sizeof(ecc_25519_work_t));
	size_t ignored_keys = 0;
	for (size_t i = 0; i < settings->n_pubkeys; i++) {
		ecc_int256_t pubkey_packed;
		if (!pubkeys_str[i])
			goto pubkey_fail;
		if (!parsehex(pubkey_packed.p, pubkeys_str[i], 32))
			goto pubkey_fail;
		if (!ecc_25519_load_packed_legacy(&settings->pubkeys[i-ignored_keys], &pubkey_packed))
			goto pubkey_fail;
		if (!ecdsa_is_valid_pubkey(&settings->pubkeys[i-ignored_keys]))
			goto pubkey_fail;
		continue;

pubkey_fail:
		fprintf(stderr, "autoupdater: warning: ignoring invalid public key %s\n", pubkeys_str[i]);
		ignored_keys++;
	}
	settings->n_pubkeys -= ignored_keys;

	/* Don't free UCI context, we still reference values from it */
}