|
From: James R. V. Z. <jr...@co...> - 2009-01-27 01:18:46
|
I propose the patch below, which adds a function that returns the
string that defines a specified user defined function.
I would like to be able to put the definition of a function into a
label, like this:
gnuplot> fun(x)=1-x**2/2
gnuplot> set label definition('fun') at graph .05,.95
gnuplot> plot [-1:1] fun(x),cos(x)
This becomes useful if the function definition is long, and/or you
want to plot it for several values of a second variable. Without this
function, one can of course use "show functions" and cut and paste the
text into a separate "set label" command. However, that has to be
repeated each time you change the function definition - i.e. a manual
action to keep the function definition and the label aligned.
More basically, if you have many functions, this lets you see the
definition of one without having it scrolled off the screen by a lot
of others:
gnuplot> print definition('fun')
fun(x)=1-x**2/2
The name is the simplest one I came up with. Maybe "definitionof"
would be more descriptive.
I included basic documentation in the patch. If it's accepted, I
would add some cross-references.
- Jim Van Zandt
Index: src/internal.h
===================================================================
RCS file: /cvsroot/gnuplot/gnuplot/src/internal.h,v
retrieving revision 1.19
diff -u -r1.19 internal.h
--- src/internal.h 28 Aug 2007 06:13:07 -0000 1.19
+++ src/internal.h 26 Jan 2009 03:00:33 -0000
@@ -89,5 +89,6 @@
void f_strftime __PROTO((union argument *x));
void f_strptime __PROTO((union argument *x));
void f_assign __PROTO((union argument *x));
+void f_definition __PROTO((union argument *x));
#endif /* GNUPLOT_INTERNAL_H */
Index: src/internal.c
===================================================================
RCS file: /cvsroot/gnuplot/gnuplot/src/internal.c,v
retrieving revision 1.51
diff -u -r1.51 internal.c
--- src/internal.c 25 Sep 2008 18:33:50 -0000 1.51
+++ src/internal.c 26 Jan 2009 03:00:33 -0000
@@ -1521,3 +1521,33 @@
}
}
+/* Return the string defining the specified user function
+ * JRV Jan 2008
+ */
+void
+f_definition(union argument *arg)
+{
+ struct value a, result;
+ struct udft_entry *udf = first_udf;
+
+ pop(&a); /* pop the argument */
+
+ /* Make sure we got a string */
+ if (a.type != STRING)
+ int_error(NO_CARET,"definition requires a string parameter");
+
+ while (udf) {
+ /* find a function with that name */
+ if (udf->definition && !strcmp(a.v.string_val, udf->udf_name)) {
+ /* push its definition string onto the stack */
+ push(Gstring(&result, udf->definition));
+ goto done;
+ }
+ udf = udf->next_udf;
+ }
+ int_error(NO_CARET,"undefined user function: %s",a.v.string_val);
+ done:
+ gpfree_string(&a);
+}
+
+
Index: src/eval.c
===================================================================
RCS file: /cvsroot/gnuplot/gnuplot/src/eval.c,v
retrieving revision 1.71
diff -u -r1.71 eval.c
--- src/eval.c 2 Sep 2008 21:17:01 -0000 1.71
+++ src/eval.c 26 Jan 2009 03:00:34 -0000
@@ -191,6 +191,7 @@
{"stringcolumn", f_stringcolumn}, /* for using specs */
{"strcol", f_stringcolumn}, /* shorthand form */
+ {"definition", f_definition}, /* for string variables only */
{"sprintf", f_sprintf}, /* for string variables only */
{"gprintf", f_gprintf}, /* for string variables only */
{"strlen", f_strlen}, /* for string variables only */
Index: docs/gnuplot.doc
===================================================================
RCS file: /cvsroot/gnuplot/gnuplot/docs/gnuplot.doc,v
retrieving revision 1.553
diff -u -r1.553 gnuplot.doc
--- docs/gnuplot.doc 4 Jan 2009 05:47:14 -0000 1.553
+++ docs/gnuplot.doc 26 Jan 2009 03:00:39 -0000
@@ -1082,6 +1082,16 @@
%c c l .
%Function@Arguments@Returns
%_
+4 definition
+?expressions functions definition
+?functions definition
+?definition
+=definition
+#definition("name") & string & string defining the user function named "name" \\
+%definition("name")@string@string defining the user function named "name"
+ `definition("name")` returns the string defining the user function named
+ "name". For example, `foo(x)=x+3; definition("foo")` returns the
+ string "foo(x)=x+3".
4 gprintf
?expressions functions gprintf
?functions gprintf
|
|
From: Ethan A M. <merritt@u.washington.edu> - 2009-01-27 06:54:31
|
On Monday 26 January 2009, James R. Van Zandt wrote:
>
> I propose the patch below, which adds a function that returns the
> string that defines a specified user defined function.
>
> I would like to be able to put the definition of a function into a
> label, like this:
>
> gnuplot> fun(x)=1-x**2/2
> gnuplot> set label definition('fun') at graph .05,.95
> gnuplot> plot [-1:1] fun(x),cos(x)
I agree that the functionality would be nice.
A couple of thoughts:
It is already possible to do essentially the same thing by reversing
the order of operations and using the recently added evaluate() function:
gnuplot> def = "f(x) = 1-x**2/2"
gnuplot> evaluate(def)
gnuplot> plot [-1:1] cos(x), f(x) title def
Another possibility is to make the function definitions visible as
string variables directly, exactly as the existing variables are.
gnuplot> g(x,y) = x**2 + y**3
gnuplot> show variable GPFUN
Variables beginning with GPFUN:
GPFUN_g = "g(x,y) = x**2 + y**3"
gnuplot> set label GPFUN_g at graph .05, .95
The three approaches are each a little different, although I think you
end up with equal functionality in each case. Maybe it wouldn't hurt
to have all three available?
> This becomes useful if the function definition is long, and/or you
> want to plot it for several values of a second variable. Without this
> function, one can of course use "show functions" and cut and paste the
> text into a separate "set label" command. However, that has to be
> repeated each time you change the function definition - i.e. a manual
> action to keep the function definition and the label aligned.
>
> More basically, if you have many functions, this lets you see the
> definition of one without having it scrolled off the screen by a lot
> of others:
>
> gnuplot> print definition('fun')
> fun(x)=1-x**2/2
That seems like a separate question, and could be fixed by allowing you
to request a specific subset of the user-defined functions as we already
do for user-defined variables.
gnuplot> f(x) = 4
gnuplot> show fun g
Variables beginning with GPFUN:
g(x,y) = x**2 + y**3
goo(n) = n**n
> The name is the simplest one I came up with. Maybe "definitionof"
> would be more descriptive.
>
> I included basic documentation in the patch. If it's accepted, I
> would add some cross-references.
>
> - Jim Van Zandt
>
>
> Index: src/internal.h
> ===================================================================
> RCS file: /cvsroot/gnuplot/gnuplot/src/internal.h,v
> retrieving revision 1.19
> diff -u -r1.19 internal.h
> --- src/internal.h 28 Aug 2007 06:13:07 -0000 1.19
> +++ src/internal.h 26 Jan 2009 03:00:33 -0000
> @@ -89,5 +89,6 @@
> void f_strftime __PROTO((union argument *x));
> void f_strptime __PROTO((union argument *x));
> void f_assign __PROTO((union argument *x));
> +void f_definition __PROTO((union argument *x));
>
> #endif /* GNUPLOT_INTERNAL_H */
> Index: src/internal.c
> ===================================================================
> RCS file: /cvsroot/gnuplot/gnuplot/src/internal.c,v
> retrieving revision 1.51
> diff -u -r1.51 internal.c
> --- src/internal.c 25 Sep 2008 18:33:50 -0000 1.51
> +++ src/internal.c 26 Jan 2009 03:00:33 -0000
> @@ -1521,3 +1521,33 @@
> }
> }
>
> +/* Return the string defining the specified user function
> + * JRV Jan 2008
> + */
> +void
> +f_definition(union argument *arg)
> +{
> + struct value a, result;
> + struct udft_entry *udf = first_udf;
> +
> + pop(&a); /* pop the argument */
> +
> + /* Make sure we got a string */
> + if (a.type != STRING)
> + int_error(NO_CARET,"definition requires a string parameter");
> +
> + while (udf) {
> + /* find a function with that name */
> + if (udf->definition && !strcmp(a.v.string_val, udf->udf_name)) {
> + /* push its definition string onto the stack */
> + push(Gstring(&result, udf->definition));
> + goto done;
> + }
> + udf = udf->next_udf;
> + }
> + int_error(NO_CARET,"undefined user function: %s",a.v.string_val);
> + done:
> + gpfree_string(&a);
> +}
> +
> +
> Index: src/eval.c
> ===================================================================
> RCS file: /cvsroot/gnuplot/gnuplot/src/eval.c,v
> retrieving revision 1.71
> diff -u -r1.71 eval.c
> --- src/eval.c 2 Sep 2008 21:17:01 -0000 1.71
> +++ src/eval.c 26 Jan 2009 03:00:34 -0000
> @@ -191,6 +191,7 @@
>
> {"stringcolumn", f_stringcolumn}, /* for using specs */
> {"strcol", f_stringcolumn}, /* shorthand form */
> + {"definition", f_definition}, /* for string variables only */
> {"sprintf", f_sprintf}, /* for string variables only */
> {"gprintf", f_gprintf}, /* for string variables only */
> {"strlen", f_strlen}, /* for string variables only */
> Index: docs/gnuplot.doc
> ===================================================================
> RCS file: /cvsroot/gnuplot/gnuplot/docs/gnuplot.doc,v
> retrieving revision 1.553
> diff -u -r1.553 gnuplot.doc
> --- docs/gnuplot.doc 4 Jan 2009 05:47:14 -0000 1.553
> +++ docs/gnuplot.doc 26 Jan 2009 03:00:39 -0000
> @@ -1082,6 +1082,16 @@
> %c c l .
> %Function@Arguments@Returns
> %_
> +4 definition
> +?expressions functions definition
> +?functions definition
> +?definition
> +=definition
> +#definition("name") & string & string defining the user function named "name" \\
> +%definition("name")@string@string defining the user function named "name"
> + `definition("name")` returns the string defining the user function named
> + "name". For example, `foo(x)=x+3; definition("foo")` returns the
> + string "foo(x)=x+3".
> 4 gprintf
> ?expressions functions gprintf
> ?functions gprintf
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by:
> SourcForge Community
> SourceForge wants to tell your story.
> http://p.sf.net/sfu/sf-spreadtheword
> _______________________________________________
> gnuplot-beta mailing list
> gnu...@li...
> https://lists.sourceforge.net/lists/listinfo/gnuplot-beta
>
--
Ethan A Merritt
|
|
From: James R. V. Z. <jr...@co...> - 2009-01-29 03:31:58
|
Ethan A Merritt <merritt@u.washington.edu> writes:
>On Monday 26 January 2009, James R. Van Zandt wrote:
>>
>> I propose the patch below, which adds a function that returns the
>> string that defines a specified user defined function.
...
>A couple of thoughts:
>
>It is already possible to do essentially the same thing by reversing
>the order of operations and using the recently added evaluate() function:
>
> gnuplot> def = "f(x) = 1-x**2/2"
> gnuplot> evaluate(def)
> gnuplot> plot [-1:1] cos(x), f(x) title def
This seems awkward to me.
>Another possibility is to make the function definitions visible as
>string variables directly, exactly as the existing variables are.
>
> gnuplot> g(x,y) = x**2 + y**3
> gnuplot> show variable GPFUN
> Variables beginning with GPFUN:
> GPFUN_g = "g(x,y) = x**2 + y**3"
> gnuplot> set label GPFUN_g at graph .05, .95
I think this is about as handy as the definition() function.
It would even let us retire the "show functions" command.
It would be easy to create an associated string variable when
recording the definition of a function.
A diabolical user could goof this up:
gnuplot> g(x,y) = x**2 + y**3
gnuplot> GPFUN_g = 42
gnuplot> set label GPFUN_g at graph .05, .95
but I don't suppose that is worth worrying about.
The next step might be for the expression parser to look for functions
via the string variable. Probably not worth the trouble.
By the way, you slipped in a useful generalization of the "show
variables" command. I would extend it to allow globbing:
gnuplot> show variable xy*z
xyaz = 13
xybz = 14
xyzz = "forty two"
- Jim Van Zandt
|
|
From: Ethan A M. <merritt@u.washington.edu> - 2009-01-29 04:33:47
|
On Wednesday 28 January 2009, James R. Van Zandt wrote:
>
> Ethan A Merritt <merritt@u.washington.edu> writes:
> >On Monday 26 January 2009, James R. Van Zandt wrote:
> >>
> >> I propose the patch below, which adds a function that returns the
> >> string that defines a specified user defined function.
> ...
> >A couple of thoughts:
> >
> >It is already possible to do essentially the same thing by reversing
> >the order of operations and using the recently added evaluate() function:
> >
> > gnuplot> def = "f(x) = 1-x**2/2"
> > gnuplot> evaluate(def)
> > gnuplot> plot [-1:1] cos(x), f(x) title def
>
> This seems awkward to me.
I agree. I was just it pointing out as something that it is already possible.
>
> >Another possibility is to make the function definitions visible as
> >string variables directly, exactly as the existing variables are.
> >
> > gnuplot> g(x,y) = x**2 + y**3
> > gnuplot> show variable GPFUN
> > Variables beginning with GPFUN:
> > GPFUN_g = "g(x,y) = x**2 + y**3"
> > gnuplot> set label GPFUN_g at graph .05, .95
>
> I think this is about as handy as the definition() function.
> It would even let us retire the "show functions" command.
>
> It would be easy to create an associated string variable when
> recording the definition of a function.
>
> A diabolical user could goof this up:
>
> gnuplot> g(x,y) = x**2 + y**3
> gnuplot> GPFUN_g = 42
> gnuplot> set label GPFUN_g at graph .05, .95
>
> but I don't suppose that is worth worrying about.
We already dis-allow setting variable names GPVAL_* and MOUSE_*
from the command line. This would be similarly protected.
> The next step might be for the expression parser to look for functions
> via the string variable. Probably not worth the trouble.
>
> By the way, you slipped in a useful generalization of the "show
> variables" command.
It's been there for a while, but I only discovered it recently.
> I would extend it to allow globbing:
>
> gnuplot> show variable xy*z
> xyaz = 13
> xybz = 14
> xyzz = "forty two"
> - Jim Van Zandt
Ethan
|
|
From: Ethan A M. <merritt@u.washington.edu> - 2009-01-29 07:00:32
|
On Wednesday 28 January 2009, Ethan A Merritt wrote:
> On Wednesday 28 January 2009, James R. Van Zandt wrote:
> >
> > Ethan A Merritt <merritt@u.washington.edu> writes:
> > >On Monday 26 January 2009, James R. Van Zandt wrote:
> > >>
> > >> I propose the patch below, which adds a function that returns the
> > >> string that defines a specified user defined function.
> > ...
> > >A couple of thoughts:
> > >
> > >It is already possible to do essentially the same thing by reversing
> > >the order of operations and using the recently added evaluate() function:
> > >
> > > gnuplot> def = "f(x) = 1-x**2/2"
> > > gnuplot> evaluate(def)
> > > gnuplot> plot [-1:1] cos(x), f(x) title def
> >
> > This seems awkward to me.
>
> I agree. I was just it pointing out as something that it is already possible.
Bleah. Make that "I was just pointing it out as something..."
> >
> > >Another possibility is to make the function definitions visible as
> > >string variables directly, exactly as the existing variables are.
> > >
> > > gnuplot> g(x,y) = x**2 + y**3
> > > gnuplot> show variable GPFUN
> > > Variables beginning with GPFUN:
> > > GPFUN_g = "g(x,y) = x**2 + y**3"
> > > gnuplot> set label GPFUN_g at graph .05, .95
> >
> > I think this is about as handy as the definition() function.
> > It would even let us retire the "show functions" command.
> >
> > It would be easy to create an associated string variable when
> > recording the definition of a function.
Right. Here you go:
--- gnuplot/src/command.c 2008-12-12 19:11:34.000000000 -0800
+++ gnuplot-cvs/src/command.c 2009-01-28 22:45:09.000000000 -0800
@@ -530,6 +530,15 @@ define()
memcpy(c_dummy_var, save_dummy, sizeof(save_dummy));
m_capture(&(udf->definition), start_token, c_token - 1);
dummy_func = NULL; /* dont let anyone else use our workspace */
+
+ /* Save function definition in a user-accessible variable */
+ if (1) {
+ char *tmpnam = gp_alloc(6+strlen(udf->udf_name), "varname");
+ strcpy(tmpnam, "GPFUN_");
+ strcat(tmpnam, udf->udf_name);
+ fill_gpval_string(tmpnam, udf->definition);
+ free(tmpnam);
+ }
} else {
/* variable ! */
char *varname = gp_input_line + token[c_token].start_index;
|
|
From: Petr M. <mi...@ph...> - 2009-01-31 10:16:02
|
> I propose the patch below, which adds a function that returns the
> string that defines a specified user defined function.
I see it gives the following:
GPFUN_f = "f(x,y)=x+y+20"
GPFUN_g = "g(x,y)=x*y"
I wonder how I can get the definition part only, i.e. "x+y+20" or "x*y".
Maybe you could add another variable
GPFUN__f = "x+y+20"
?
I though there are some "string" functions that could allow this, but it
seems we don't have "string index" or "string split" functions.
Maybe it would be also useful to have
strindex("hello world", "llo") => 3
strsplit("hello=world", "=", 0) => "hello"
strsplit("hello=world", "=", 1) => "="
strsplit("hello=world", "=", 2) => "world"
---
PM
|
|
From: James R. V. Z. <jr...@co...> - 2009-01-31 22:13:11
|
Petr Mikulik <mi...@ph...> writes:
> MIME-Version: 1.0
> Cc: gnu...@li...
> Content-Type: text/plain; charset="us-ascii"
>
> > I propose the patch below, which adds a function that returns the
> > string that defines a specified user defined function.
I have committed the code that Ethan Merritt posted. I may commit my
original implementation of definition() as well, if only because I can
touch type that faster (no upper case :-).
> I see it gives the following:
> GPFUN_f = "f(x,y)=x+y+20"
> GPFUN_g = "g(x,y)=x*y"
>
> I wonder how I can get the definition part only, i.e. "x+y+20" or "x*y".
>
> Maybe you could add another variable
> GPFUN__f = "x+y+20"
Not very mnemonic.
> I thought there are some "string" functions that could allow this,
> but it seems we don't have "string index" or "string split"
> functions. Maybe it would be also useful to have
> strindex("hello world", "llo") => 3
> strsplit("hello=world", "=", 0) => "hello"
> strsplit("hello=world", "=", 1) => "="
> strsplit("hello=world", "=", 2) => "world"
I agree. Yours are inspired by awk, I think. We could argue whether
indexes should start with 0 instead of 1. I suggest the second
argument of strsplit should be like the second argument of strspn in
C: tokens are separated by one or more characters from that string,
and the separators found are returned. E.g.
strsplit("hello, world", " ;," , 1) => ", "
strsplit("hello, world", " ;," , 2) => "world"
strsplit("hello, world", "," , 1) => ","
strsplit("hello, world", "," , 2) => " world"
To go along with strindex, I would also like to see:
substr(string, index, length)
- Jim Van Zandt
|
|
From: Petr M. <mi...@ph...> - 2009-02-01 23:05:06
|
> > > I propose the patch below, which adds a function that returns the
> > > string that defines a specified user defined function.
>
> I have committed the code that Ethan Merritt posted. I may commit my
> original implementation of definition() as well, if only because I can
> touch type that faster (no upper case :-).
Then it could be like
definition('f', 0)
=> a*x+y
definition('f', 1)
=> f(x)=a*x+y
> > I thought there are some "string" functions that could allow this,
> > but it seems we don't have "string index" or "string split"
> > functions. Maybe it would be also useful to have
> > strindex("hello world", "llo") => 3
>
> tokens are separated by one or more characters from that string,
> and the separators found are returned. E.g.
> strsplit("hello, world", " ;," , 1) => ", "
> strsplit("hello, world", " ;," , 2) => "world"
> strsplit("hello, world", "," , 1) => ","
> strsplit("hello, world", "," , 2) => " world"
Yes, by awk. I propose to start with 1 as it is currently used in
print word("a b c d e", 1)
=> "a"
print substr("hello", 1,99)
=> "hello"
I think it would be sufficient not to return separators
strsplit("hello, world, ahoj, svete", ",", 1) => "hello"
strsplit("hello, world, ahoj, svete", ",", 2) => "hello"
strsplit("hello, world, ahoj, svete", ",", 3) => "ahoj"
strsplit("hello, world, ahoj, svete", ",", 4) => "ahoj"
and this would also need
strsplitn("hello, world, ahoj, svete", ",") => 4
> To go along with strindex, I would also like to see:
> substr(string, index, length)
This exists, see above.
---
PM
|
|
From: Ethan M. <merritt@u.washington.edu> - 2009-02-02 17:13:26
|
On Saturday 31 January 2009 02:15:49 Petr Mikulik wrote:
> > I propose the patch below, which adds a function that returns the
> > string that defines a specified user defined function.
>
> I see it gives the following:
> GPFUN_f = "f(x,y)=x+y+20"
> GPFUN_g = "g(x,y)=x*y"
>
> I wonder how I can get the definition part only, i.e. "x+y+20" or "x*y".
> Maybe you could add another variable
> GPFUN__f = "x+y+20"
> ?
>
> I though there are some "string" functions that could allow this, but it
> seems we don't have "string index" or "string split" functions.
We do. It is called strstrt(haystack,needle):
gnuplot> show var GPFUN
GPFUN_f = "f(a,b) = a**2 + sqrt(b*a)"
gnuplot> print strstrt(GPFUN_f,"=")
8
gnuplot> print GPFUN_f[ strstrt(GPFUN_f,"=") : * ]
= a**2 + sqrt(b*a)
gnuplot> print GPFUN_f[ strstrt(GPFUN_f,"=")+1 : * ]
a**2 + sqrt(b*a)
gnuplot> body = GPFUN_f[strstrt(GPFUN_f,"=")+1:*]
gnuplot> print body
a**2 + sqrt(b*a)
>
> Maybe it would be also useful to have
> strindex("hello world", "llo") => 3
> strsplit("hello=world", "=", 0) => "hello"
> strsplit("hello=world", "=", 1) => "="
> strsplit("hello=world", "=", 2) => "world"
>
> ---
> PM
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by:
> SourcForge Community
> SourceForge wants to tell your story.
> http://p.sf.net/sfu/sf-spreadtheword
> _______________________________________________
> gnuplot-beta mailing list
> gnu...@li...
> https://lists.sourceforge.net/lists/listinfo/gnuplot-beta
>
--
Ethan A Merritt
Biomolecular Structure Center
University of Washington, Seattle 98195-7742
|
|
From: Petr M. <mi...@ph...> - 2009-02-02 22:50:56
|
> > seems we don't have "string index" or "string split" functions. > > We do. It is called strstrt(haystack,needle): > > gnuplot> show var GPFUN > GPFUN_f = "f(a,b) = a**2 + sqrt(b*a)" > > gnuplot> print strstrt(GPFUN_f,"=") > 8 > gnuplot> print GPFUN_f[ strstrt(GPFUN_f,"=") : * ] Interesting, I have never used this. How can I get the "reversed" version of it? Just today I needed to extract the last part of the directory, e.g. from /tmp/bla/now I needed to get "now". Finally I've got it by calling shall's `basename` but the reversed version of "strstrt" would help a lot. Further I have noticed `pwd` command from shell is needed to set a variable for the current working directory because gnuplot> pwd just prints it to screen, but I cannot do a=pwd or a=eval pwd Would an automatic variable GPVAL_PWD be the only chance for a portable solution? --- PM |
|
From: Ethan M. <merritt@u.washington.edu> - 2009-02-02 22:19:06
|
On Monday 02 February 2009 13:52:18 you wrote:
> > > seems we don't have "string index" or "string split" functions.
> >
> > We do. It is called strstrt(haystack,needle):
> >
> > gnuplot> show var GPFUN
> > GPFUN_f = "f(a,b) = a**2 + sqrt(b*a)"
> >
> > gnuplot> print strstrt(GPFUN_f,"=")
> > 8
> > gnuplot> print GPFUN_f[ strstrt(GPFUN_f,"=") : * ]
>
> Interesting, I have never used this.
>
> How can I get the "reversed" version of it? Just today I needed to extract
> the last part of the directory, e.g. from
> /tmp/bla/now
> I needed to get "now". Finally I've got it by calling shall's `basename` but
> the reversed version of "strstrt" would help a lot.
Gnuplot's strstrt() function is just a wrapper for the C library routine strstr().
In C you could do:
char *piece = "/some/long/path/name";
char *end;
while (end = strstr( piece, "/" ))
do {piece = end+1;}
Unfortunately, gnuplot doesn't support while/do so I think you are out of luck.
> Further I have noticed `pwd` command from shell is needed to set a variable
> for the current working directory because
> gnuplot> pwd
> just prints it to screen, but I cannot do
> a=pwd
> or a=eval pwd
The "pwd" command from the shell is needed in any case. It is only a matter
of whether it is called directly or called via the internal routine pwd_command().
I would do
a = system("pwd")
> Would an automatic variable GPVAL_PWD be the only chance for a portable
> solution?
I don't know. I don't think that the concept of "current working directory"
is universally portable even apart from how you would issue the command.
In VMS, for example, the current directory name and the current disk are
separate things. Just asking for the current directory would not tell you
what disk you would be writing to.
--
Ethan A Merritt
|
|
From: Tait <gnu...@t4...> - 2009-02-03 01:08:31
|
> > Further I have noticed `pwd` command from shell is needed to set a variable
> > for the current working directory because
> > gnuplot> pwd
> > just prints it to screen, but I cannot do
> > a=pwd
> > or a=eval pwd
>
> The "pwd" command from the shell is needed in any case. It is only a matter
> of whether it is called directly or called via the internal routine pwd_command().
> I would do
> a = system("pwd")
gnuplot> a=system("pwd")
warning: system evaluation not supported by MS-Windows 32 bit
gnuplot> pwd
C:\Program Files\gnuplot\bin
gnuplot>
So there is a need for something portable, and it's not system("pwd").
> I don't know. I don't think that the concept of "current working directory"
> is universally portable even apart from how you would issue the command.
> In VMS, for example, the current directory name and the current disk are
> separate things. Just asking for the current directory would not tell you
> what disk you would be writing to.
In Windows, the drive is given as part of pwd, so it would make sense,
I think, to emulate the same behavior in VMS and return the full path,
diskname$foo:[path.to.dir].
Tait
|
|
From: Ethan M. <merritt@u.washington.edu> - 2009-02-03 03:23:31
|
On Monday 02 February 2009 16:50:55 Tait wrote:
> > > Further I have noticed `pwd` command from shell is needed to set a variable
> > > for the current working directory because
> > > gnuplot> pwd
> > > just prints it to screen, but I cannot do
> > > a=pwd
> > > or a=eval pwd
> >
> > The "pwd" command from the shell is needed in any case. It is only a matter
> > of whether it is called directly or called via the internal routine pwd_command().
> > I would do
> > a = system("pwd")
>
> gnuplot> a=system("pwd")
> warning: system evaluation not supported by MS-Windows 32 bit
> gnuplot> pwd
> C:\Program Files\gnuplot\bin
> gnuplot>
>
> So there is a need for something portable, and it's not system("pwd").
Huh. I did not know that.
So the windows code does not support the system() command?
Can that be fixed?
It seems to me that's a more serious issue than the details
of how best to query the current working directory.
> > I don't know. I don't think that the concept of "current working directory"
> > is universally portable even apart from how you would issue the command.
> > In VMS, for example, the current directory name and the current disk are
> > separate things. Just asking for the current directory would not tell you
> > what disk you would be writing to.
>
> In Windows, the drive is given as part of pwd, so it would make sense,
> I think, to emulate the same behavior in VMS and return the full path,
> diskname$foo:[path.to.dir].
I can see that the configuration tool does set HAVE_GETCWD for VMS, but
I don't currently have a VMS system to check on what exactly it returns.
--
Ethan A Merritt
|
|
From: Petr M. <mi...@ph...> - 2009-02-03 08:18:57
|
> > > The "pwd" command from the shell is needed in any case. It is only a matter
> > > of whether it is called directly or called via the internal routine pwd_command().
> > > I would do
> > > a = system("pwd")
> >
> > gnuplot> a=system("pwd")
> > warning: system evaluation not supported by MS-Windows 32 bit
>
> > gnuplot> pwd
> > C:\Program Files\gnuplot\bin
> > gnuplot>
> >
> > So there is a need for something portable, and it's not system("pwd").
>
> Huh. I did not know that.
> So the windows code does not support the system() command?
Well, this depends on executable:
wgnuplot.exe does not support pipes and system() and !command
wgnuplot_pipes.exe supports them (but keeps an attached console window)
---
PM
|
|
From: Petr M. <mi...@ph...> - 2009-02-03 22:39:57
|
> Gnuplot's strstrt() function is just a wrapper for the C library routine strstr().
> In C you could do:
>
> char *piece = "/some/long/path/name";
> char *end;
>
> while (end = strstr( piece, "/" ))
> do {piece = end+1;}
>
> Unfortunately, gnuplot doesn't support while/do so I think you are out of luck.
We can add new function strstrtrev("piece", "/") that would return the last
occurence of the substring.
Would it be useful to have function for handling file names
strfileparts('/home/me/bla.dat',n)
which would return path (for n=1), name (n=2) and extension (n=3)?
That would be quite handy especially in the "plot for" loop.
> Would an automatic variable GPVAL_PWD be the only chance for a portable
> solution?
I have added it.
---
PM
|
|
From: Ethan M. <merritt@u.washington.edu> - 2009-02-03 23:21:38
|
On Tuesday 03 February 2009 14:39:43 Petr Mikulik wrote:
> > Gnuplot's strstrt() function is just a wrapper for the C library routine strstr().
> > In C you could do:
> >
> > char *piece = "/some/long/path/name";
> > char *end;
> >
> > while (end = strstr( piece, "/" ))
> > do {piece = end+1;}
> >
> > Unfortunately, gnuplot doesn't support while/do so I think you are out of luck.
>
> We can add new function strstrtrev("piece", "/") that would return the last
> occurence of the substring.
>
> Would it be useful to have function for handling file names
> strfileparts('/home/me/bla.dat',n)
> which would return path (for n=1), name (n=2) and extension (n=3)?
These sound very specialized. Why do we need such file-oriented string
operations, that even the standard C library does not support?
I think I'd rather go ahead and add support for a 'while' statement.
It would be limited to a single line, just as the current 'if' statement,
but it should be possible to do.
> That would be quite handy especially in the "plot for" loop.
Could you give an example showing why this would be useful?
--
Ethan A Merritt
|
|
From: Ethan M. <merritt@u.washington.edu> - 2009-02-04 00:25:06
|
On Tuesday 03 February 2009 14:39:43 Petr Mikulik wrote:
>
> > Would an automatic variable GPVAL_PWD be the only chance for a portable
> > solution?
>
> I have added it.
2009-02-03 Petr Mikulik <mi...@ph...>
* src/eval.c (update_gpval_variables) src/command.c (changedir_command)
New automatic variable GPVAL_PWD for current working directory.
^^^ ^ ^ ^
Wouldn't that better be GPVAL_cwd or GPVAL_CWD?
--
Ethan A Merritt
|
|
From: Petr M. <mi...@ph...> - 2009-02-04 09:06:21
|
> > > Would an automatic variable GPVAL_PWD be the only chance for a portable > > > solution? > > > > I have added it. > > 2009-02-03 Petr Mikulik <mi...@ph...> > > * src/eval.c (update_gpval_variables) src/command.c (changedir_command) > New automatic variable GPVAL_PWD for current working directory. > ^^^ ^ ^ ^ > > Wouldn't that better be GPVAL_cwd or GPVAL_CWD? I hesitated as well, but then I tried bash: set | grep -i pwd where I can see variables PWD OLDPWD MC_PWD MC_PWD_FILE and thus I have decided for GPVAL_PWD as well. --- PM |