is there an api equivalent for atoi? 
Author Message
 is there an api equivalent for atoi?

I'm writing a windows app that needs to be SMALL.. no MFC, no atl, no UI,
etc.  I don't want to include all of stdlib.h just to use atoi() ONCE.   Is
there an equivalent Win32 function???

Thanks,

Jason



Tue, 04 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?
You can write your own atoi routine with C++ or _asm with just a few bytes
of code.
--
URL: http://otool.hypermart.net/

Fax: (225)673-3062


Quote:
> I'm writing a windows app that needs to be SMALL.. no mfc, no atl, no UI,
> etc.  I don't want to include all of stdlib.h just to use atoi() ONCE.
Is
> there an equivalent Win32 function???

> Thanks,

> Jason



Tue, 04 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?

Quote:

> I'm writing a windows app that needs to be SMALL.. no mfc, no atl, no UI,
> etc.  I don't want to include all of stdlib.h just to use atoi() ONCE.   Is
> there an equivalent Win32 function???

> Thanks,

> Jason

No API!

I'm not sure that the linker will include ALL function declared in
stdlib.h if
you just use one function (in fact it merrely not!), so normally
including stdlib.h
will not transform your program in bloatware.

Anyway, you can use something like:

int atoi(char* str)
{
   int retval = 0;
   int i;
   if (!str) return 0;

   for(i=0; str[i]; i++)
      {
         if (i > (sizeof(int)*8))
            {  // Prevent walk thru all the mem if
               // the passed pointer was stupid.
               retval = 0;
               break;
            }
         if (str[i] < '0' || str[i] > '9')
            {
               retval = 0;
               break;
            }
         retval *= 10;
         retval += str[i] - '0';
      }
   return retval;

Quote:
}



Tue, 04 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?

Quote:
> I'm writing a windows app that needs to be SMALL.. no mfc, no atl, no UI,
> etc.  I don't want to include all of stdlib.h just to use atoi() ONCE.   Is
> there an equivalent Win32 function???

Including stdlib.h does NOT mean that all the functions prototyped in
stdlib.h will be linked into your program.  The only way it'll make a
significant difference in size is if you're presently not using the
standard library AT ALL.  In that case, using atoi would mean linking
in the standard library startup code, which adds a few kilobytes or so
to the program.  In a case like that, you could write your own version
of atoi:

int atoi(char const *string) {

        char *pos;
        int neg = 0;
        unsigned val = 0;

        // skip leading white-space
        while (*pos == ' ' || *pos == '\t')    
                pos++;

        // check for a minus sign.
        if ( *pos == '-' ) {
                neg = 1;
                pos++;
                while (*pos == ' ' || *pos == '\t')
                        pos++;
        }

        // read in the digits.
        while (*pos>='0' && *pos<='9') {
                val *= 10;
                val += *pos - '0';
        }

        // if it was negative, return the negative of the value.
        if (neg)
                return -val;

        // otherwise, return the value;
        return (int)val;

Quote:
}

This does have a few shortcomings you could deal with if you chose.  
Just for example, it can return bad results if you try to use it with
a number that'll fit into an unsigned int but not into a signed int.


Tue, 04 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?

Quote:

>I'm writing a windows app that needs to be SMALL.. no mfc, no atl, no UI,
>etc.  I don't want to include all of stdlib.h just to use atoi() ONCE.   Is
>there an equivalent Win32 function???

The "stdlib.h" header file has NO effect on what gets linked into your
program! Most modern linkers (eg the VC++ linker) are "smart" and will only
link into your program the functions which you actually call. Including
"stdlib.h" certainly won't result in all the functions declared in that
header file being linked into your executable!

Chris
-----------------------------------------------------------------------

Visit our web site at http://www.skymap.com
Astronomy software written by astronomers, for astronomers



Tue, 04 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?
go to CRT source and copy & paste atoi() to your program.
Quote:

> I'm writing a windows app that needs to be SMALL.. no mfc, no atl, no UI,
> etc.  I don't want to include all of stdlib.h just to use atoi() ONCE.   Is
> there an equivalent Win32 function???

> Thanks,

> Jason



Thu, 06 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?
Copy the function from the file ATOX.C
You make need to copy other functions such as isspace and isdigit.

Regards,

James Pullicino.


Quote:


>>I'm writing a windows app that needs to be SMALL.. no mfc, no atl, no UI,
>>etc.  I don't want to include all of stdlib.h just to use atoi() ONCE.
Is
>>there an equivalent Win32 function???

>The "stdlib.h" header file has NO effect on what gets linked into your
>program! Most modern linkers (eg the VC++ linker) are "smart" and will only
>link into your program the functions which you actually call. Including
>"stdlib.h" certainly won't result in all the functions declared in that
>header file being linked into your executable!

>Chris
>-----------------------------------------------------------------------

>Visit our web site at http://www.skymap.com
>Astronomy software written by astronomers, for astronomers



Fri, 07 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?
Not true.  the library startup is always there, and is ~30K in VC.  Link
MSVCRT or write your own CRT for small apps.  I have my own personal RTL
that has ~130 bytes overhead.  Most of my apps are under 10K.

Andy



Sat, 08 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?
Why the hell are you {*filter*}ing about 30K?


Sat, 08 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?

Quote:
> Why the hell are you {*filter*}ing about 30K?

Uploading a small application that is 30K bigger 1,000,000 times equals an
extra  bandwidth of 30,720,000,000 bytes! In certain circonstances that is
important.

Adeluc

DecoTech Design Software Inc.
Internet: http://www.*-*-*.com/



Sat, 08 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?


Quote:
> Not true.  the library startup is always there, and is ~30K in VC.

At least a little quoting is helpful -- I have a vague recollection of
there having been a recent question about writing programs to be as
small as possible, but I'm not sure what you're saying isn't true.

It's certainly possible to write programs that don't use the library
startup code in VC.

In any case, the size of the library startup code depends heavily upon
exactly how you link things.  If you link with the default alignment,
it takes up about 24K.  If you link with an alignment of 16 bytes,
that's reduced to around 14K.  The startup code apparently has quite a
few sections that are close to empty, which I suppose isn't too
surprising.



Sat, 08 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?
On Tue, 22 Jun 1999 12:32:03 -0700, "Jahanzeb Khan"

Quote:

>Why the hell are you {*filter*}ing about 30K?

Because it is there.


Sun, 09 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?

Quote:

>It's certainly possible to write programs that don't use the library
>startup code in VC.

Absolutely, yes. For example ATL components don't, by default, include it.

Chris
-----------------------------------------------------------------------

Visit our web site at http://www.skymap.com
Astronomy software written by astronomers, for astronomers



Sun, 09 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?
On Wed, 23 Jun 1999 08:32:46 +0100, "Chris Marriott"

Quote:


>>It's certainly possible to write programs that don't use the library
>>startup code in VC.

>Absolutely, yes. For example ATL components don't, by default, include it.

I use Borland C++ 5.02 IDE compiler... does anyone know how I can tell
it not to include the standard startup code, or to include my own
startup code? Also, how might I go about *creating* my own alternative
startup code? Could it be written in C or would assembly code be
necessary?

-Dale



Mon, 10 Dec 2001 03:00:00 GMT  
 is there an api equivalent for atoi?

Hi,

What about including the source code of
the atoi function in your own code ?
You can adapt it to your needs and make
it as short as possible.

I have attached the C source and the corresponding
ASM source. I found it in the CRT of Visual C++ 5.0

Fabrice,

-----------------------------------------------------
"A thousand-mile journey begins with the first step."
-----------------------------------------------------

Fabrice Dupre
Hewlett-Packard - ISBU/Smart Card Center       Tel: +33 476 14 43 57
38053 GRENOBLE Cedex 09 -  FRANCE              Fax: +33 476 14 12 78

[ ATOX.C 2K ]
/***
*atox.c - atoi and atol conversion
*
*       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
*       Converts a character string into an int or long.
*
*******************************************************************************/

#include <cruntime.h>
#include <stdlib.h>
#include <ctype.h>

/***
*long atol(char *nptr) - Convert string to long
*
*Purpose:
*       Converts ASCII string pointed to by nptr to binary.
*       Overflow is not detected.
*
*Entry:
*       nptr = ptr to string to convert
*
*Exit:
*       return long int value of the string
*
*Exceptions:
*       None - overflow is not detected.
*
*******************************************************************************/

long __cdecl atol(
        const char *nptr
        )
{
        int c;              /* current char */
        long total;         /* current total */
        int sign;           /* if '-', then negative, otherwise positive */

        /* skip whitespace */
        while ( isspace((int)(unsigned char)*nptr) )
            ++nptr;

        c = (int)(unsigned char)*nptr++;
        sign = c;           /* save sign indication */
        if (c == '-' || c == '+')
            c = (int)(unsigned char)*nptr++;    /* skip sign */

        total = 0;

        while (isdigit(c)) {
            total = 10 * total + (c - '0');     /* accumulate digit */
            c = (int)(unsigned char)*nptr++;    /* get next char */
        }

        if (sign == '-')
            return -total;
        else
            return total;   /* return result, negated if necessary */

Quote:
}

/***
*int atoi(char *nptr) - Convert string to long
*
*Purpose:
*       Converts ASCII string pointed to by nptr to binary.
*       Overflow is not detected.  Because of this, we can just use
*       atol().
*
*Entry:
*       nptr = ptr to string to convert
*
*Exit:
*       return int value of the string
*
*Exceptions:
*       None - overflow is not detected.
*
*******************************************************************************/

int __cdecl atoi(
        const char *nptr
        )
{
        return (int)atol(nptr);

Quote:
}

#ifndef _NO_INT64

__int64 __cdecl _atoi64(
        const char *nptr
        )
{
        int c;              /* current char */
        __int64 total;      /* current total */
        int sign;           /* if '-', then negative, otherwise positive */

        /* skip whitespace */
        while ( isspace((int)(unsigned char)*nptr) )
            ++nptr;

        c = (int)(unsigned char)*nptr++;
        sign = c;           /* save sign indication */
        if (c == '-' || c == '+')
            c = (int)(unsigned char)*nptr++;    /* skip sign */

        total = 0;

        while (isdigit(c)) {
            total = 10 * total + (c - '0');     /* accumulate digit */
            c = (int)(unsigned char)*nptr++;    /* get next char */
        }

        if (sign == '-')
            return -total;
        else
            return total;   /* return result, negated if necessary */

Quote:
}

#endif  /* _NO_INT64 */

[ atox.asm 3K ]
--- atox.c  ------------------------------------------------------------------------------------------------------------------------------------------------
atol:
004128B0   push        ebp
004128B1   mov         ebp,esp
004128B3   sub         esp,14h
004128B6   cmp         dword ptr [___mb_cur_max (0055a4dc)],1
004128BD   jle         atol+26h (004128d6)
004128BF   push        8
004128C1   mov         eax,dword ptr [nptr]
004128C4   xor         ecx,ecx
004128C6   mov         cl,byte ptr [eax]
004128C8   push        ecx
004128C9   call        _isctype (0041bce0)
004128CE   add         esp,8
004128D1   mov         dword ptr [ebp-10h],eax
004128D4   jmp         atol+3Fh (004128ef)
004128D6   mov         edx,dword ptr [nptr]
004128D9   xor         eax,eax
004128DB   mov         al,byte ptr [edx]
004128DD   mov         ecx,dword ptr [__pctype (0055a2d0)]
004128E3   xor         edx,edx
004128E5   mov         dx,word ptr [ecx+eax*2]
004128E9   and         edx,8
004128EC   mov         dword ptr [ebp-10h],edx
004128EF   cmp         dword ptr [ebp-10h],0
004128F3   je          atol+50h (00412900)
004128F5   mov         eax,dword ptr [nptr]
004128F8   add         eax,1
004128FB   mov         dword ptr [nptr],eax
004128FE   jmp         atol+6 (004128b6)
00412900   mov         ecx,dword ptr [nptr]
00412903   xor         edx,edx
00412905   mov         dl,byte ptr [ecx]
00412907   mov         dword ptr [c],edx
0041290A   mov         eax,dword ptr [nptr]
0041290D   add         eax,1
00412910   mov         dword ptr [nptr],eax
00412913   mov         ecx,dword ptr [c]
00412916   mov         dword ptr [sign],ecx
00412919   cmp         dword ptr [c],2Dh
0041291D   je          atol+75h (00412925)
0041291F   cmp         dword ptr [c],2Bh
00412923   jne         atol+88h (00412938)
00412925   mov         edx,dword ptr [nptr]
00412928   xor         eax,eax
0041292A   mov         al,byte ptr [edx]
0041292C   mov         dword ptr [c],eax
0041292F   mov         ecx,dword ptr [nptr]
00412932   add         ecx,1
00412935   mov         dword ptr [nptr],ecx
00412938   mov         dword ptr [total],0
0041293F   cmp         dword ptr [___mb_cur_max (0055a4dc)],1
00412946   jle         atol+0ABh (0041295b)
00412948   push        4
0041294A   mov         edx,dword ptr [c]
0041294D   push        edx
0041294E   call        _isctype (0041bce0)
00412953   add         esp,8
00412956   mov         dword ptr [ebp-14h],eax
00412959   jmp         atol+0C0h (00412970)
0041295B   mov         eax,dword ptr [c]
0041295E   mov         ecx,dword ptr [__pctype (0055a2d0)]
00412964   xor         edx,edx
00412966   mov         dx,word ptr [ecx+eax*2]
0041296A   and         edx,4
0041296D   mov         dword ptr [ebp-14h],edx
00412970   cmp         dword ptr [ebp-14h],0
00412974   je          atol+0EBh (0041299b)
00412976   mov         eax,dword ptr [total]
00412979   imul        eax,eax,0Ah
0041297C   mov         ecx,dword ptr [c]
0041297F   lea         edx,[eax+ecx-30h]
00412983   mov         dword ptr [total],edx
00412986   mov         eax,dword ptr [nptr]
00412989   xor         ecx,ecx
0041298B   mov         cl,byte ptr [eax]
0041298D   mov         dword ptr [c],ecx
00412990   mov         edx,dword ptr [nptr]
00412993   add         edx,1
00412996   mov         dword ptr [nptr],edx
00412999   jmp         atol+8Fh (0041293f)
0041299B   cmp         dword ptr [sign],2Dh
0041299F   jne         atol+0F8h (004129a8)
004129A1   mov         eax,dword ptr [total]
004129A4   neg         eax
004129A6   jmp         atol+0FBh (004129ab)
004129A8   mov         eax,dword ptr [total]
004129AB   mov         esp,ebp
004129AD   pop         ebp
004129AE   ret

--- atox.c  ------------------------------------------------------------------------------------------------------------------------------------------------
atoi:
004129B0   push        ebp
004129B1   mov         ebp,esp
004129B3   mov         eax,dword ptr [nptr]
004129B6   push        eax
004129B7   call        atol (004128b0)
004129BC   add         esp,4
004129BF   pop         ebp
004129C0   ret



Tue, 11 Dec 2001 03:00:00 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Is there atoi and/or itoa equivalent?

2. URGENT-need equivalent of atoi() in f77

3. STL equivalent of atoi()?

4. No equivalent to atoi()?

5. ::PostMessage Win32 API equivalent??

6. Functional equivalent to ODBC API call in ADO ??

7. Functional equivalent to ODBC API call in ADO ??

8. CFormView equivalent in API

9. CFormView equivalent in API

10. gettempfilename (win32 api) equivalent in c#

11. .NET Equivalent of SetParent API

12. Using Win32 API's or equivalent function

 

 
Powered by phpBB® Forum Software