~ubuntu-branches/ubuntu/karmic/ugene/karmic

« back to all changes in this revision

Viewing changes to src/plugins_3rdparty/umuscle/src/muscle/options.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ivan Efremov
  • Date: 2009-01-26 19:17:51 UTC
  • Revision ID: james.westby@ubuntu.com-20090126191751-9kqqevd3yf4o098r
Tags: upstream-1.3.2+repack
ImportĀ upstreamĀ versionĀ 1.3.2+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************
 
2
* Unipro UGENE - Integrated Bioinformatics Suite
 
3
* Copyright (C) 2008 Unipro, Russia (http://ugene.unipro.ru)
 
4
* All Rights Reserved
 
5
 
6
*     This source code is distributed under the terms of the
 
7
*     GNU General Public License. See the files COPYING and LICENSE
 
8
*     for details.
 
9
*****************************************************************/
 
10
 
 
11
#include "muscle.h"
 
12
#include <stdio.h>
 
13
#include "muscle_context.h"
 
14
typedef MuscleContext::options_struct::VALUE_OPT VALUE_OPT;
 
15
typedef MuscleContext::options_struct::FLAG_OPT FLAG_OPT;
 
16
 
 
17
static bool TestSetFlagOpt(const char *Arg)
 
18
        {
 
19
    MuscleContext *ctx = getMuscleContext();
 
20
    int &FlagOptCount = ctx->options.FlagOptCount;
 
21
    FLAG_OPT *FlagOpts = ctx->options.FlagOpts;
 
22
        for (int i = 0; i < FlagOptCount; ++i)
 
23
                if (!stricmp(Arg, FlagOpts[i].m_pstrName))
 
24
                        {
 
25
                        FlagOpts[i].m_bSet = true;
 
26
                        return true;
 
27
                        }
 
28
        return false;
 
29
        }
 
30
 
 
31
static bool TestSetValueOpt(const char *Arg, const char *Value)
 
32
        {
 
33
    MuscleContext *ctx = getMuscleContext();
 
34
    VALUE_OPT *ValueOpts = ctx->options.ValueOpts;
 
35
    int &ValueOptCount = ctx->options.ValueOptCount;
 
36
        for (int i = 0; i < ValueOptCount; ++i)
 
37
                if (!stricmp(Arg, ValueOpts[i].m_pstrName))
 
38
                        {
 
39
                        if (0 == Value)
 
40
                                {
 
41
                                fprintf(stderr, "Option -%s must have value\n", Arg);
 
42
                                exit(EXIT_NotStarted);
 
43
                                }
 
44
                        ValueOpts[i].m_pstrValue = strsave(Value);
 
45
                        return true;
 
46
                        }
 
47
        return false;
 
48
        }
 
49
 
 
50
bool FlagOpt(const char *Name)
 
51
        {
 
52
    MuscleContext *ctx = getMuscleContext();
 
53
    int &FlagOptCount = ctx->options.FlagOptCount;
 
54
    FLAG_OPT *FlagOpts = ctx->options.FlagOpts;
 
55
        for (int i = 0; i < FlagOptCount; ++i)
 
56
                if (!stricmp(Name, FlagOpts[i].m_pstrName))
 
57
                        return FlagOpts[i].m_bSet;
 
58
        Quit("FlagOpt(%s) invalid", Name);
 
59
        return false;
 
60
        }
 
61
 
 
62
const char *ValueOpt(const char *Name)
 
63
        {
 
64
    MuscleContext *ctx = getMuscleContext();
 
65
    VALUE_OPT *ValueOpts = ctx->options.ValueOpts;
 
66
    int &ValueOptCount = ctx->options.ValueOptCount;
 
67
        for (int i = 0; i < ValueOptCount; ++i)
 
68
                if (!stricmp(Name, ValueOpts[i].m_pstrName))
 
69
                        return ValueOpts[i].m_pstrValue;
 
70
        Quit("ValueOpt(%s) invalid", Name);
 
71
        return 0;
 
72
        }
 
73
 
 
74
void ProcessArgVect(int argc, char *argv[])
 
75
        {
 
76
        for (int iArgIndex = 0; iArgIndex < argc; )
 
77
                {
 
78
                const char *Arg = argv[iArgIndex];
 
79
                if (Arg[0] != '-')
 
80
                        {
 
81
                        fprintf(stderr, "Command-line option \"%s\" must start with '-'\n", Arg);
 
82
                        exit(EXIT_NotStarted);
 
83
                        }
 
84
                const char *ArgName = Arg + 1;
 
85
                if (TestSetFlagOpt(ArgName))
 
86
                        {
 
87
                        ++iArgIndex;
 
88
                        continue;
 
89
                        }
 
90
                
 
91
                char *Value = 0;
 
92
                if (iArgIndex < argc - 1)
 
93
                        Value = argv[iArgIndex+1];
 
94
                if (TestSetValueOpt(ArgName, Value))
 
95
                        {
 
96
                        iArgIndex += 2;
 
97
                        continue;
 
98
                        }
 
99
                fprintf(stderr, "Invalid command line option \"%s\"\n", ArgName);
 
100
                Usage();
 
101
                exit(EXIT_NotStarted);
 
102
                }
 
103
        }
 
104
 
 
105
void ProcessArgStr(const char *ArgStr)
 
106
        {
 
107
        const int MAX_ARGS = 64;
 
108
        char *argv[MAX_ARGS];
 
109
 
 
110
        if (0 == ArgStr)
 
111
                return;
 
112
 
 
113
// Modifiable copy
 
114
        char *StrCopy = strsave(ArgStr);
 
115
 
 
116
        int argc = 0;
 
117
        bool bInArg = false;
 
118
        char *Str = StrCopy;
 
119
        while (char c = *Str)
 
120
                {
 
121
                if (isspace(c))
 
122
                        {
 
123
                        *Str = 0;
 
124
                        bInArg = false;
 
125
                        }
 
126
                else if (!bInArg)
 
127
                        {
 
128
                        bInArg = true;
 
129
                        if (argc >= MAX_ARGS)
 
130
                                Quit("Too many args in MUSCLE_CMDLINE");
 
131
                        argv[argc++] = Str;
 
132
                        }
 
133
                Str++;
 
134
                }
 
135
        ProcessArgVect(argc, argv);
 
136
        free(StrCopy);
 
137
        }
 
138
 
 
139
void ListFlagOpts()
 
140
        {
 
141
    MuscleContext *ctx = getMuscleContext();
 
142
    int &FlagOptCount = ctx->options.FlagOptCount;
 
143
    FLAG_OPT *FlagOpts = ctx->options.FlagOpts;
 
144
        for (int i = 0; i < FlagOptCount; ++i)
 
145
                Log("%s %d\n", FlagOpts[i].m_pstrName, FlagOpts[i].m_bSet);
 
146
        }