Example #1
0
void PDB::readXYZ(const char* filename){
	printf("Reading %s.\n", filename);
	char buffer[BUF_SIZE];
	FILE* file = safe_fopen(filename, "r");
	safe_fgets(buffer, BUF_SIZE, file);
	const int n = atoi(buffer);
	atoms.resize(n);
	safe_fgets(buffer, BUF_SIZE, file); // Skip mandatory comment line
	int i;
	char* pch;
	for(i = 0; i < n; i++){
		safe_fgets(buffer, BUF_SIZE, file);
		pch = strtok(buffer, " \t\r\n");
		strncpy(atoms[i].name,pch,4);
		pch = strtok(NULL, " \t\r\n");
		atoms[i].x = atof(pch);
		pch = strtok(NULL, " \t\r\n");
		atoms[i].y = atof(pch);
		pch = strtok(NULL, " \t\r\n");
		atoms[i].z = atof(pch);
		atoms[i].id = atoms[i].resid = i+1;
		atoms[i].chain = 'X';
		strcpy(atoms[i].resName, "XXX");
		atoms[i].altLoc = 0;
		atoms[i].occupancy = atoms[i].beta = 0.0;
	}
	printf("Done reading '%s'.\n", filename);
	fclose(file);
}
static void NOINLINE retrieve_file_data(FILE *dfp, int output_fd)
{
    char buf[512];

    if (!(option_mask32 & WGET_OPT_QUIET))
        progress_meter(-1);

    if (G.chunked)
        goto get_clen;

    /* Loops only if chunked */
    while (1) {
        while (1) {
            int n;
            unsigned rdsz;

            rdsz = sizeof(buf);
            if (G.got_clen) {
                if (G.content_len < (off_t)sizeof(buf)) {
                    if ((int)G.content_len <= 0)
                        break;
                    rdsz = (unsigned)G.content_len;
                }
            }
            n = safe_fread(buf, rdsz, dfp);
            if (n <= 0) {
                if (ferror(dfp)) {
                    /* perror will not work: ferror doesn't set errno */
                    bb_error_msg_and_die(bb_msg_read_error);
                }
                break;
            }
            xwrite(output_fd, buf, n);
#if ENABLE_FEATURE_WGET_STATUSBAR
            G.transferred += n;
#endif
            if (G.got_clen)
                G.content_len -= n;
        }

        if (!G.chunked)
            break;

        safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
get_clen:
        safe_fgets(buf, sizeof(buf), dfp);
        G.content_len = STRTOOFF(buf, NULL, 16);
        /* FIXME: error check? */
        if (G.content_len == 0)
            break; /* all done! */
        G.got_clen = 1;
    }

    if (!(option_mask32 & WGET_OPT_QUIET))
        progress_meter(0);
}
Example #3
0
void readCoord(const char* filename, SOP& sop){
	//printf("Reading coordinates from '%s'.\n", filename);
	int is_xyz = 0;
	char buffer[BUF_SIZE+1];
	FILE* file = safe_fopen(filename, "r");
	if(file == NULL){
		DIE("ERROR: Coordinates file %s can not be found.\n", filename);
	}
	if (!strcmp(filename + strlen(filename)-4, ".xyz")) {
		is_xyz = 1;
		safe_fgets(buffer, BUF_SIZE, file);
		safe_fgets(buffer, BUF_SIZE, file);
	}
	int index = 0;
	while(fgets(buffer, BUF_SIZE, file) != NULL){
		if(is_xyz){
			char *pch = strtok(buffer, " \t\r\n");
			pch = strtok(NULL, " \t\r\n");
			sop.aminos[index].x = atof(pch);
			pch = strtok(NULL, " \t\r\n");
			sop.aminos[index].y = atof(pch);
			pch = strtok(NULL, " \t\r\n");
			sop.aminos[index].z = atof(pch);
			index++;
		}
		if(!is_xyz && strncmp(buffer, "ATOM", 4) == 0){
            PDBAtom tmp;
            tmp.parse(buffer);
			sop.aminos[index].x = tmp.x;
			sop.aminos[index].y = tmp.y;
			sop.aminos[index].z = tmp.z;
#ifdef DEBUG
			printf("%d: %f, %f, %f\n", index, sop.aminos[index].x, sop.aminos[index].y, sop.aminos[index].z);
#endif
			index++;
		}
	}
	fclose(file);
	if(index == 0){
		DIE("Can't read pdb file.\n");
	}
	if(index != sop.aminos.size()){
		DIE("Read coordinates for %d beads, yet topology has %zu beads.\n", index, sop.aminos.size());
	}
	//printf("Done reading coordinates.\n");
}
Example #4
0
/* A cheap grep for text files, returns 1 if a substring
 * was found in the text file fn, 0 if it wasn't
 */
static int grep(const char *fn, const char *needle)
{
	FILE *fp;

	if((fp = fopen(fn, "r")) == NULL) {
		return 0;
	}
	while(!feof(fp)) {
		char line[1024];
		if(safe_fgets(line, sizeof(line), fp) == NULL) {
			continue;
		}
		/* TODO: this will not work if the search string
		 * ends up being split across line reads */
		if(strstr(line, needle)) {
			fclose(fp);
			return 1;
		}
	}
	fclose(fp);
	return 0;
}
Example #5
0
static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
{
	FILE *fp = NULL;
	char line[1024];
	alpm_db_t *db = info->origin_data.db;

	/* bitmask logic here:
	 * infolevel: 00001111
	 * inforeq:   00010100
	 * & result:  00000100
	 * == to inforeq? nope, we need to load more info. */
	if((info->infolevel & inforeq) == inforeq) {
		/* already loaded all of this info, do nothing */
		return 0;
	}

	if(info->infolevel & INFRQ_ERROR) {
		/* We've encountered an error loading this package before. Don't attempt
		 * repeated reloads, just give up. */
		return -1;
	}

	_alpm_log(db->handle, ALPM_LOG_FUNCTION,
			"loading package data for %s : level=0x%x\n",
			info->name, inforeq);

	/* clear out 'line', to be certain - and to make valgrind happy */
	memset(line, 0, sizeof(line));

	/* DESC */
	if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
		char *path = _alpm_local_db_pkgpath(db, info, "desc");
		if(!path || (fp = fopen(path, "r")) == NULL) {
			_alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
			free(path);
			goto error;
		}
		free(path);
		while(!feof(fp)) {
			if(safe_fgets(line, sizeof(line), fp) == NULL && !feof(fp)) {
				goto error;
			}
			if(_alpm_strip_newline(line, 0) == 0) {
				/* length of stripped line was zero */
				continue;
			}
			if(strcmp(line, "%NAME%") == 0) {
				READ_NEXT();
				if(strcmp(line, info->name) != 0) {
					_alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: name "
								"mismatch on package %s\n"), db->treename, info->name);
				}
			} else if(strcmp(line, "%VERSION%") == 0) {
				READ_NEXT();
				if(strcmp(line, info->version) != 0) {
					_alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: version "
								"mismatch on package %s\n"), db->treename, info->name);
				}
			} else if(strcmp(line, "%BASE%") == 0) {
				READ_AND_STORE(info->base);
			} else if(strcmp(line, "%DESC%") == 0) {
				READ_AND_STORE(info->desc);
			} else if(strcmp(line, "%GROUPS%") == 0) {
				READ_AND_STORE_ALL(info->groups);
			} else if(strcmp(line, "%URL%") == 0) {
				READ_AND_STORE(info->url);
			} else if(strcmp(line, "%LICENSE%") == 0) {
				READ_AND_STORE_ALL(info->licenses);
			} else if(strcmp(line, "%ARCH%") == 0) {
				READ_AND_STORE(info->arch);
			} else if(strcmp(line, "%BUILDDATE%") == 0) {
				READ_NEXT();
				info->builddate = _alpm_parsedate(line);
			} else if(strcmp(line, "%INSTALLDATE%") == 0) {
				READ_NEXT();
				info->installdate = _alpm_parsedate(line);
			} else if(strcmp(line, "%PACKAGER%") == 0) {
				READ_AND_STORE(info->packager);
			} else if(strcmp(line, "%REASON%") == 0) {
				READ_NEXT();
				info->reason = (alpm_pkgreason_t)atoi(line);
			} else if(strcmp(line, "%VALIDATION%") == 0) {
				alpm_list_t *i, *v = NULL;
				READ_AND_STORE_ALL(v);
				for(i = v; i; i = alpm_list_next(i))
				{
					if(strcmp(i->data, "none") == 0) {
						info->validation |= ALPM_PKG_VALIDATION_NONE;
					} else if(strcmp(i->data, "md5") == 0) {
						info->validation |= ALPM_PKG_VALIDATION_MD5SUM;
					} else if(strcmp(i->data, "sha256") == 0) {
						info->validation |= ALPM_PKG_VALIDATION_SHA256SUM;
					} else if(strcmp(i->data, "pgp") == 0) {
						info->validation |= ALPM_PKG_VALIDATION_SIGNATURE;
					} else {
						_alpm_log(db->handle, ALPM_LOG_WARNING,
								_("unknown validation type for package %s: %s\n"),
								info->name, (const char *)i->data);
					}
				}
				FREELIST(v);
			} else if(strcmp(line, "%SIZE%") == 0) {
				READ_NEXT();
				info->isize = _alpm_strtoofft(line);
			} else if(strcmp(line, "%REPLACES%") == 0) {
				READ_AND_SPLITDEP(info->replaces);
			} else if(strcmp(line, "%DEPENDS%") == 0) {
				READ_AND_SPLITDEP(info->depends);
			} else if(strcmp(line, "%OPTDEPENDS%") == 0) {
				READ_AND_SPLITDEP(info->optdepends);
			} else if(strcmp(line, "%CONFLICTS%") == 0) {
				READ_AND_SPLITDEP(info->conflicts);
			} else if(strcmp(line, "%PROVIDES%") == 0) {
				READ_AND_SPLITDEP(info->provides);
			}
		}
		fclose(fp);
		fp = NULL;
		info->infolevel |= INFRQ_DESC;
	}

	/* FILES */
	if(inforeq & INFRQ_FILES && !(info->infolevel & INFRQ_FILES)) {
		char *path = _alpm_local_db_pkgpath(db, info, "files");
		if(!path || (fp = fopen(path, "r")) == NULL) {
			_alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
			free(path);
			goto error;
		}
		free(path);
		while(safe_fgets(line, sizeof(line), fp)) {
			_alpm_strip_newline(line, 0);
			if(strcmp(line, "%FILES%") == 0) {
				size_t files_count = 0, files_size = 0, len;
				alpm_file_t *files = NULL;

				while(safe_fgets(line, sizeof(line), fp) &&
						(len = _alpm_strip_newline(line, 0))) {
					if(!_alpm_greedy_grow((void **)&files, &files_size,
								(files_count ? (files_count + 1) * sizeof(alpm_file_t) : 8 * sizeof(alpm_file_t)))) {
						goto error;
					}
					/* since we know the length of the file string already,
					 * we can do malloc + memcpy rather than strdup */
					len += 1;
					MALLOC(files[files_count].name, len, goto error);
					memcpy(files[files_count].name, line, len);
					files_count++;
				}
				/* attempt to hand back any memory we don't need */
				if(files_count > 0) {
					files = realloc(files, sizeof(alpm_file_t) * files_count);
					/* make sure the list is sorted */
					qsort(files, files_count, sizeof(alpm_file_t), _alpm_files_cmp);
				} else {
					FREE(files);
				}
				info->files.count = files_count;
				info->files.files = files;
			} else if(strcmp(line, "%BACKUP%") == 0) {
Example #6
0
/**
 * @brief Parse a pacman-style INI config file.
 *
 * @param file path to the config file
 * @param cb callback for key/value pairs
 * @param data caller defined data to be passed to the callback
 *
 * @return the callback return value
 *
 * @note The callback will be called at the beginning of each section with an
 * empty key and value and for each key/value pair.
 *
 * @note If the parser encounters an error the callback will be called with
 * section, key, and value set to NULL and errno set by fopen, fgets, or
 * strdup.
 *
 * @note The @a key and @a value passed to @ cb will be overwritten between
 * calls.  The section name will remain valid until after @a cb is called to
 * begin a new section.
 *
 * @note Parsing will immediately stop if the callback returns non-zero.
 */
int parse_ini(const char *file, ini_parser_fn cb, void *data)
{
	char line[PATH_MAX], *section_name = NULL;
	FILE *fp = NULL;
	int linenum = 0;
	int ret = 0;

	fp = fopen(file, "r");
	if(fp == NULL) {
		return cb(file, 0, NULL, NULL, NULL, data);
	}

	while(safe_fgets(line, PATH_MAX, fp)) {
		char *key, *value, *ptr;
		size_t line_len;

		linenum++;

		/* ignore whole line and end of line comments */
		if((ptr = strchr(line, '#'))) {
			*ptr = '\0';
		}

		line_len = strtrim(line);

		if(line_len == 0) {
			continue;
		}

		if(line[0] == '[' && line[line_len - 1] == ']') {
			char *name;
			/* new config section, skip the '[' */
			name = strdup(line + 1);
			name[line_len - 2] = '\0';

			ret = cb(file, linenum, name, NULL, NULL, data);
			free(section_name);
			section_name = name;

			/* we're at a new section; perform any post-actions for the prior */
			if(ret) {
				goto cleanup;
			}
			continue;
		}

		/* directive */
		/* strsep modifies the 'line' string: 'key \0 value' */
		key = line;
		value = line;
		strsep(&value, "=");
		strtrim(key);
		strtrim(value);

		if((ret = cb(file, linenum, section_name, key, value, data)) != 0) {
			goto cleanup;
		}
	}

cleanup:
	fclose(fp);
	free(section_name);
	return ret;
}
Example #7
0
static void NOINLINE retrieve_file_data(FILE *dfp, int output_fd)
{
	char buf[512];
#if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
# if ENABLE_FEATURE_WGET_TIMEOUT
	unsigned second_cnt;
# endif
	struct pollfd polldata;

	polldata.fd = fileno(dfp);
	polldata.events = POLLIN | POLLPRI;
	ndelay_on(polldata.fd);
#endif
	progress_meter(PROGRESS_START);

	if (G.chunked)
		goto get_clen;

	/* Loops only if chunked */
	while (1) {
		while (1) {
			int n;
			unsigned rdsz;

			rdsz = sizeof(buf);
			if (G.got_clen) {
				if (G.content_len < (off_t)sizeof(buf)) {
					if ((int)G.content_len <= 0)
						break;
					rdsz = (unsigned)G.content_len;
				}
			}
#if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
# if ENABLE_FEATURE_WGET_TIMEOUT
			second_cnt = G.timeout_seconds;
# endif
			while (1) {
				if (safe_poll(&polldata, 1, 1000) != 0)
					break; /* error, EOF, or data is available */
# if ENABLE_FEATURE_WGET_TIMEOUT
				if (second_cnt != 0 && --second_cnt == 0) {
					progress_meter(PROGRESS_END);
					bb_perror_msg_and_die("download timed out");
				}
# endif
				/* Needed for "stalled" indicator */
				progress_meter(PROGRESS_BUMP);
			}
#endif
			n = safe_fread(buf, rdsz, dfp);
			if (n <= 0) {
				if (ferror(dfp)) {
					/* perror will not work: ferror doesn't set errno */
					bb_error_msg_and_die(bb_msg_read_error);
				}
				break;
			}
			xwrite(output_fd, buf, n);
#if ENABLE_FEATURE_WGET_STATUSBAR
			G.transferred += n;
			progress_meter(PROGRESS_BUMP);
#endif
			if (G.got_clen)
				G.content_len -= n;
		}

		if (!G.chunked)
			break;

		safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
 get_clen:
		safe_fgets(buf, sizeof(buf), dfp);
		G.content_len = STRTOOFF(buf, NULL, 16);
		/* FIXME: error check? */
		if (G.content_len == 0)
			break; /* all done! */
		G.got_clen = 1;
	}

	progress_meter(PROGRESS_END);
}
Example #8
0
void load_CONFIG(CONFIG *cfg, char *file, int silent)
{
   FILE *fp;
   char buff[MAX_BUFF_LEN], id[MAX_BUFF_LEN], *buff_ptr;
   int cfg_ind;

   if ((*(file) == NULL_CHAR) || ((fp=fopen(file,"r")) == NULL)){
       fprintf(stderr,"Warning: Config file %s is unreadable\n",file);
       return;
   }

   while (safe_fgets(buff,MAX_BUFF_LEN,fp) != CNULL)
       if (!is_comment(buff) && !is_comment_info(buff)){
           buff_ptr = buff;
           strip_newline(buff_ptr);
           wrdcpy(id,buff_ptr);
           if ((cfg_ind = is_CONFIG_id(cfg,id)) != NOT_CONFIG_ID){
               find_next_word(&buff_ptr);
               switch (cfg->rec_list[cfg_ind].value_type){
                   case CFG_TGL:
                       cfg->rec_list[cfg_ind].value = (char *)TOGGLE_ON_CHR;
                       break;
                   case CFG_C:
                       wrdcpy(cfg->rec_list[cfg_ind].value,buff_ptr);
                       break;
                   case CFG_C2:{
                       char **t_arr;
                       int arg_count = 0;

                       t_arr = (char **)cfg->rec_list[cfg_ind].value;

                       while ((*buff_ptr != NULL_CHAR) &&
                              (arg_count < cfg->rec_list[cfg_ind].num_elem)){
                           wrdcpy(t_arr[arg_count++],buff_ptr);
                           find_next_word(&buff_ptr);
                       }
                       if ((arg_count == cfg->rec_list[cfg_ind].num_elem) &&
                           (*buff_ptr != NULL_CHAR))
                        if (!silent){
                         fprintf(stderr,"Warning: Ignored Config elements\n");
                         fprintf(stderr,"         %s of\n         %s\n",
                                         buff_ptr,buff);
			}
                       break;
		   }
                   case CFG_STR:
                       strcpy(cfg->rec_list[cfg_ind].value,buff_ptr);
                       break;
                   default:
                       fprintf(stderr,"Error: Unknown CONFIG type %d\n",
                             cfg->rec_list[cfg_ind].value_type);
                       exit(-1);
                       break;
	       }
               reset_group(cfg,cfg_ind);
	   }
           else if (!silent)
              fprintf(stderr,"Warning: Configuration not usable - %s\n",buff);
       }
   fclose(fp);
}
Example #9
0
static int
retrieve_file_data(struct globals *state,
                   FILE *dfp,
                   int (*progress)(void *data, int current, int total),
                   int (*output_func)(void *data, char *bytes, int len),
                   void *data)
{
	char buf[4*1024]; /* made bigger to speed up local xfers */
	unsigned second_cnt;
	struct pollfd polldata;

	polldata.fd = fileno(dfp);
	polldata.events = POLLIN | POLLPRI;
	progress(data, 0, state->total_len);

	if (state->chunked)
		goto get_clen;

	/* Loops only if chunked */
	while (1) {

		ndelay_on(polldata.fd);
		while (1) {
			int n;
			unsigned rdsz;

			rdsz = sizeof(buf);
			if (state->got_clen) {
				if (state->content_len < (off_t)sizeof(buf)) {
					if ((int)state->content_len <= 0)
						break;
					rdsz = (unsigned)state->content_len;
				}
			}
			second_cnt = state->timeout_seconds;
			while (1) {
				if (safe_poll(&polldata, 1, 1000) != 0)
					break; /* error, EOF, or data is available */
				if (second_cnt != 0 && --second_cnt == 0) {
					progress(data, -1, state->total_len);
					ERROR("download timed out");
					return -1;
				}
				/* Needed for "stalled" indicator */
				progress(data, state->transferred, state->total_len);
			}
			/* fread internally uses read loop, which in our case
			 * is usually exited when we get EAGAIN.
			 * In this case, libc sets error marker on the stream.
			 * Need to clear it before next fread to avoid possible
			 * rare false positive ferror below. Rare because usually
			 * fread gets more than zero bytes, and we don't fall
			 * into if (n <= 0) ...
			 */
			clearerr(dfp);
			errno = 0;
			n = safe_fread(buf, rdsz, dfp);
			/* man fread:
			 * If error occurs, or EOF is reached, the return value
			 * is a short item count (or zero).
			 * fread does not distinguish between EOF and error.
			 */
			if (n <= 0) {
				if (errno == EAGAIN) /* poll lied, there is no data? */
					continue; /* yes */
				if (ferror(dfp))
					ERROR("Could not read file");
				break; /* EOF, not error */
			}

			output_func(data, buf, n);
			state->transferred += n;
			progress(data, state->transferred, state->total_len);
			if (state->got_clen) {
				state->content_len -= n;
				if (state->content_len == 0)
					break;
			}
		}
		ndelay_off(polldata.fd);

		if (!state->chunked)
			break;

		safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
 get_clen:
		safe_fgets(buf, sizeof(buf), dfp);
		state->content_len = strtol(buf, NULL, 16);
		/* FIXME: error check? */
		if (state->content_len == 0)
			break; /* all done! */
		state->got_clen = 1;
	}

	progress(data, state->transferred, state->total_len);
	return 0;
}