Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Advanced reading from stdin with standard C (http://www.velocityreviews.com/forums/t551313-advanced-reading-from-stdin-with-standard-c.html)

Joakim Hove 11-12-2007 03:25 PM

Advanced reading from stdin with standard C
 
[I am sorry if this post is a contradiction in terms - i.e. if there
is no such thing as stdio in the C standard?]

Hello,

I am reading a filename from the user - the loop is typically like
this:

1. Read a directory from the user.
2. Read several filenames from the user.

The filenames read in 2. above will typically (but not necessarily) be
located in the directory entered under point 1., so to make it more
streamlined for the user I would like something like

Give name of file with.. => /some/path/given/first/_

I.e. the path /some/path/given/first/ should be printed first, and
then user can continue editing. I have the following two requirements:

1. When the user has completed the input buffer should contain the
full path to some file. [This I can manage by initalising the input
buffer.]
2. If some file is located in another location, the user should be
able to delete parts of the path, so the final result can be .e.g. /
some/other/path/file.txt

I have looked into gnu readline - but it does not seem it provides
this functionality. Any tips gretly appreciated.

Joakim Hove


Julienne Walker 11-12-2007 03:49 PM

Re: Advanced reading from stdin with standard C
 
On Nov 12, 10:25 am, Joakim Hove <joakim.h...@gmail.com> wrote:
> 2. If some file is located in another location, the user should be
> able to delete parts of the path, so the final result can be .e.g. /
> some/other/path/file.txt


That's outside the bounds of C and in the realm of whatever program
handles input before it's sent to your program. To do what you want
would require a non-portable solution that gives you more control over
console input. As it is, you only get to see the final result (after
any edits are made) that's returned by fgets or whatnot.


Walter Roberson 11-12-2007 05:57 PM

Re: Advanced reading from stdin with standard C
 
In article <1194881157.890300.239720@v3g2000hsg.googlegroups. com>,
Joakim Hove <joakim.hove@gmail.com> wrote:
>I.e. the path /some/path/given/first/ should be printed first, and
>then user can continue editing. I have the following two requirements:


>1. When the user has completed the input buffer should contain the
>full path to some file. [This I can manage by initalising the input
>buffer.]


No you cannot manage that by initializing the input buffer.
It contradicts your point 2, that the user should be able to edit
the directory path: if the user can edit the directory path, they
can edit right back to the beginning, erasing even the leading
directory delimeter, leaving only an unqualified (no directory)
filename.

>2. If some file is located in another location, the user should be
>able to delete parts of the path, so the final result can be .e.g. /
>some/other/path/file.txt


>I have looked into gnu readline - but it does not seem it provides
>this functionality. Any tips gretly appreciated.


I haven't used gnu readline before, but a quick check of the
documentation suggests to me that you could call rl_insert_text()
to insert the directory path before allowing the user to do any
editting.
http://tiswww.case.edu/php/chet/read...ine.html#SEC36

But you should check that with a newsgroup or mailing list
that deals with gnu readline. If you do use gnu readline, be sure
to read the licensing terms first.

What you are asking for is not possible using only the facilities
provided by the C I/O library.
--
"Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us." -- Ecclesiastes

Ben Bacarisse 11-12-2007 05:58 PM

Re: Advanced reading from stdin with standard C
 
Joakim Hove <joakim.hove@gmail.com> writes:

> I am reading a filename from the user - the loop is typically like
> this:
>
> 1. Read a directory from the user.
> 2. Read several filenames from the user.
>
> The filenames read in 2. above will typically (but not necessarily) be
> located in the directory entered under point 1., so to make it more
> streamlined for the user I would like something like
>
> Give name of file with.. => /some/path/given/first/_
>
> I.e. the path /some/path/given/first/ should be printed first, and
> then user can continue editing. I have the following two requirements:
>
> 1. When the user has completed the input buffer should contain the
> full path to some file. [This I can manage by initalising the input
> buffer.]
> 2. If some file is located in another location, the user should be
> able to delete parts of the path, so the final result can be .e.g. /
> some/other/path/file.txt
>
> I have looked into gnu readline - but it does not seem it provides
> this functionality. Any tips gretly appreciated.


<Off-topic>I think you'll find GNU readline will do just fine -- at
least I can do what you seem to want in a few lines.

A post in comp.unix.programmer might help.</off-topic>

--
Ben.

santosh 11-12-2007 06:58 PM

Re: Advanced reading from stdin with standard C
 
In article <1194881157.890300.239720@v3g2000hsg.googlegroups. com>,
Joakim Hove <joakim.hove@gmail.com> wrote on Monday 12 Nov 2007 8:55
pm:

> [I am sorry if this post is a contradiction in terms - i.e. if there
> is no such thing as stdio in the C standard?]


Standard C does specify I/O functions collected under a header called
stdio.h.

> I am reading a filename from the user - the loop is typically like
> this:
>
> 1. Read a directory from the user.


Standard C has no support for directories. But POSIX has standardised
functions for accessing directories.

> 2. Read several filenames from the user.
>
> The filenames read in 2. above will typically (but not necessarily) be
> located in the directory entered under point 1., so to make it more
> streamlined for the user I would like something like
>
> Give name of file with.. => /some/path/given/first/_
>
> I.e. the path /some/path/given/first/ should be printed first, and
> then user can continue editing. I have the following two requirements:


If I understand you correctly, you want to print out the path and then
let the user type the filename just after it and also edit the path
string if they so desire.

Standard C has no support for command line editing. This is usually
provided by your operating system's terminal code. Alternatively you
might consider the reasonably portable curses library. Versions exist
for most major systems.

> 1. When the user has completed the input buffer should contain the
> full path to some file. [This I can manage by initalising the input
> buffer.]


Just print out the path name and tell the user to enter the filename on
the following line. If they want to specify another path then they can
enter a full pathname instead of just a filename. You can then read in
the line and if a directory separator is present then you know that a
new path has been specified. Otherwise you can just concatenate your
preselected path with the filename input.

This method is portable and is not of much inconvenience to users.

> 2. If some file is located in another location, the user should be
> able to delete parts of the path, so the final result can be .e.g. /
> some/other/path/file.txt


Standard C does not guarantee this. Your system might however provide
this capability, but you'll likely have to use low-level terminal
functions.

> I have looked into gnu readline - but it does not seem it provides
> this functionality. Any tips gretly appreciated.


IMO losing portability for a small cosmetic UI feature is not worth it.
However YMMV. For more details post to a system specific group like
<news:comp.unix.programmer> or
<news:comp.os.ms-windows.programmer.win32>.



All times are GMT. The time now is 10:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.