Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-09 Thread oliver
On Wed, Jun 08, 2011 at 08:35:34PM -0400, Simon Urbanek wrote:
 
 On Jun 8, 2011, at 8:32 PM, oliver wrote:
 
  On Thu, Jun 09, 2011 at 02:17:31AM +0200, oliver wrote:
  [...]
  OK, I looked at this now.
  
  LENGTH() checks the length of the vector.
  
  Good to know this.
  
  So the problem of a vector of length 0 can be with any arguments of type 
  SEXP,
  hence I will need to check ANY arg on it's length.
  
  This is vital to stability under any situation.
  
  Thanks for this valuable hint!
  
  I will add checks for all my SEXP-args.
  [...]
  
  Hey, LENGTH() does not work with String-vectors! :(
  
 
 Of course it does ...
 
 

It does not so on my R 2.10.1 installation.


In the R-Shell I get:

  ==
   length(c())
  [1] 0
   
  ==

So c() is vec of length 0.

When I feed my readjpeg() with c() as filename arg,

testing with:

  if( LENGTH( filename_sexp )  1 )
  {
error(LENGTH( filename_sexp )  1);
//error(filename can't be vector of length 0);
  }
  else
  {
error(LENGTH( filename_sexp ) is not  1);
  }



I got:
  Error in readjpeg(filename = c()) : LENGTH( filename_sexp ) is not  1


You can explain why?


Ciao,
   Oliver

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-09 Thread Duncan Murdoch

On 11-06-09 7:27 AM, oliver wrote:

On Wed, Jun 08, 2011 at 08:35:34PM -0400, Simon Urbanek wrote:


On Jun 8, 2011, at 8:32 PM, oliver wrote:


On Thu, Jun 09, 2011 at 02:17:31AM +0200, oliver wrote:
[...]

OK, I looked at this now.

LENGTH() checks the length of the vector.

Good to know this.

So the problem of a vector of length 0 can be with any arguments of type SEXP,
hence I will need to check ANY arg on it's length.

This is vital to stability under any situation.

Thanks for this valuable hint!

I will add checks for all my SEXP-args.

[...]

Hey, LENGTH() does not work with String-vectors! :(



Of course it does ...




It does not so on my R 2.10.1 installation.


In the R-Shell I get:

   ==
 length(c())
   [1] 0
   
   ==

So c() is vec of length 0.

When I feed my readjpeg() with c() as filename arg,

testing with:

   if( LENGTH( filename_sexp )  1 )
   {
 error(LENGTH( filename_sexp )  1);
 //error(filename can't be vector of length 0);
   }
   else
   {
 error(LENGTH( filename_sexp ) is not  1);
   }



I got:
   Error in readjpeg(filename = c()) : LENGTH( filename_sexp ) is not  1


You can explain why?


c() doesn't create a STRSXP, it is NULL, which is a NILSXP.  LENGTH() 
doesn't work on that object.  (I'd recommend using length() rather than 
LENGTH(); it's a function, not a macro, and it does give the expected 
answer.)


Duncan Murdoch

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-09 Thread oliver
On Thu, Jun 09, 2011 at 07:43:20AM -0400, Duncan Murdoch wrote:
 On 11-06-09 7:27 AM, oliver wrote:
 On Wed, Jun 08, 2011 at 08:35:34PM -0400, Simon Urbanek wrote:
 
 On Jun 8, 2011, at 8:32 PM, oliver wrote:
 
 On Thu, Jun 09, 2011 at 02:17:31AM +0200, oliver wrote:
 [...]
 OK, I looked at this now.
 
 LENGTH() checks the length of the vector.
 
 Good to know this.
 
 So the problem of a vector of length 0 can be with any arguments of type 
 SEXP,
 hence I will need to check ANY arg on it's length.
 
 This is vital to stability under any situation.
 
 Thanks for this valuable hint!
 
 I will add checks for all my SEXP-args.
 [...]
 
 Hey, LENGTH() does not work with String-vectors! :(
 
 
 Of course it does ...
 
 
 
 It does not so on my R 2.10.1 installation.
 
 
 In the R-Shell I get:
 
==
  length(c())
[1] 0

==
 
 So c() is vec of length 0.
 
 When I feed my readjpeg() with c() as filename arg,
 
 testing with:
 
if( LENGTH( filename_sexp )  1 )
{
  error(LENGTH( filename_sexp )  1);
  //error(filename can't be vector of length 0);
}
else
{
  error(LENGTH( filename_sexp ) is not  1);
}
 
 
 
 I got:
Error in readjpeg(filename = c()) : LENGTH( filename_sexp ) is not  1
 
 
 You can explain why?
 
 c() doesn't create a STRSXP, it is NULL, which is a NILSXP.
 LENGTH() doesn't work on that object.  (I'd recommend using length()
 rather than LENGTH(); it's a function, not a macro, and it does give
 the expected answer.)
[...]

Interestingly, c() as value for an integer value
can be testes correctly with LENGTH().

So the question arises: is c() always creating NILSXP,
or is it NILSXP only for STRSXP, and 0 otherwise?

NILSXP, as it might be close to a NULL might be interpreted as 0
length by LENGTH...

Nevertheless it's a littlebid confusing for people who don't know the
internals of R.

Will try length() soon.

Ciao,
   Oliver



P.S.: To be picky: NOT knowing the internals and nevertheless using the 
available
  functions/macros is called encapsulation in OO or the difference between
  interface and implementation... which is good style of programming.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-09 Thread Duncan Murdoch

On 09/06/2011 9:28 AM, oliver wrote:

On Thu, Jun 09, 2011 at 07:43:20AM -0400, Duncan Murdoch wrote:
  On 11-06-09 7:27 AM, oliver wrote:
  On Wed, Jun 08, 2011 at 08:35:34PM -0400, Simon Urbanek wrote:
  
  On Jun 8, 2011, at 8:32 PM, oliver wrote:
  
  On Thu, Jun 09, 2011 at 02:17:31AM +0200, oliver wrote:
  [...]
  OK, I looked at this now.
  
  LENGTH() checks the length of the vector.
  
  Good to know this.
  
  So the problem of a vector of length 0 can be with any arguments of type 
SEXP,
  hence I will need to check ANY arg on it's length.
  
  This is vital to stability under any situation.
  
  Thanks for this valuable hint!
  
  I will add checks for all my SEXP-args.
  [...]
  
  Hey, LENGTH() does not work with String-vectors! :(
  
  
  Of course it does ...
  
  
  
  It does not so on my R 2.10.1 installation.
  
  
  In the R-Shell I get:
  
  ==
 length(c())
  [1] 0
  
  ==
  
  So c() is vec of length 0.
  
  When I feed my readjpeg() with c() as filename arg,
  
  testing with:
  
  if( LENGTH( filename_sexp )   1 )
  {
error(LENGTH( filename_sexp )   1);
//error(filename can't be vector of length 0);
  }
  else
  {
error(LENGTH( filename_sexp ) is not   1);
  }
  
  
  
  I got:
  Error in readjpeg(filename = c()) : LENGTH( filename_sexp ) is not   1
  
  
  You can explain why?

  c() doesn't create a STRSXP, it is NULL, which is a NILSXP.
  LENGTH() doesn't work on that object.  (I'd recommend using length()
  rather than LENGTH(); it's a function, not a macro, and it does give
  the expected answer.)
[...]

Interestingly, c() as value for an integer value
can be testes correctly with LENGTH().


Presumably you converted it to an INTSXP.  c() is NULL.


So the question arises: is c() always creating NILSXP,
or is it NILSXP only for STRSXP, and 0 otherwise?


That question makes no sense at all.

Duncan Murdoch


NILSXP, as it might be close to a NULL might be interpreted as 0
length by LENGTH...

Nevertheless it's a littlebid confusing for people who don't know the
internals of R.

Will try length() soon.

Ciao,
Oliver



P.S.: To be picky: NOT knowing the internals and nevertheless using the 
available
   functions/macros is called encapsulation in OO or the difference between
   interface and implementation... which is good style of programming.



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-09 Thread oliver
On Thu, Jun 09, 2011 at 10:54:28AM -0400, Duncan Murdoch wrote:
 On 09/06/2011 9:28 AM, oliver wrote:
 On Thu, Jun 09, 2011 at 07:43:20AM -0400, Duncan Murdoch wrote:
   On 11-06-09 7:27 AM, oliver wrote:
   On Wed, Jun 08, 2011 at 08:35:34PM -0400, Simon Urbanek wrote:
   
   On Jun 8, 2011, at 8:32 PM, oliver wrote:
   
   On Thu, Jun 09, 2011 at 02:17:31AM +0200, oliver wrote:
   [...]
   OK, I looked at this now.
   
   LENGTH() checks the length of the vector.
   
   Good to know this.
   
   So the problem of a vector of length 0 can be with any arguments of 
  type SEXP,
   hence I will need to check ANY arg on it's length.
   
   This is vital to stability under any situation.
   
   Thanks for this valuable hint!
   
   I will add checks for all my SEXP-args.
   [...]
   
   Hey, LENGTH() does not work with String-vectors! :(
   
   
   Of course it does ...
   
   
   
   It does not so on my R 2.10.1 installation.
   
   
   In the R-Shell I get:
   
   ==
  length(c())
   [1] 0
   
   ==
   
   So c() is vec of length 0.
   
   When I feed my readjpeg() with c() as filename arg,
   
   testing with:
   
   if( LENGTH( filename_sexp )   1 )
   {
 error(LENGTH( filename_sexp )   1);
 //error(filename can't be vector of length 0);
   }
   else
   {
 error(LENGTH( filename_sexp ) is not   1);
   }
   
   
   
   I got:
   Error in readjpeg(filename = c()) : LENGTH( filename_sexp ) is not  
   1
   
   
   You can explain why?
 
   c() doesn't create a STRSXP, it is NULL, which is a NILSXP.
   LENGTH() doesn't work on that object.  (I'd recommend using length()
   rather than LENGTH(); it's a function, not a macro, and it does give
   the expected answer.)
 [...]
 
 Interestingly, c() as value for an integer value
 can be testes correctly with LENGTH().
 
 Presumably you converted it to an INTSXP.  c() is NULL.
[...]

Ooops, yes. In the R-part. :-)
I did it, because when I don't get an int from the user's call,
I have set it to NA (default in the R-function definition).

I made
  width - as.integer(width)
for easy check inside the C-part, on integer.

(I really only need int, no floating point values.)

I thought this makes the C-code easier at that part.

Or I may check on NA inside the C-part.

How would I do that correctly?

After so much different opinions I read here, I'm rather puzzled on that now.

If there is already a R-extensions-FAQ I could collect the informations
from the mail sof the last days, so that the different opinions and the 
discussion
can result in a some answers...


 
 So the question arises: is c() always creating NILSXP,
 or is it NILSXP only for STRSXP, and 0 otherwise?
 
 That question makes no sense at all.

It make sense for the picky.
As NULL is rather (void*)0  and a pointer is not an int (just can be coerced 
normally) ;)


Ciao,
  Oliver

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-09 Thread Simon Urbanek

On Jun 9, 2011, at 11:15 AM, oliver wrote:

 On Thu, Jun 09, 2011 at 10:54:28AM -0400, Duncan Murdoch wrote:
 On 09/06/2011 9:28 AM, oliver wrote:
 On Thu, Jun 09, 2011 at 07:43:20AM -0400, Duncan Murdoch wrote:
 On 11-06-09 7:27 AM, oliver wrote:
 On Wed, Jun 08, 2011 at 08:35:34PM -0400, Simon Urbanek wrote:
 
 On Jun 8, 2011, at 8:32 PM, oliver wrote:
 
 On Thu, Jun 09, 2011 at 02:17:31AM +0200, oliver wrote:
 [...]
 OK, I looked at this now.
 
 LENGTH() checks the length of the vector.
 
 Good to know this.
 
 So the problem of a vector of length 0 can be with any arguments of 
 type SEXP,
 hence I will need to check ANY arg on it's length.
 
 This is vital to stability under any situation.
 
 Thanks for this valuable hint!
 
 I will add checks for all my SEXP-args.
 [...]
 
 Hey, LENGTH() does not work with String-vectors! :(
 
 
 Of course it does ...
 
 
 
 It does not so on my R 2.10.1 installation.
 
 
 In the R-Shell I get:
 
   ==
  length(c())
   [1] 0
 
   ==
 
 So c() is vec of length 0.
 
 When I feed my readjpeg() with c() as filename arg,
 
 testing with:
 
   if( LENGTH( filename_sexp )   1 )
   {
 error(LENGTH( filename_sexp )   1);
 //error(filename can't be vector of length 0);
   }
   else
   {
 error(LENGTH( filename_sexp ) is not   1);
   }
 
 
 
 I got:
   Error in readjpeg(filename = c()) : LENGTH( filename_sexp ) is not   1
 
 
 You can explain why?
 
 c() doesn't create a STRSXP, it is NULL, which is a NILSXP.
 LENGTH() doesn't work on that object.  (I'd recommend using length()
 rather than LENGTH(); it's a function, not a macro, and it does give
 the expected answer.)
 [...]
 
 Interestingly, c() as value for an integer value
 can be testes correctly with LENGTH().
 
 Presumably you converted it to an INTSXP.  c() is NULL.
 [...]
 
 Ooops, yes. In the R-part. :-)
 I did it, because when I don't get an int from the user's call,
 I have set it to NA (default in the R-function definition).
 
 I made
  width - as.integer(width)
 for easy check inside the C-part, on integer.
 
 (I really only need int, no floating point values.)
 
 I thought this makes the C-code easier at that part.
 
 Or I may check on NA inside the C-part.
 
 How would I do that correctly?
 
 After so much different opinions I read here, I'm rather puzzled on that now.
 
 If there is already a R-extensions-FAQ I could collect the informations
 from the mail sof the last days, so that the different opinions and the 
 discussion
 can result in a some answers...
 

So far you were creating the confusion, the facts are fairly simple if you have 
some clue about C and R. I would like to ask you to, please, read and 
understand C standard and R-exts *before* posting further questions since it's 
pointless if we provide answers that you don't understand. The fact that you 
are impatient (as you said yourself) doesn't entitle you to waste other 
people's time to explain things to you while you are unwilling to take the time 
yourself. If you have serious question beyond what is documented, feel free to 
ask, but make sure you have spent the time to think about it before you do so.


 
 So the question arises: is c() always creating NILSXP,
 or is it NILSXP only for STRSXP, and 0 otherwise?
 
 That question makes no sense at all.
 
 It make sense for the picky.
 As NULL is rather (void*)0  and a pointer is not an int (just can be coerced 
 normally) ;)
 

It's not - you're confusing NULL in R with NULL in C.
Both NILSXP and STRSXP are SEXP types - and obviously distinct ones. R will 
never return a null pointer for a SEXP.

Cheers,
Simon

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-09 Thread oliver
On Thu, Jun 09, 2011 at 12:56:09PM -0400, Simon Urbanek wrote:
 
 On Jun 9, 2011, at 11:15 AM, oliver wrote:
 
  On Thu, Jun 09, 2011 at 10:54:28AM -0400, Duncan Murdoch wrote:
  On 09/06/2011 9:28 AM, oliver wrote:
  On Thu, Jun 09, 2011 at 07:43:20AM -0400, Duncan Murdoch wrote:
  On 11-06-09 7:27 AM, oliver wrote:
  On Wed, Jun 08, 2011 at 08:35:34PM -0400, Simon Urbanek wrote:
  
  On Jun 8, 2011, at 8:32 PM, oliver wrote:
  
  On Thu, Jun 09, 2011 at 02:17:31AM +0200, oliver wrote:
  [...]
  OK, I looked at this now.
  
  LENGTH() checks the length of the vector.
  
  Good to know this.
  
  So the problem of a vector of length 0 can be with any arguments of 
  type SEXP,
  hence I will need to check ANY arg on it's length.
  
  This is vital to stability under any situation.
  
  Thanks for this valuable hint!
  
  I will add checks for all my SEXP-args.
  [...]
  
  Hey, LENGTH() does not work with String-vectors! :(
  
  
  Of course it does ...
  
  
  
  It does not so on my R 2.10.1 installation.
  
  
  In the R-Shell I get:
  
==
   length(c())
[1] 0
  
==
  
  So c() is vec of length 0.
  
  When I feed my readjpeg() with c() as filename arg,
  
  testing with:
  
if( LENGTH( filename_sexp )   1 )
{
  error(LENGTH( filename_sexp )   1);
  //error(filename can't be vector of length 0);
}
else
{
  error(LENGTH( filename_sexp ) is not   1);
}
  
  
  
  I got:
Error in readjpeg(filename = c()) : LENGTH( filename_sexp ) is not   
  1
  
  
  You can explain why?
  
  c() doesn't create a STRSXP, it is NULL, which is a NILSXP.
  LENGTH() doesn't work on that object.  (I'd recommend using length()
  rather than LENGTH(); it's a function, not a macro, and it does give
  the expected answer.)
  [...]
  
  Interestingly, c() as value for an integer value
  can be testes correctly with LENGTH().
  
  Presumably you converted it to an INTSXP.  c() is NULL.
  [...]
  
  Ooops, yes. In the R-part. :-)
  I did it, because when I don't get an int from the user's call,
  I have set it to NA (default in the R-function definition).
  
  I made
   width - as.integer(width)
  for easy check inside the C-part, on integer.
  
  (I really only need int, no floating point values.)
  
  I thought this makes the C-code easier at that part.
  
  Or I may check on NA inside the C-part.
  
  How would I do that correctly?
  
  After so much different opinions I read here, I'm rather puzzled on that 
  now.
  
  If there is already a R-extensions-FAQ I could collect the informations
  from the mail sof the last days, so that the different opinions and the 
  discussion
  can result in a some answers...
  
 
 So far you were creating the confusion, the facts are fairly simple if you
 have some clue about C and R. I would like to ask you to, please, read and
 understand C standard and R-exts *before* posting further questions since it's
 pointless if we provide answers that you don't understand.
[...]

I understand the answers if it's about C, but I'm new to R.
If you think I did wrote something about C, that is not correct,
please show me.


On the R-usage: Regarding the LENGTH() vs. length(),
both are used in the examples of Writing R Extensions
and somewhere it was mentioned that either macros or functions can be used.

Here on the  list I read different opinions about that, coming from  different 
people.
As there are also different usages in the Writing R Extensions it's confusing.

Macro or function?
Should I include Rinternals.h or Rdefines.h?

It's not clear, what I should prefer, or when.



 The fact that you
 are impatient (as you said yourself) doesn't entitle you to waste other
 people's time to explain things to you while you are unwilling to take the 
 time
 yourself.


That the memory allocation part was coming in chapter six,
which at first glance rather looked like a reference of convenience or 
mathematical
functions that can be called, I didn't awaited.
(And so I missed it in chapter 5, hence my questions.)

So I just can apologize that I didn't read that before asking,
but also can mention that the memory allocation issue also might make sense to 
be
mentioned in chapter 5.

And on the other hand I think wasting time of others makes no sense.
I also would invest time to help others on topics, even it might be documented
somewhere. (Thats why there are mailing lists, IMHO.)
It could even be the case that people read something but didn't remember
anything after first reading.

For example I had to look at the Writing R extensions more than once,
and some things I already read, I had not saved immediately.
(That's normal, that people need repitition to learn, btw.)

There always is a tradeoff between first-read-long-documentations and do not 
practice
and read in the docs, try it out, read more, 

Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-08 Thread Prof Brian Ripley

On Tue, 7 Jun 2011, Duncan Murdoch wrote:


On 07/06/2011 9:08 AM, oliver wrote:

Hello,

following an advice here from the list I looked into sources of other
packages (xts) and found the TYPEOF() macro/function, which really is
helpful.


It is documented, of course, but actually better alternatives are 
described in 'Writing R Extensions'.


We would urge you to study the R sources rather than copy bad habits 
from randomly chosen package sources.



I iused the follwong code snippet:


   switch( TYPEOF( filename_sexp ) )
   {
 case STRSXP: filename = CHAR( STRING_ELT(filename_sexp, 0) );
  break;

 default: error(filename argument must be a string);
  break;
   }


Here, filename is of type char*
and one function opens a file with that name.
So it is purely intended to just grab out the char* from the
String-Expression.

Am I doing something wrong here, or is it ok, but I have somehow
to say the extracting macros/functions, that it is really intended
to throw away information and that a warning is not necessary?


The result of calling CHAR should be a const char *.  You are not allowed 
to touch the string it points to.


Note too that isString() exists for this purpose, and there is no 
check in that code that LENGTH(filename_sexp)  0 (or == 1).  In the R 
sources you will often see things like


if(!isString(sfile) || LENGTH(sfile)  1)
error(invalid '%s' argument, description);

Then, reading on,

file = translateChar(STRING_ELT(sfile, 0));

for you cannot (in general) assume that the character vector passed is 
in the native encoding.


--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-08 Thread oliver
On Wed, Jun 08, 2011 at 12:22:10PM +0100, Prof Brian Ripley wrote:
 On Tue, 7 Jun 2011, Duncan Murdoch wrote:
 
 On 07/06/2011 9:08 AM, oliver wrote:
 Hello,
 
 following an advice here from the list I looked into sources of other
 packages (xts) and found the TYPEOF() macro/function, which really is
 helpful.
 
 It is documented, of course, but actually better alternatives are
 described in 'Writing R Extensions'.
 
 We would urge you to study the R sources rather than copy bad habits
 from randomly chosen package sources.
[...]

Hmhh, it was not randomly chosen, it was, what I got as a hint here on the list.


 
 I iused the follwong code snippet:
 
 
switch( TYPEOF( filename_sexp ) )
{
  case STRSXP: filename = CHAR( STRING_ELT(filename_sexp, 0) );
   break;
 
  default: error(filename argument must be a string);
   break;
}
 
 
 Here, filename is of type char*
 and one function opens a file with that name.
 So it is purely intended to just grab out the char* from the
 String-Expression.
 
 Am I doing something wrong here, or is it ok, but I have somehow
 to say the extracting macros/functions, that it is really intended
 to throw away information and that a warning is not necessary?
 
 The result of calling CHAR should be a const char *.  You are
 not allowed to touch the string it points to.
 
 Note too that isString() exists for this purpose,
[...]

OK, I also sometimes used that (maybe I threw it out or used it in other
modules).



 and there is no
 check in that code that LENGTH(filename_sexp)  0 (or == 1).  In the
 R sources you will often see things like
 
 if(!isString(sfile) || LENGTH(sfile)  1)
 error(invalid '%s' argument, description);
[...]

If it's a vector, I can just pic the first element.
Or does   LENGTH(sfile)  give the length of the string itself
(like strlen(3))?

If the latter, then my file-opeing operation would faile with an error.
Of course I check if my fopen() gibves back NULL.



 
 Then, reading on,
 
 file = translateChar(STRING_ELT(sfile, 0));

translateChar is explained on page 120 of the extension writing do.

I'm not in this point of the documentation.

And I often need to look around, when I want to find a function,
as they are documented/explained somewhere during the flow of the text.

Something like a API description that is brief would help.

For example something like in the manuals of the OCaml language:

  http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html


That's very brief.

Chapter 6 of the  Writing R Extensions is rather in this style
and gives a good overview.
Something like that for the macros would be helpful.


 
 for you cannot (in general) assume that the character vector passed
 is in the native encoding.

I try to do some coercions (e.g. as.integer())
when I need integer in the C-code and then
in the C-part rigidly check on integer.

About the char-encodings I have not thought much.


Ciao,
   Oliver

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-08 Thread Simon Urbanek

On Jun 8, 2011, at 12:08 PM, oliver wrote:

 On Wed, Jun 08, 2011 at 12:22:10PM +0100, Prof Brian Ripley wrote:
 On Tue, 7 Jun 2011, Duncan Murdoch wrote:
 
 On 07/06/2011 9:08 AM, oliver wrote:
 Hello,
 
 following an advice here from the list I looked into sources of other
 packages (xts) and found the TYPEOF() macro/function, which really is
 helpful.
 
 It is documented, of course, but actually better alternatives are
 described in 'Writing R Extensions'.
 
 We would urge you to study the R sources rather than copy bad habits
 from randomly chosen package sources.
 [...]
 
 Hmhh, it was not randomly chosen, it was, what I got as a hint here on the 
 list.
 

It was not provided to you to look at how it checks arguments. For basic usage 
it's better to look at the R code. The coding styles vary a lot in packages 
(and so does the quality of packages) - some of them are really bad, but you 
have to remember that most people write packages in their free time and are not 
programmers...


 
 
 I iused the follwong code snippet:
 
 
  switch( TYPEOF( filename_sexp ) )
  {
case STRSXP: filename = CHAR( STRING_ELT(filename_sexp, 0) );
 break;
 
default: error(filename argument must be a string);
 break;
  }
 
 
 Here, filename is of type char*
 and one function opens a file with that name.
 So it is purely intended to just grab out the char* from the
 String-Expression.
 
 Am I doing something wrong here, or is it ok, but I have somehow
 to say the extracting macros/functions, that it is really intended
 to throw away information and that a warning is not necessary?
 
 The result of calling CHAR should be a const char *.  You are
 not allowed to touch the string it points to.
 
 Note too that isString() exists for this purpose,
 [...]
 
 OK, I also sometimes used that (maybe I threw it out or used it in other
 modules).
 
 
 
 and there is no
 check in that code that LENGTH(filename_sexp)  0 (or == 1).  In the
 R sources you will often see things like
 
if(!isString(sfile) || LENGTH(sfile)  1)
error(invalid '%s' argument, description);
 [...]
 
 If it's a vector, I can just pic the first element.

Yes, but only if it's not a vector of length zero - hence the necessary check.


 Or does   LENGTH(sfile)  give the length of the string itself
 (like strlen(3))?
 

No.


 If the latter, then my file-opeing operation would faile with an error.
 Of course I check if my fopen() gibves back NULL.
 
 
 
 
 Then, reading on,
 
file = translateChar(STRING_ELT(sfile, 0));
 
 translateChar is explained on page 120 of the extension writing do.
 
 I'm not in this point of the documentation.
 
 And I often need to look around, when I want to find a function,
 as they are documented/explained somewhere during the flow of the text.
 
 Something like a API description that is brief would help.
 
 For example something like in the manuals of the OCaml language:
 
  http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html
 
 
 That's very brief.
 
 Chapter 6 of the  Writing R Extensions is rather in this style
 and gives a good overview.
 Something like that for the macros would be helpful.
 
 
 
 for you cannot (in general) assume that the character vector passed
 is in the native encoding.
 
 I try to do some coercions (e.g. as.integer())
 when I need integer in the C-code and then
 in the C-part rigidly check on integer.
 

It's usually more efficient to use coerceVector() on the C side since that 
guarantees no copy is made for matching types (this matters for passing data, 
not so much for passing arguments). You can do either - check on R side or 
check on C side, usually you don't need both (but you certainly can).

Cheers,
Simon


 About the char-encodings I have not thought much.
 
 
 Ciao,
   Oliver
 
 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel
 
 

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-08 Thread oliver
On Wed, Jun 08, 2011 at 02:23:29PM -0400, Simon Urbanek wrote:
 
 On Jun 8, 2011, at 12:08 PM, oliver wrote:
 
  On Wed, Jun 08, 2011 at 12:22:10PM +0100, Prof Brian Ripley wrote:
  On Tue, 7 Jun 2011, Duncan Murdoch wrote:
  
  On 07/06/2011 9:08 AM, oliver wrote:
  Hello,
  
  following an advice here from the list I looked into sources of other
  packages (xts) and found the TYPEOF() macro/function, which really is
  helpful.
  
  It is documented, of course, but actually better alternatives are
  described in 'Writing R Extensions'.
  
  We would urge you to study the R sources rather than copy bad habits
  from randomly chosen package sources.
  [...]
  
  Hmhh, it was not randomly chosen, it was, what I got as a hint here on the 
  list.
  
 
 It was not provided to you to look at how it checks arguments. For basic
 usage it's better to look at the R code. The coding styles vary a lot in
 packages (and so does the quality of packages) - some of them are really bad,
 but you have to remember that most people write packages in their free time 
 and
 are not programmers...

OK, I see.


[...]
  and there is no
  check in that code that LENGTH(filename_sexp)  0 (or == 1).  In the
  R sources you will often see things like
  
 if(!isString(sfile) || LENGTH(sfile)  1)
 error(invalid '%s' argument, description);
  [...]
  
  If it's a vector, I can just pic the first element.
 
 Yes, but only if it's not a vector of length zero - hence the necessary check.
 
 
  Or does   LENGTH(sfile)  give the length of the string itself
  (like strlen(3))?
  
 
 No.
[...]

OK, I looked at this now.

LENGTH() checks the length of the vector.

Good to know this.

So the problem of a vector of length 0 can be with any arguments of type SEXP,
hence I will need to check ANY arg on it's length.

This is vital to stability under any situation.

Thanks for this valuable hint!

I will add checks for all my SEXP-args.


Ciao,
   Oliver

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-08 Thread oliver
On Thu, Jun 09, 2011 at 02:17:31AM +0200, oliver wrote:
[...]
 OK, I looked at this now.
 
 LENGTH() checks the length of the vector.
 
 Good to know this.
 
 So the problem of a vector of length 0 can be with any arguments of type SEXP,
 hence I will need to check ANY arg on it's length.
 
 This is vital to stability under any situation.
 
 Thanks for this valuable hint!
 
 I will add checks for all my SEXP-args.
[...]

Hey, LENGTH() does not work with String-vectors! :(



Ciao,
   Oliver

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-08 Thread Simon Urbanek

On Jun 8, 2011, at 8:32 PM, oliver wrote:

 On Thu, Jun 09, 2011 at 02:17:31AM +0200, oliver wrote:
 [...]
 OK, I looked at this now.
 
 LENGTH() checks the length of the vector.
 
 Good to know this.
 
 So the problem of a vector of length 0 can be with any arguments of type 
 SEXP,
 hence I will need to check ANY arg on it's length.
 
 This is vital to stability under any situation.
 
 Thanks for this valuable hint!
 
 I will add checks for all my SEXP-args.
 [...]
 
 Hey, LENGTH() does not work with String-vectors! :(
 

Of course it does ...

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-08 Thread Jeffrey Ryan
On Wed, Jun 8, 2011 at 7:17 PM, oliver oli...@first.in-berlin.de wrote:

 On Wed, Jun 08, 2011 at 02:23:29PM -0400, Simon Urbanek wrote:
 
  On Jun 8, 2011, at 12:08 PM, oliver wrote:
 
   On Wed, Jun 08, 2011 at 12:22:10PM +0100, Prof Brian Ripley wrote:
   On Tue, 7 Jun 2011, Duncan Murdoch wrote:
  
   On 07/06/2011 9:08 AM, oliver wrote:
   Hello,
  
   following an advice here from the list I looked into sources of
 other
   packages (xts) and found the TYPEOF() macro/function, which really
 is
   helpful.
  
   It is documented, of course, but actually better alternatives are
   described in 'Writing R Extensions'.
  
   We would urge you to study the R sources rather than copy bad habits
   from randomly chosen package sources.
   [...]
  
   Hmhh, it was not randomly chosen, it was, what I got as a hint here on
 the list.
  
 
  It was not provided to you to look at how it checks arguments. For basic
  usage it's better to look at the R code. The coding styles vary a lot in
  packages (and so does the quality of packages) - some of them are really
 bad,
  but you have to remember that most people write packages in their free
 time and
  are not programmers...

 OK, I see.


Of course - most of R core aren't programmers either - whatever that
means.  Statisticians, mathematicians, etc...

;-)

Most contributed packages aren't meant to be case studies in a comp-sci
class either, they are meant to solve real world problems - problems that
many of us work on daily.

That said, I'd also say look to R sources first, but since many things in R
core aren't available in the API - you aren't really able to copy the 'best
practices' alluded to.  And sometimes you've got to bootstrap solutions when
the list is otherwise silent.  Another reason that you should look outside
of R sources in addition to inside of them is that the community code is far
more abundant that the core code.  Sort of like theory vs. practice - they
only teach so much in school.

For reference, TYPEOF is straight from R source (of course):

http://svn.r-project.org/R/trunk/src/main/subset.c

Cheers,
Jeff



 [...]
   and there is no
   check in that code that LENGTH(filename_sexp)  0 (or == 1).  In the
   R sources you will often see things like
  
  if(!isString(sfile) || LENGTH(sfile)  1)
  error(invalid '%s' argument, description);
   [...]
  
   If it's a vector, I can just pic the first element.
 
  Yes, but only if it's not a vector of length zero - hence the necessary
 check.
 
 
   Or does   LENGTH(sfile)  give the length of the string itself
   (like strlen(3))?
  
 
  No.
 [...]

 OK, I looked at this now.

 LENGTH() checks the length of the vector.

 Good to know this.

 So the problem of a vector of length 0 can be with any arguments of type
 SEXP,
 hence I will need to check ANY arg on it's length.

 This is vital to stability under any situation.

 Thanks for this valuable hint!

 I will add checks for all my SEXP-args.


 Ciao,
   Oliver

 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel




-- 
Jeffrey Ryan
jeffrey.r...@lemnica.com

www.lemnica.com
www.esotericR.com

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] warning: assignment discards qualifiers from pointer target type

2011-06-07 Thread oliver

Hello,

following an advice here from the list I looked into sources of other
packages (xts) and found the TYPEOF() macro/function, which really is
helpful.

I iused the follwong code snippet:


  switch( TYPEOF( filename_sexp ) )
  {
case STRSXP: filename = CHAR( STRING_ELT(filename_sexp, 0) );
 break;

default: error(filename argument must be a string);
 break;
  }


Here, filename is of type char*
and one function opens a file with that name.
So it is purely intended to just grab out the char* from the
String-Expression.

Am I doing something wrong here, or is it ok, but I have somehow
to say the extracting macros/functions, that it is really intended
to throw away information and that a warning is not necessary?

Ciao,
   Oliver

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] warning: assignment discards qualifiers from pointer target type

2011-06-07 Thread Duncan Murdoch

On 07/06/2011 9:08 AM, oliver wrote:

Hello,

following an advice here from the list I looked into sources of other
packages (xts) and found the TYPEOF() macro/function, which really is
helpful.

I iused the follwong code snippet:


   switch( TYPEOF( filename_sexp ) )
   {
 case STRSXP: filename = CHAR( STRING_ELT(filename_sexp, 0) );
  break;

 default: error(filename argument must be a string);
  break;
   }


Here, filename is of type char*
and one function opens a file with that name.
So it is purely intended to just grab out the char* from the
String-Expression.

Am I doing something wrong here, or is it ok, but I have somehow
to say the extracting macros/functions, that it is really intended
to throw away information and that a warning is not necessary?


The result of calling CHAR should be a const char *.  You are not 
allowed to touch the string it points to.


Duncan Murdoch

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel