FILE* fopen(const char *fname, const char *flag) {
    FILE *fp;
    size_t Len;
    char *new_fname = NULL;
    file_line_number = 0;

    /* Appends a prefix string for output files */
    if (out_file_prefix) {
        if (std::strchr(flag, 'w')) {
            Len = 1; /* NULL char */
            Len += std::strlen(out_file_prefix);
            Len += std::strlen(fname);
            new_fname = (char *) vtr::malloc(Len * sizeof(char));
            strcpy(new_fname, out_file_prefix);
            strcat(new_fname, fname);
            fname = new_fname;
        }
    }

    if (NULL == (fp = std::fopen(fname, flag))) {
        throw VtrError(string_fmt("Error opening file %s for %s access: %s.\n", fname, flag, strerror(errno)), __FILE__, __LINE__);        
    }

    if (new_fname)
        std::free(new_fname);

    return (fp);
}
char* fgets(char *buf, int max_size, FILE * fp) {
    /* Get an input line, update the line number and cut off *
     * any comment part.  A \ at the end of a line with no   *
     * comment part (#) means continue. vtr::fgets should give * 
     * identical results for Windows (\r\n) and Linux (\n)   *
     * newlines, since it replaces each carriage return \r   *
     * by a newline character \n.  Returns NULL after EOF.     */

    int ch;
    int i;

    cont = 0; /* line continued? */
    file_line_number++; /* global variable */

    for (i = 0; i < max_size - 1; i++) { /* Keep going until the line finishes or the buffer is full */

        ch = std::fgetc(fp);

        if (std::feof(fp)) { /* end of file */
            if (i == 0) {
                return NULL ; /* required so we can write while (vtr::fgets(...) != NULL) */
            } else { /* no newline before end of file - last line must be returned */
                buf[i] = '\0';
                return buf;
            }
        }

        if (ch == '#') { /* comment */
            buf[i] = '\0';
            while ((ch = std::fgetc(fp)) != '\n' && !std::feof(fp))
                ; /* skip the rest of the line */
            return buf;
        }

        if (ch == '\r' || ch == '\n') { /* newline (cross-platform) */
            if (i != 0 && buf[i - 1] == '\\') { /* if \ at end of line, line continued */
                cont = 1;
                buf[i - 1] = '\n'; /* May need this for tokens */
                buf[i] = '\0';
            } else {
                buf[i] = '\n';
                buf[i + 1] = '\0';
            }
            return buf;
        }

        buf[i] = ch; /* copy character into the buffer */

    }

    /* Buffer is full but line has not terminated, so error */
    throw VtrError(string_fmt("Error on line %d -- line is too long for input buffer.\n"
                              "All lines must be at most %d characters long.\n", BUFSIZE - 2),
                    __FILE__, __LINE__);
    return NULL;
}
int atoi(const char *str) {

    /* Returns the integer represented by the first part of the character       *
     * string.                                              */

    if (str[0] < '0' || str[0] > '9') {
        if (!(str[0] == '-' && str[1] >= '0' && str[1] <= '9')) {
            throw VtrError(string_fmt("Expected integer instead of '%s'", str), __FILE__, __LINE__);
        }
    }
    return std::atoi(str);
}
Пример #4
0
void StatusPort::on_refresh()
{
	//Set the new value for confidence bar
  string value;
  string_fmt(value,m_pct);
  value = value + "%";
  m_confidence_bar.set_fraction(m_pct/100.0);
  m_confidence_bar.set_text(value);

	// set new value for text output
  m_refTextBuffer1->set_text(m_text);
  m_analysis_text.set_buffer(m_refTextBuffer1);
}
Пример #5
0
void rmrf(const char *path) {
	struct stat st;
	if (kga_lstat_skip_enoent(path, &st)) return;
	if (S_ISDIR(st.st_mode)) {
		scope {
			char *sub_path = string_new();
			DIR *dir = kga_opendir(path);
			for (struct dirent *dirent; (dirent = readdir(dir)); ) {
				if (dirent->d_name[0] == '.' && (dirent->d_name[1] == '\0' || (dirent->d_name[1] == '.' && dirent->d_name[2] == '\0'))) {
					continue;
				};
				string_fmt(sub_path, "%s/%s", path, dirent->d_name);
				rmrf(sub_path);
			};
		};
	};