| 1 | /* $Id: kHlpGetEnvUZ.c 3575 2007-09-02 20:05:39Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kHlpEnv - kHlpGetEnvUZ.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kStuff.
|
|---|
| 10 | *
|
|---|
| 11 | * kStuff is free software; you can redistribute it and/or
|
|---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 13 | * License as published by the Free Software Foundation; either
|
|---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kStuff is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 19 | * Lesser General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 22 | * License along with kStuff; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | /*******************************************************************************
|
|---|
| 28 | * Header Files *
|
|---|
| 29 | *******************************************************************************/
|
|---|
| 30 | #include <k/kHlpEnv.h>
|
|---|
| 31 | #include <k/kHlpString.h>
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Gets an environment variable and converts it to a KSIZE.
|
|---|
| 36 | *
|
|---|
| 37 | * @returns 0 and *pcb on success.
|
|---|
| 38 | * @returns On failure see kHlpGetEnv.
|
|---|
| 39 | * @param pszVar The name of the variable.
|
|---|
| 40 | * @param pcb Where to put the value.
|
|---|
| 41 | */
|
|---|
| 42 | KHLP_DECL(int) kHlpGetEnvUZ(const char *pszVar, KSIZE *pcb)
|
|---|
| 43 | {
|
|---|
| 44 | KSIZE cb;
|
|---|
| 45 | unsigned uBase;
|
|---|
| 46 | char szVal[64];
|
|---|
| 47 | KSIZE cchVal = sizeof(szVal);
|
|---|
| 48 | const char *psz;
|
|---|
| 49 | int rc;
|
|---|
| 50 |
|
|---|
| 51 | *pcb = 0;
|
|---|
| 52 | rc = kHlpGetEnv(pszVar, szVal, cchVal);
|
|---|
| 53 | if (rc)
|
|---|
| 54 | return rc;
|
|---|
| 55 |
|
|---|
| 56 | /* figure out the base. */
|
|---|
| 57 | uBase = 10;
|
|---|
| 58 | psz = szVal;
|
|---|
| 59 | if ( *psz == '0'
|
|---|
| 60 | && (psz[1] == 'x' || psz[1] == 'X'))
|
|---|
| 61 | {
|
|---|
| 62 | uBase = 16;
|
|---|
| 63 | psz += 2;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /* convert it up to the first unknown char. */
|
|---|
| 67 | cb = 0;
|
|---|
| 68 | for(;;)
|
|---|
| 69 | {
|
|---|
| 70 | const char ch = *psz;
|
|---|
| 71 | unsigned uDigit;
|
|---|
| 72 | if (!ch)
|
|---|
| 73 | break;
|
|---|
| 74 | else if (ch >= '0' && ch <= '9')
|
|---|
| 75 | uDigit = ch - '0';
|
|---|
| 76 | else if (ch >= 'a' && ch <= 'z')
|
|---|
| 77 | uDigit = ch - 'a' + 10;
|
|---|
| 78 | else if (ch >= 'A' && ch <= 'Z')
|
|---|
| 79 | uDigit = ch - 'A' + 10;
|
|---|
| 80 | else
|
|---|
| 81 | break;
|
|---|
| 82 | if (uDigit >= uBase)
|
|---|
| 83 | break;
|
|---|
| 84 |
|
|---|
| 85 | /* add the digit */
|
|---|
| 86 | cb *= uBase;
|
|---|
| 87 | cb += uDigit;
|
|---|
| 88 |
|
|---|
| 89 | psz++;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /* check for unit */
|
|---|
| 93 | if (*psz == 'm' || *psz == 'M')
|
|---|
| 94 | cb *= 1024*1024;
|
|---|
| 95 | else if (*psz == 'k' ||*psz == 'K')
|
|---|
| 96 | cb *= 1024;
|
|---|
| 97 | else if (*psz == 'g' || *psz == 'G')
|
|---|
| 98 | cb *= 1024*1024*1024;
|
|---|
| 99 |
|
|---|
| 100 | *pcb = cb;
|
|---|
| 101 | return 0;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|