C: non elegant way to convert filename to unix... :(
Hello,
In C programming language, this code below is working well. It is fine. It allows to convert. I need sthg compatible and Posix. So it is posix ;) :) "file with spaces.txt" to "file\ with\ spaces.txt" It does convert the string (array of chars) to *nix. However this code is really not elegant at all. Would you know Qu(1) how to avoid: Code:
j = snprintf( charo, 5 , "%c", aoption[i]); Code:
strncat( buffer , aoption[i] , PATH_MAX - strlen(buffer) - 1); I would rather use strncat but no idea how. snprintf is not sexy, and it is not a reliable work-around. The use of strncat is more reliable. How to use strncat with this line above? Qu(2) The whole code is not elegant and it may crash depending on the file names which are passed to aoption. Code:
char *filename_c_to_unix( char aoption[PATH_MAX] ){ The most important question to me is to learn how to use strncat with converting/using a pointer to my regular array of chars (knowing that I all the time use a regular array of chars). Would you have a better line for question (1)? This question/issue qu(1) is the most important to me. Looking forward to reading you ! Many thanks |
I don't really understand your code, but please, please, don't put strlen into cycle-condition, it is extremely in-efficient.
Code:
old: j = snprintf( charo, 5 , "%c", aoption[i]); |
Normally at the cli you'd use eg sed to convert spaces to underscores in a string; better than '\ '.
C has a regex lib, so I'd use that. |
Use a char pointer to iterate over both the file name and the output buffer, as in:
Code:
char *filename_escape(const char *path){ |
Quote:
http://freecode.com/projects/bstring http://freecode.com/projects/vstr http://freecode.com/projects/str http://freecode.com/projects/libstrvar http://freecode.com/projects/libsbuf http://freecode.com/projects/libtext http://www.and.org/ustr/ http://libhx.sourceforge.net/ http://www.theiling.de/projects/erwin.html http://www.koanlogic.com/libu/api/html/index.html http://judy.sourceforge.net/ - I once got truly interested in this. |
All times are GMT -5. The time now is 10:40 AM. |