Statistics
| Branch: | Revision:

root / src / common / fnamelisttokenizer.cc @ 81ad8b66

History | View | Annotate | Download (2.06 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  CSTRTOKENIZER2.CC - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
#include <assert.h>
18
#include <stdlib.h>
19
#include <string.h>
20
#include "fnamelisttokenizer.h"
21
22
USING_NAMESPACE
23
24
25
FilenamesListTokenizer::FilenamesListTokenizer(const char *s)
26
{
27
    if (!s)
28
        s = "";
29
    str = new char[strlen(s)+1];
30
    strcpy(str,s);
31
    rest = str;
32
}
33
34
FilenamesListTokenizer::~FilenamesListTokenizer()
35
{
36
    delete [] str;
37
}
38
39
bool FilenamesListTokenizer::hasMoreTokens()
40
{
41
    while (*rest==' ' || *rest=='\t') rest++;
42
    return *rest != '\0';
43
}
44
45
const char *FilenamesListTokenizer::nextToken()
46
{
47
    // we want to write "s" instead of "rest"
48
    char *&s = rest;
49
50
    // skip spaces before token
51
    while (*s==' ' || *s=='\t') s++;
52
    if (!*s)
53
        return NULL;
54
55
    char *token;
56
    if (*s=='"')
57
    {
58
        // parse quoted string
59
        token = s+1;
60
        s++;
61
        // try to find end-quote, or end of the string
62
        while (*s && *s!='"') s++;
63
        // terminate quoted string with zero, overwriting close quote (if it was there)
64
        if (*s)
65
            *s++ = 0;
66
    }
67
    else
68
    {
69
        // parse unquoted string
70
        token = s;
71
        // try find end of string
72
        while (*s && *s!=' ' && *s!='\t') s++;
73
        // terminate string with zero (if we are not already at end of the line)
74
        if (*s)
75
            *s++ = 0;
76
    }
77
    return token;
78
}
79
80
std::vector<std::string> FilenamesListTokenizer::asVector()
81
{
82
    const char *s;
83
    std::vector<std::string> v;
84
    while ((s=nextToken())!=NULL)
85
        v.push_back(std::string(s));
86
    return v;
87
}
88