LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2013, 03:26 PM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
Question 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]);
The above use of snprintf is a sort of trick to avoid the error which occurs with :
Code:
strncat( buffer , aoption[i] , PATH_MAX - strlen(buffer) - 1);
because aoption is a pointer.
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] ){

	char buffer[PATH_MAX];
	char charo[PATH_MAX];
	int i, chd, cx , j ; 
	strncpy( buffer, "" , PATH_MAX);
	werase( stdscr);

	for ( i=0; ( i <= strlen( aoption ) ) ; i++ ) 
	{
		//	printw(  ">%c ", aoption[i]);
		//	refresh();
		// strncat( buffer , "-" , PATH_MAX - strlen(buffer) - 1);
		j = snprintf( charo, 5 , "%c", aoption[i]);
		switch ( charo[0] ) {
			case '&':
				strncat( buffer , "\\" , PATH_MAX - strlen(buffer) - 1);
				strncat( buffer , "&" , PATH_MAX - strlen(buffer) - 1);
				break;



			case '\'':
				strncat( buffer , "\\'" , PATH_MAX - strlen(buffer) - 1);
				break;



			case ' ':
				strncat( buffer , "\\ " , PATH_MAX - strlen(buffer) - 1);
				break;

			default:
				strncat( buffer , charo , PATH_MAX - strlen(buffer) - 1);
				break;
		}

	}
	size_t len = strlen(buffer);
	char *r = malloc(len+1);
	return r ? memcpy(r, buffer, len+1) : NULL;
}

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

Last edited by Xeratul; 03-10-2013 at 03:35 PM.
 
Old 03-10-2013, 04:04 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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]);
new: charo[0]= aoption[i]; charo[1]= 0; j= 1;
PS: What is the transformation you're trying to perform? Please explain in words.

Last edited by NevemTeve; 03-10-2013 at 04:10 PM.
 
Old 03-10-2013, 11:27 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 03-11-2013, 08:03 AM   #4
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Use a char pointer to iterate over both the file name and the output buffer, as in:
Code:
char *filename_escape(const char *path){
    char buffer[PATH_MAX], *out = buffer;
    const char *in = path;

#define PUTCHAR(ch) do { \
    *out = ch; \
    if (++out == buffer + sizeof buffer) \
        return NULL;
} while (0)

    for (; *in; ++in) {
        PUTCHAR(*in);
        /* do your magic here */
    }

#undef PUTCHAR

    /* ... */
}
You might be better off having a dynamic buffer though, since path is not limited by PATH_MAX.
 
Old 03-11-2013, 05:24 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Xeratul View Post
...
The whole code is not elegant
...
If you are not sure the wheels should be reinvented, start from the following:

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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can we convert a dynamic library (filename.so) to a static library (filename.a). hanumaan Programming 4 11-20-2009 12:07 AM
how to convert address in current process to filename:line number telia Linux - General 5 01-12-2009 04:06 AM
Convert filename extension to lowercase jakev383 Programming 6 12-07-2008 06:24 AM
how to convert filename mojibake back into original encoding?? wordsmithy Linux - General 1 05-13-2008 02:13 AM
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 06:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration