nsprpub/pr/tests/i2l.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/i2l.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <stdlib.h>
    1.10 +
    1.11 +#include "prio.h"
    1.12 +#include "prinit.h"
    1.13 +#include "prprf.h"
    1.14 +#include "prlong.h"
    1.15 +
    1.16 +#include "plerror.h"
    1.17 +#include "plgetopt.h"
    1.18 +
    1.19 +typedef union Overlay_i
    1.20 +{
    1.21 +    PRInt32 i;
    1.22 +    PRInt64 l;
    1.23 +} Overlay_i;
    1.24 +
    1.25 +typedef union Overlay_u
    1.26 +{
    1.27 +    PRUint32 i;
    1.28 +    PRUint64 l;
    1.29 +} Overlay_u;
    1.30 +
    1.31 +static PRFileDesc *err = NULL;
    1.32 +
    1.33 +static void Help(void)
    1.34 +{
    1.35 +    PR_fprintf(err, "Usage: -i n | -u n | -h\n");
    1.36 +    PR_fprintf(err, "\t-i n treat following number as signed integer\n");
    1.37 +    PR_fprintf(err, "\t-u n treat following number as unsigned integer\n");
    1.38 +    PR_fprintf(err, "\t-h   This message and nothing else\n");
    1.39 +}  /* Help */
    1.40 +
    1.41 +static PRIntn PR_CALLBACK RealMain(PRIntn argc, char **argv)
    1.42 +{
    1.43 +    Overlay_i si;
    1.44 +    Overlay_u ui;
    1.45 +    PLOptStatus os;
    1.46 +    PRBool bsi = PR_FALSE, bui = PR_FALSE;
    1.47 +    PLOptState *opt = PL_CreateOptState(argc, argv, "hi:u:");
    1.48 +    err = PR_GetSpecialFD(PR_StandardError);
    1.49 +
    1.50 +    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    1.51 +    {
    1.52 +        if (PL_OPT_BAD == os) continue;
    1.53 +        switch (opt->option)
    1.54 +        {
    1.55 +        case 'i':  /* signed integer */
    1.56 +            si.i = (PRInt32)atoi(opt->value);
    1.57 +            bsi = PR_TRUE;
    1.58 +            break;
    1.59 +        case 'u':  /* unsigned */
    1.60 +            ui.i = (PRUint32)atoi(opt->value);
    1.61 +            bui = PR_TRUE;
    1.62 +            break;
    1.63 +        case 'h':  /* user wants some guidance */
    1.64 +         default:
    1.65 +            Help();  /* so give him an earful */
    1.66 +            return 2;  /* but not a lot else */
    1.67 +        }
    1.68 +    }
    1.69 +    PL_DestroyOptState(opt);
    1.70 +
    1.71 +#if defined(HAVE_LONG_LONG)
    1.72 +    PR_fprintf(err, "We have long long\n");
    1.73 +#else
    1.74 +    PR_fprintf(err, "We don't have long long\n");
    1.75 +#endif
    1.76 +
    1.77 +    if (bsi)
    1.78 +    {
    1.79 +        PR_fprintf(err, "Converting %ld: ", si.i);
    1.80 +        LL_I2L(si.l, si.i);
    1.81 +        PR_fprintf(err, "%lld\n", si.l);
    1.82 +    }
    1.83 +
    1.84 +    if (bui)
    1.85 +    {
    1.86 +        PR_fprintf(err, "Converting %lu: ", ui.i);
    1.87 +        LL_I2L(ui.l, ui.i);
    1.88 +        PR_fprintf(err, "%llu\n", ui.l);
    1.89 +    }
    1.90 +    return 0;
    1.91 +
    1.92 +}  /* main */
    1.93 +
    1.94 +
    1.95 +int main(int argc, char **argv)
    1.96 +{
    1.97 +    PRIntn rv;
    1.98 +    
    1.99 +    PR_STDIO_INIT();
   1.100 +    rv = PR_Initialize(RealMain, argc, argv, 0);
   1.101 +    return rv;
   1.102 +}  /* main */
   1.103 +
   1.104 +/* i2l.c */

mercurial