示例#1
0
	std::string exec(const char* cmd) {
		// http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c
		// std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
		// TODO

		// fork(), which is called by popen(), fails when the parent process
		// already consumes a lot of memory.
		// TODO: need a pre-forking.
		FILE* pipe = popen(cmd, "r");
		if (pipe == NULL) {
			if (errno == ENOMEM) {
				throw ErrorNoMem();
			} else {
				throw runtime_error(str(boost::format("Popen returned NULL. errno=%d") % errno));
			}
		}

		const size_t BUFSIZE = 1024;
		char buffer[BUFSIZE];
		string result = "";
		while (!feof(pipe)) {
			if (fgets(buffer, BUFSIZE, pipe) != NULL)
				result += buffer;
		}

		pclose(pipe);

		return result;
	}
示例#2
0
void ILU1_Precond::add_element( int *ptr, int **col, int n, int &nz, int &asize, int c )
{
    if( nz == asize ) {
	// Reallocate memory
	asize += n;
	int *ncol = (int *)realloc( (void *)(*col), asize*sizeof(int) );
	if( ncol == NULL ) {
	    free( ptr );
	    free( *col );
	    throw( ErrorNoMem( ERROR_LOCATION ) );
	}
	(*col) = ncol;
    }
    (*col)[nz] = c;
    nz++;
}