summaryrefslogtreecommitdiffstats
path: root/src/helloworld.d
blob: fdf8063736002ed462c963ccbd8ba4df4c5d8494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import config;
import testlibrary;
import testshlibrary;
import std.stdio;
import catalog;

void main(char[][] args)
{
    // Internationalization Example
    Catalog.Init(config.PACKAGE, config.PACKAGE_LOCALE_DIR);

    writefln(Catalog.GetString("Hello!"));

    writefln(HelloWorldFactory.getName()  ~ ", " ~ HelloWorldSharedObjectFactory.getVersion() ~ " " ~ config.VERSION);

    writefln(Catalog.GetString("Compiled using: ") ~ config.GDC_VERSION);

    // auto type inference and built-in foreach
    foreach (argc, argv; args)
    {
        // Object Oriented Programming
        CmdLin cl = new CmdLin(argc, argv);
        // Improved typesafe printf
        writefln(cl.argnum, cl.suffix, Catalog.GetString(" arg: %s"), cl.argv);
        // Automatic or explicit memory management
        delete cl;
    }

    // Nested structs and classes
    struct specs
    {
        // all members automatically initialized
        int count, allocated;
    }

    // Nested functions can refer to outer
    // variables like args
    specs argspecs()
    {
        specs* s = new specs;
        // no need for '->'
        s.count = args.length;		   // get length of array with .length
        s.allocated = typeof(args).sizeof; // built-in native type properties
        foreach (argv; args)
            s.allocated += argv.length * typeof(argv[0]).sizeof;
        return *s;
    }

    // built-in string and common string operations
    writefln(Catalog.GetString("argc = %d, ") ~ Catalog.GetString("allocated = %d"),
	argspecs().count, argspecs().allocated);
}

class CmdLin
{
    private int _argc;
    private char[] _argv;

public:
    this(int argc, char[] argv)	// constructor
    {
        _argc = argc;
        _argv = argv;
    }

    int argnum()
    {
        return _argc + 1;
    }

    char[] argv()
    {
        return _argv;
    }

    char[] suffix()
    {
        char[] suffix = "th";
        switch (_argc)
        {
          case 0:
            suffix = "st";
            break;
          case 1:
            suffix = "nd";
            break;
          case 2:
            suffix = "rd";
            break;
          default:
	    break;
        }
        return suffix;
    }
}