project_files/frontlib/util/util.c
branchflibqtfrontend
changeset 8094 6c5b4e69f03d
parent 7316 f7b49b2c5d84
equal deleted inserted replaced
8092:08960209db8c 8094:6c5b4e69f03d
    35 	va_end(argp);
    35 	va_end(argp);
    36 	return result;
    36 	return result;
    37 }
    37 }
    38 
    38 
    39 char *flib_vasprintf(const char *fmt, va_list args) {
    39 char *flib_vasprintf(const char *fmt, va_list args) {
    40 	char *result = NULL;
    40     char *result = NULL;
    41 	if(!log_badargs_if(fmt==NULL)) {
    41 	if(!log_badargs_if(fmt==NULL)) {
    42 		int requiredSize = vsnprintf(NULL, 0, fmt, args)+1;					// Figure out how much memory we need,
    42         va_list args_copy;
    43 		if(!log_e_if(requiredSize<0, "Error formatting string with template \"%s\"", fmt)) {
    43         va_copy(args_copy, args);
       
    44 
       
    45         int requiredSize = vsnprintf(NULL, 0, fmt, args_copy)+1;					// Figure out how much memory we need,
       
    46 
       
    47         va_end(args_copy);
       
    48 
       
    49         if(!log_e_if(requiredSize<0, "Error formatting string with template \"%s\"", fmt)) {
    44 			char *tmpbuf = flib_malloc(requiredSize);						// allocate it
    50 			char *tmpbuf = flib_malloc(requiredSize);						// allocate it
    45 			if(tmpbuf && vsnprintf(tmpbuf, requiredSize, fmt, args)>=0) {	// and then do the actual formatting.
    51             if(tmpbuf && vsnprintf(tmpbuf, requiredSize, fmt, args)>=0) {	// and then do the actual formatting.
    46 				result = tmpbuf;
    52 				result = tmpbuf;
    47 				tmpbuf = NULL;
    53 				tmpbuf = NULL;
    48 			}
    54 			}
    49 			free(tmpbuf);
    55 			free(tmpbuf);
    50 		}
    56 		}