I was looking at this post. This lead me to the Allegro development road map. I thought I would contribute one of the "Nice To Have" section.
// header file #ifndef __al_included_allegro5_alprintf_h #define __al_included_allegro5_alprintf_h #include <allegro5\allegro.h> #include <stdarg.h> bool al_vfprintf(ALLEGRO_FILE *pfile, const char *format, va_list args); bool al_fprintf(ALLEGRO_FILE *pfile, const char *format, ...); #endif // __al_included_allegro5_alprintf_h
You beat me to it by actually doing work, you monster!
Is there any reason you have two functions instead of just one?
The first one should probably be called al_vfprintf rather than al_fprintf. Not least of which because c doesn't let you overload functions.
Trent Been working with ALLEGRO_USTR alot lately and since it has two appendf functions (... & va_args). Just copying the same style.
Thomas fixed
I think this is a useful addition for someone to add. Will try to remember to do it myself.
I agree. This is definitely useful. Between supporting format printing to files, it also uses the ustr code to make sure UTF8 writing works.
Bump to make sure this doesn't go forgotten. Both functions are nice.
46 rv = al_fprintf(pfile, format, args);
I think you meant al_vfprintf there, since you changed the name of the function.
Can you test this code a bit? hammer it a little with invalid input and the like? Once validated, I can apply.
Edgar Fixed
Thomas If someone else could tackle this. I don't quite have the time for a while.
I have put the code into aintern_file, and will test it a bit. Instead of returning a bool I would prefer to return an int, indicating the number of bytes written...
tobing I had it return bool because of the use of ustr's appendf function. Of course I agree it should return the size written to file.
I haven't used plain C in forever. Does it support size_t?
// header file #ifndef __al_included_allegro5_alprintf_h #define __al_included_allegro5_alprintf_h #include <allegro5\allegro.h> #include <stdarg.h> size_t al_vfprintf(ALLEGRO_FILE *pfile, const char *format, va_list args); size_t al_fprintf(ALLEGRO_FILE *pfile, const char *format, ...); #endif // __al_included_allegro5_alprintf_h
On a side note:
al_ustr_dup(al_ustr_empty_string());
v.s.
al_ustr_new("");
P.S. at work so can't actually run and test the code I just changed
size_t is valid in C as well, and most functions, especially newer ones will use size_t over int. size_t may even be 64bit on a 64bit platform.
Here's a patch for current origin/5.1 in git, that contains the last implementation by DanielH, but with al_ustr_new("") instead of the dup on empty string.
I have tested this a bit, and works fine with unicode characters.
This is committed. I added some quick documentation.
Also changed it to return a negative number on error like stdio fprintf.
Thanks for submitting and doing the cleanup.
The return is a size_t, so returning -1 is not exactly doing what it should do, so maybe we should return int instead of size_t?
The libc fprintf returns the number of bytes in an int, so I'd vote for that. Someday 2147483648 bytes in a single write won't be enough, but today is not that day.
The return is a size_t, so returning -1 is not exactly doing what it should do, so maybe we should return int instead of size_t?
Good catch, thanks. Changing it to int.
Thanks guys.
However, can someone answer a previous question?
al_ustr_dup(al_ustr_empty_string());
v.s.
al_ustr_new("");
I see that my code was changed for the latter case. In looking at other's code, I've seen both.
Ah, I didn't realize that this was a question.
Well, I used the second variant because the result seems to be the same, but uses only one call.
You're right. I did post it, but didn't phrase it as a question.
Small patch to fix a warning about comparison between signed and unsigned integers (I hate seeing warnings) :
Committed.
Thanks a lot!