[Xapian-devel] Replace atoi and atol with strtol strtoul:Need Help

Priyank Bhatt priyankbhatt1234 at gmail.com
Tue Dec 16 19:45:12 GMT 2014


Hello ,
I came across this function *HtmlParser::decode_entities(string &s)* in
*xapian-application/omega/htmlparse.cc* which basically does is extract hex
value if any or extract number.For extracting number atoi is used and value
returned by it is stored in variable "val" , I think so replacing atoi with
strtoul would be useful here as number can have larger value although the
variable "val" is unsigned int so i need to change the that definition of
"val" also. Is that ok to do so ? Just need to clarify .

On 16 December 2014 at 04:34, Olly Betts <olly at survex.com> wrote:
>
> On Tue, Dec 16, 2014 at 02:32:31AM +0530, Priyank Bhatt wrote:
> > I am working on replacing atoi () and atol() functions with strtol() and
> > strtoul() . I came across many files which uses statement  like these
> > time_t secs= atoi(data_span.c_str()), here time_t Datatype is not known
> but
> > wikipedia says that it is integer so is it necessary to replace  atoi
> with
> > strtol over here ??
>
> The time_t type is a standard one - ISO C only says it's a "arithmetic
> type" (so it could potentially be a double) but POSIX says it's an
> "integer type":
>
> http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html
>
> While that would allow it to be unsigned, in practice it seems to be a
> signed integer, and it is 64 bit on modern systems (since a signed 32
> bit integer with the Unix epoch can only represent dates up to 2038).
>
> You missed out a relevant part of line, which in full is:
>
> xapian-applications/omega/date.cc:      time_t secs =
> atoi(date_span.c_str()) * (24 * 60 * 60);
>
> So in this case, date_span is a number of days, so converting it via a
> long is reasonable - even if long is 32 bits that can still represent a
> span of 5.8 million years.  You'd want to make sure the multiplication
> happens in type time_t though.
>
> If we were actually converting a number of seconds, you'd want to use
> strtoll(), at least if sizeof(long) < 8.  C++11 includes strtoll(), and
> we decided to require a C++11 compiler for 1.3.x, so there's no need to
> worry whether strtoll() is available.
>
> > And is their any document which helps me what each file function does
> like
> > date.cc,cgiparams.cc,omega.cc,query.cc.
>
> There should be a short comment summarising the purpose of each file at
> the top, for example:
>
> | /* date.cc: date range parsing routines for omega
>
> A few files may still lack these, but most have them.
>
> Cheers,
>     Olly
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.xapian.org/pipermail/xapian-devel/attachments/20141217/ec869922/attachment.html>


More information about the Xapian-devel mailing list