1 /******************************* -*- C++ -*- *******************************
2  *                                                                         *
3  *   file            : fusepod_util.cpp                                    *
4  *   date started    : 12 Feb 2006                                         *
5  *   author          : Keegan Carruthers-Smith                             *
6  *   email           : keegan.csmith@gmail.com                             *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  ***************************************************************************/
14 
15 #include "fusepod_util.h"
16 
17 #include <cstring>
18 #include <set>
19 
20 static std::set<const char*, ltcasestr> fusepod_strings;
21 
22 void fusepod_replace_reserved_chars (std::string & ret) {
23     for (unsigned int i = 0; i < ret.size (); i++)
24         if (ret [i] == '/' || ret [i] == '~')
25             ret [i] = '_';
26 }
27 
28 string fusepod_strip_string (const string & s) {
29     unsigned int l = s.find_first_not_of (" \t\n\r");
30     if (l == string::npos)
31         return "";
32     int r = s.find_first_not_of (" \t\n\r");
33     return s.substr (l, s.length () - r);
34 }
35 
36 string fusepod_check_string (const string & s, const string & unknown) {
37     string ret = fusepod_strip_string (s);
38     if (ret == "")
39         return unknown;
40     fusepod_replace_reserved_chars (ret);
41     return ret;
42 }
43 
44 const char * fusepod_get_string (const char * s) {
45     std::set<const char*, ltcasestr>::iterator i = fusepod_strings.find (s);
46     if (i == fusepod_strings.end ()) {
47         fusepod_strings.insert (strdup (s));
48         return fusepod_get_string (s);
49     }
50     return *i;
51 }
52 
53 vector<char*> fusepod_split_path (char * s, char c) {
54     vector<char*> nodes;
55     if (*s != c && *s != 0)
56         nodes.push_back (s);
57     while (*s) {
58         if (*s == c) {
59             *s = 0;
60             if (s[1] != 0)
61                 nodes.push_back (&(s[1]));
62         }
63         ++s;
64     }
65     return nodes;
66 }
67 
68 bool fusepod_starts_with (const char * s, const char * prefix) {
69     const char *s1 = s - 1;
70     const char *p1 = prefix - 1;
71 
72     while (*(++s1) == *(++p1) && *s1);
73 
74     return *p1 == 0;
75 }
76 
77 string fusepod_int_to_string (int i) {
78     char tmp [10];
79     i %= 1000000000;
80     sprintf (tmp, "%d", i);
81     return string (tmp);
82 }
83