20130313
[gdash.git] / src / fileops / bdcffhelper.cpp
blob827c0106ad5f364b9d84b6ff8fa210019c670494
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "config.h"
19 #include <string>
20 #include <iomanip>
21 #include <stdexcept>
22 #include "fileops/bdcffhelper.hpp"
23 #include "misc/util.hpp"
24 #include "misc/printf.hpp"
26 /// Create the functor which checks if the string
27 /// has an attrib= prefix.
28 HasAttrib::HasAttrib(const std::string &attrib_)
29 : attrib(attrib_)
31 attrib+='=';
34 /// Check if the given string has the prefix.
35 bool HasAttrib::operator()(const std::string &str) const
37 return gd_str_ascii_prefix(str, attrib);
41 /// Constructor: split string given by the separator given to attrib and param.
42 /// @param str The string to split.
43 /// @param separator Separator between attrib and param; default is =.
44 AttribParam::AttribParam(const std::string &str, char separator)
46 size_t equal=str.find(separator);
47 if (equal==std::string::npos)
48 throw std::runtime_error(SPrintf("No separator in line: '%s'") % str);
49 attrib=str.substr(0, equal);
50 param=str.substr(equal+1);
54 /// Create a new formatter.
55 /// @param F The name of the output string; for example
56 /// give it "Point" if intending to write a line like "Point=1 2 DIRT"
57 BdcffFormat::BdcffFormat(const std::string &f)
58 : name(f),
59 firstparam(true)
61 os << std::setprecision(4) << std::fixed;
64 /// Get the output string.
65 /// @return The converted string.
66 std::string BdcffFormat::str() const
68 if (name.empty())
69 return os.str();
70 else
71 return name+'='+os.str();
74 /// Start a new conversion with a new name.
75 /// @param f The new name.
76 void BdcffFormat::start_new(const std::string &f)
78 name=f;
79 firstparam=true;
80 os.str(""); /* clear output */