File: keyword.cpp

package info (click to toggle)
boinc 7.14.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,132 kB
  • sloc: cpp: 163,589; php: 113,173; ansic: 49,284; pascal: 35,620; xml: 17,864; java: 13,521; python: 6,551; sh: 4,082; perl: 1,843; makefile: 1,796; objc: 1,543; sql: 959; csh: 126; lisp: 47
file content (159 lines) | stat: -rw-r--r-- 4,254 bytes parent folder | download | duplicates (3)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2017 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.

// utility functions for keywords

#include <stdio.h>

#include "parse.h"
#include "keyword.h"

int KEYWORD::parse(XML_PARSER& xp) {
    while (!xp.get_tag()) {
        if (xp.match_tag("/keyword")) {
            return 0;
        }
        if (xp.parse_int("id", id)) continue;
        if (xp.parse_string("name", name)) continue;
        if (xp.parse_string("description", description)) continue;
        if (xp.parse_int("parent", parent)) continue;
        if (xp.parse_int("level", level)) continue;
        if (xp.parse_int("category", category)) continue;
    }
    return ERR_XML_PARSE;
}

#ifndef _USING_FCGI_
void KEYWORD::write_xml(MIOFILE& mf) {
    mf.printf(
        "<keyword>\n"
        "   <name>%s</name>\n"
        "   <description>%s</description>\n"
        "   <parent>%d</parent>\n"
        "   <level>%d</level>\n"
        "   <category>%d</category>\n"
        "</keyword>\n",
        name.c_str(), description.c_str(), parent, level, category
    );
}
#endif

int KEYWORDS::parse(XML_PARSER& xp) {
    while (!xp.get_tag()) {
        if (xp.match_tag("/keywords")) {
            return 0;
        }
        if (xp.match_tag("keyword")) {
            KEYWORD kw;
            int retval = kw.parse(xp);
            if (retval) {
                printf("KEYWORD parse fail: %d\n", retval);
                return retval;
            }
            keywords[kw.id] = kw;
            continue;
        }
    }
    return ERR_XML_PARSE;
}

int USER_KEYWORDS::parse(XML_PARSER& xp) {
    clear();
    int x;
    while (!xp.get_tag()) {
        if (xp.match_tag("/user_keywords")) {
            return 0;
        }
        if (xp.parse_int("yes", x)) {
            yes.push_back(x);
        } else if (xp.parse_int("no", x)) {
            no.push_back(x);
        }
    }
    return ERR_XML_PARSE;
}

void USER_KEYWORDS::write(FILE* f) {
    if (empty()) {
        return;
    }
    unsigned int i;
    fprintf(f, "<user_keywords>\n");
    for (i=0; i<yes.size(); i++) {
        fprintf(f, "   <yes>%d</yes>\n", yes[i]);
    }
    for (i=0; i<no.size(); i++) {
        fprintf(f, "   <no>%d</no>\n", no[i]);
    }
    fprintf(f, "</user_keywords>\n");
}

void JOB_KEYWORD_IDS::parse_str(char* buf) {
    char* p = strtok(buf, " ");
    if (!p) return;
    ids.push_back(atoi(p));
    while (true) {
        p = strtok(NULL, " ");
        if (!p) break;
        ids.push_back(atoi(p));
    }
}

#ifndef _USING_FCGI_

// write list of full keywords
//
void JOB_KEYWORD_IDS::write_xml_text(MIOFILE& mf, KEYWORDS& k) {
    mf.printf("<job_keywords>\n");
    for (unsigned int i=0; i<ids.size(); i++) {
        int id = ids[i];
        k.get(id).write_xml(mf);
    }
    mf.printf("</job_keywords>\n");
}

// write 1-line list of keyword IDs
//
void JOB_KEYWORD_IDS::write_xml_num(MIOFILE& out) {
    bool first = true;
    out.printf("    <job_keyword_ids>");
    for (unsigned int i=0; i<ids.size(); i++) {
        if (first) {
            out.printf("%d", ids[i]);
            first = false;
        } else {
            out.printf(", %d", ids[i]);
        }
    }
    out.printf("</job_keyword_ids>\n");
}
#endif

int JOB_KEYWORDS::parse(XML_PARSER& xp) {
    while (!xp.get_tag()) {
        if (xp.match_tag("/job_keywords")) {
            return 0;
        }
        if (xp.match_tag("keyword")) {
            KEYWORD kw;
            int retval = kw.parse(xp);
            if (retval) return retval;
            keywords.push_back(kw);
        }
    }
    return ERR_XML_PARSE;
}