Example #1
0
jm_status_enu_t jm_rmdir(jm_callbacks* cb, const char* dir) {
#ifdef WIN32
	const char* fmt_cmd = "rmdir /s /q %s";
#else
    const char* fmt_cmd = "rm -rf %s";
#endif
    char * buf = (char*)cb->calloc(sizeof(char), strlen(dir)+strlen(fmt_cmd)+1);
	if(!cb) {
		cb = jm_get_default_callbacks();
	}
	if(!buf) {
	    jm_log_error(cb,module,"Could not allocate memory");
		return jm_status_error;
	}
    sprintf(buf, fmt_cmd, dir);/*safe*/
#ifdef WIN32
	{
		char* ch = buf+strlen(fmt_cmd) - 2;
		while(*ch) {
			if(*ch == '/') *ch = '\\';
			ch++;
		}
	}
#endif
    jm_log_verbose(cb,module,"Removing %s", dir);
    if(system(buf)) {
	    jm_log_error(cb,module,"Error removing %s (%s)", dir, strerror(errno));
		return jm_status_error;
	}
    cb->free(buf);
	return jm_status_success;
}
void  fmi2_log_forwarding_v(fmi2_component_environment_t c, fmi2_string_t instanceName, fmi2_status_t status, fmi2_string_t category, fmi2_string_t message, va_list args) {
    char buf[50000], *curp;
	const char* statusStr;
	fmi2_import_t* fmu = (fmi2_import_t*)c;
	jm_callbacks* cb;
	jm_log_level_enu_t logLevel;

	if(fmu) {
		 cb = fmu->callbacks;
	}
	else 
		cb = jm_get_default_callbacks();
	logLevel = cb->log_level;
	switch(status) {
		case fmi2_status_discard:
		case fmi2_status_pending:
		case fmi2_status_ok:
			logLevel = jm_log_level_info;
			break;
		case fmi2_status_warning:
			logLevel = jm_log_level_warning;
			break;
		case fmi2_status_error:
			logLevel = jm_log_level_error;
			break;
		case fmi2_status_fatal:
		default:
			logLevel = jm_log_level_fatal;
	}

    if(logLevel > cb->log_level) return;

	curp = buf;
    *curp = 0;

	if(category) {
        sprintf(curp, "[%s]", category);
        curp += strlen(category)+2;
    }
	statusStr = fmi2_status_to_string(status);
    sprintf(curp, "[FMU status:%s] ", statusStr);
        curp += strlen(statusStr) + strlen("[FMU status:] ");
	vsprintf(curp, message, args);

	if(fmu) {
		fmi2_import_expand_variable_references(fmu, buf, cb->errMessageBuffer,JM_MAX_ERROR_MESSAGE_SIZE);
	}
	else {
		strncpy(cb->errMessageBuffer, buf, JM_MAX_ERROR_MESSAGE_SIZE);
		cb->errMessageBuffer[JM_MAX_ERROR_MESSAGE_SIZE - 1] = '\0';
	}
	if(cb->logger) {
		cb->logger(cb, instanceName, logLevel, cb->errMessageBuffer);
	}

}
Example #3
0
char* jm_create_URL_from_abs_path(jm_callbacks* cb, const char* path) {
	/* worst case: all symbols are 4-byte UTF-8 and need to be %-encoded */
#define MAX_URL_LENGTH  (FILENAME_MAX * 4 * 3 + 7)
	char buffer[MAX_URL_LENGTH];
	char* url;
	size_t urllen;
	if(!cb) {
		cb = jm_get_default_callbacks();
	}

#if defined(_WIN32) || defined(WIN32)
	{
		DWORD pathLen = MAX_URL_LENGTH;
		HRESULT code = UrlCreateFromPathA(
			path,
			buffer,
			&pathLen,
			0);
		if( (code != S_FALSE) && (code != S_OK)) {
			jm_log_fatal(cb, module,"Could not constuct file URL from path %s", path);
			return 0;
		}
		urllen = pathLen;
	}
#else
	{
		size_t i, len = strlen(path);
		char *curBuf = buffer + 7;
		unsigned char ch;
		strcpy(buffer, "file://");
		for( i = 0; i < len; i++) {
			ch = (unsigned char)path[i];
			if( (ch == '/') || ((ch >= 'A') && (ch <= 'Z')) 
				|| ((ch >= 'a') && (ch <= 'z'))
				|| (ch == '-') || (ch == '_') || (ch == '.') ||(ch == '~')) {
					*curBuf = ch;
					curBuf++;
					continue;
			}
			sprintf(curBuf, "%%%2X", (int)ch);/*safe*/
			curBuf+=3;
		}
		*curBuf = 0;
		urllen = curBuf - buffer;
	}
#endif
	url = (char*)cb->malloc(urllen+1);
	if(!url) {
		jm_log_fatal(cb, module,"Could not allocate memory");
		return 0;
	}
	strcpy(url, buffer);
	return url;
}
Example #4
0
jm_status_enu_t jm_mkdir(jm_callbacks* cb, const char* dir) {
	if(!cb) {
		cb = jm_get_default_callbacks();
	}
	if(MKDIR(dir)) {
		jm_log_fatal(cb,module,"Could not create directory %s", dir);
		return jm_status_error;
	}
	else
		return jm_status_success;
}
Example #5
0
char* jm_mk_temp_dir(jm_callbacks* cb, const char* systemTempDir, const char* tempPrefix)
{
	size_t len;

	char tmpDir[FILENAME_MAX + 2];
	char* tmpPath;

	if(!cb) {
		cb = jm_get_default_callbacks();
	}
	if(!systemTempDir) {
		systemTempDir = jm_get_system_temp_dir();
		if(!systemTempDir) systemTempDir = "./";
	}
	if(!tempPrefix) {
		tempPrefix = "jm";
	}
	len = strlen(systemTempDir);

	if(!jm_get_dir_abspath(cb, systemTempDir, tmpDir, FILENAME_MAX + 2)) {
		return 0;
	}

	len = strlen(tmpDir);
	if(tmpDir[len-1] != FMI_FILE_SEP[0]) {
		tmpDir[len] = FMI_FILE_SEP[0]; 
		tmpDir[len+1] = 0;
		len++;
	}
	len += strlen(tempPrefix) + 6;
	if(len + 16 > FILENAME_MAX) {
		jm_log_fatal(cb,module, "Canonical name for the temporary files directory is too long (system limit for path length is %d)", FILENAME_MAX);
		return 0;
	}
	tmpPath = (char*)cb->malloc(len + 7);
	if(!tmpPath) {
		jm_log_fatal(cb, module,"Could not allocate memory");
		return 0;
	}
	sprintf(tmpPath,"%s%sXXXXXX",tmpDir,tempPrefix);/*safe*/

	if(!jm_mktemp(tmpPath)) {
		jm_log_fatal(cb, module,"Could not create a unique temporary directory name");
		cb->free(tmpPath);
		return 0;
	}
	if(jm_mkdir(cb,tmpPath) != jm_status_success) {
		cb->free(tmpPath);
		return 0;
	}
	return tmpPath;
}
Example #6
0
char* jm_get_dir_abspath(jm_callbacks* cb, const char* dir, char* outPath, size_t len) {
	char curDir[FILENAME_MAX + 2];

	if(!cb) {
		cb = jm_get_default_callbacks();
	}
	if( jm_portability_get_current_working_directory(curDir, FILENAME_MAX+1) != jm_status_success) {
		jm_log_fatal(cb,module, "Could not get current working directory (%s)", strerror(errno));
		return 0;
	};

	if(jm_portability_set_current_working_directory(dir) != jm_status_success) {
		jm_log_fatal(cb,module, "Could not change to the directory %s", dir);
		jm_portability_set_current_working_directory(curDir);
		return 0;
	};
	if( jm_portability_get_current_working_directory(outPath, len) != jm_status_success) {
		jm_log_fatal(cb,module, "Could not get absolute path for the directory (%s)", strerror(errno));
		jm_portability_set_current_working_directory(curDir);
		return 0;
	};
	jm_portability_set_current_working_directory(curDir);
	return outPath;
}
fmi1_xml_model_description_t * fmi1_xml_allocate_model_description( jm_callbacks* callbacks) {
    jm_callbacks* cb;
    fmi1_xml_model_description_t* md;

    if(callbacks) {
        cb = callbacks;
    }
    else {
        cb = jm_get_default_callbacks();
    }
    md = (fmi1_xml_model_description_t*)cb->malloc(sizeof(fmi1_xml_model_description_t));
    if(!md) {
		jm_log_fatal(cb, module, "Could not allocate memory");
		return 0;
	}

    md->callbacks = cb;

    md->status = fmi1_xml_model_description_enu_empty;

    jm_vector_init(char)( & md->fmi1_xml_standard_version, 0,cb);
    jm_vector_init(char)(&md->modelName, 0,cb);
    jm_vector_init(char)(&md->modelIdentifier, 0,cb);
    jm_vector_init(char)(&md->GUID, 0,cb);
    jm_vector_init(char)(&md->description, 0,cb);
    jm_vector_init(char)(&md->author, 0,cb);
    jm_vector_init(char)(&md->version, 0,cb);
    jm_vector_init(char)(&md->generationTool, 0,cb);
    jm_vector_init(char)(&md->generationDateAndTime, 0,cb);


    md->namingConvension = fmi1_naming_enu_flat;
    md->numberOfContinuousStates = 0;
    md->numberOfEventIndicators = 0;

    md->defaultExperimentStartTime = 0;

    md->defaultExperimentStopTime = 1.0;

    md->defaultExperimentTolerance = FMI1_DEFAULT_EXPERIMENT_TOLERANCE;

    jm_vector_init(jm_voidp)(&md->vendorList, 0, cb);

    jm_vector_init(jm_named_ptr)(&md->unitDefinitions, 0, cb);
    jm_vector_init(jm_named_ptr)(&md->displayUnitDefinitions, 0, cb);

    fmi1_xml_init_type_definitions(&md->typeDefinitions, cb);

    jm_vector_init(jm_named_ptr)(&md->variablesByName, 0, cb);

	md->variablesOrigOrder = 0;

	md->variablesByVR = 0;

	md->inputVariables = 0;

	md->outputVariables = 0;

    jm_vector_init(jm_string)(&md->descriptions, 0, cb);

    md->fmuKind = fmi1_fmu_kind_enu_me;

    fmi1_xml_init_capabilities(&md->capabilities);

    jm_vector_init(jm_string)(&md->additionalModels, 0, cb);

    jm_vector_init(char)(&md->entryPoint, 0,cb);
    jm_vector_init(char)(&md->mimeType, 0,cb);

    return md;
}
void  fmi1_log_forwarding_v(fmi1_component_t c, fmi1_string_t instanceName, fmi1_status_t status, fmi1_string_t category, fmi1_string_t message, va_list args) {
    char buf[50000], *curp;
	const char* statusStr;
	fmi1_import_t* fmu = 0;
	jm_callbacks* cb = jm_get_default_callbacks();
	jm_log_level_enu_t logLevel = jm_log_level_error;
	if(fmi1_import_active_fmu) {
		size_t n = jm_vector_get_size(jm_voidp)(fmi1_import_active_fmu);
		size_t i;
		for(i= 0; i < n; i++) {
			fmu = (fmi1_import_t*)jm_vector_get_item(jm_voidp)(fmi1_import_active_fmu, i);
			if(fmu->capi->c == c) {
				cb = fmu->callbacks;
				break;
			}
		}
		if(i >= n) { /* Could not find matching FMU -> use default callbacks */
			fmu = 0;
			cb = jm_get_default_callbacks();
		}
	}
	switch(status) {
		case fmi1_status_discard:
		case fmi1_status_pending:
		case fmi1_status_ok:
			logLevel = jm_log_level_info;
			break;
		case fmi1_status_warning:
			logLevel = jm_log_level_warning;
			break;
		case fmi1_status_error:
			logLevel = jm_log_level_error;
			break;
		case fmi1_status_fatal:
		default:
			logLevel = jm_log_level_fatal;
	}

        if(logLevel > cb->log_level) return;

	curp = buf;
    *curp = 0;

	if(category) {
        sprintf(curp, "[%s]", category);
        curp += strlen(category)+2;
    }
	statusStr = fmi1_status_to_string(status);
    sprintf(curp, "[FMU status:%s] ", statusStr);
        curp += strlen(statusStr) + strlen("[FMU status:] ");
	vsprintf(curp, message, args);

	if(fmu) {
		fmi1_import_expand_variable_references(fmu, buf, cb->errMessageBuffer,JM_MAX_ERROR_MESSAGE_SIZE);
	}
	else {
		strncpy(cb->errMessageBuffer, buf, JM_MAX_ERROR_MESSAGE_SIZE);
		cb->errMessageBuffer[JM_MAX_ERROR_MESSAGE_SIZE - 1] = '\0';
	}
	if(cb->logger) {
		cb->logger(cb, instanceName, logLevel, cb->errMessageBuffer);
	}

}