source: trunk/lib/utility.cc @ 175

Last change on this file since 175 was 175, checked in by Peter Johansson, 16 years ago

fixes #53

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1// $Id: utility.cc 175 2006-09-02 03:22:52Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  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  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "utility.h"
25
26#include <fstream>
27#include <iostream> // remove this when 'blame' is removed
28#include <sstream>
29#include <stdexcept>
30#include <string>
31#include <sys/param.h>
32#include <unistd.h>
33
34namespace theplu{
35namespace svndigest{
36
37  int access_rights(const std::string& path, const std::string& bits)
38  {
39    if (access(path.c_str(),F_OK)) {
40      throw std::runtime_error(std::string("access_rights: ") + path +
41                               "' does not exist.");
42    }
43    int mode=0;
44    for (u_int i=0; i<bits.length(); ++i)
45      switch (bits[i]) {
46          case 'r':
47            mode|=R_OK;
48            break;
49          case 'w':
50            mode|=W_OK;
51            break;
52          case 'x':
53            mode|=X_OK;
54            break;
55      }
56    return access(path.c_str(),mode);
57  }
58
59
60  void anchor(std::ostream& os, const std::string& href, 
61              const std::string& name, u_int level, 
62              const std::string& title)
63  {
64    os << "<a title=\"" << title << "\" href=\"" ;
65    for (size_t i=0; i<level; ++i)
66      os << "../";
67    os << href << "\">" << name << "</a>";
68  }
69
70
71  std::string file_name(const std::string& full_path)
72  {
73    std::stringstream ss(full_path);
74    std::string name;
75    while (getline(ss,name,'/')) {}
76    return name;
77  }
78
79
80  void print_css(std::ostream& s)
81  {
82    s << "body {\n";
83    s << " background: #fff; \n";
84    s << " color: #000; \n";
85    s << " margin: 0px; \n";
86    s << " padding: 0; \n";
87    s << "} \n";
88    s << "\n";
89    s << "#menu {\n";
90    s << " background: #eee;\n";
91    s << " width: 100%;\n";
92    s << " margin: 0px;\n";
93    s << " padding: 0px;\n";
94    s << "}\n\n";
95    s << "#menu ul\n";
96    s << "{ \n";
97    s << "padding: 0px;\n";
98    s << "margin: 0px;list-style-type: none; text-align: center;"
99      << "border-bottom: 1px solid black;}\n";
100    s << "#menu ul li { display: inline; border-right: 1px solid black;}\n";
101    s << "#menu ul li a {text-decoration: none; padding-right: 1em;" 
102      << "padding-left: 1em; margin: 0px;}\n";
103    s << "#menu ul li a:hover{ color: #000; background: #ddd;}\n";
104    s << "\n";
105    s << "#main {\n";
106    s << " margin: 10px; \n";
107    s << "}\n";
108    s << "\n";
109    s << "body, th, td {\n";
110    s << " font: normal 13px verdana,arial,'Bitstream Vera Sans',"
111      << "helvetica,sans-serif;\n";
112    s << "}\n";
113    s << ":link, :visited {\n";
114    s << " text-decoration: none;\n";
115    s << " color: #b00;\n";
116    s << "}\n";
117    s << "\n";
118    s << "table.listings {\n";
119    s << " clear: both;\n";
120    s << " border-bottom: 1px solid #d7d7d7;\n";
121    s << " border-collapse: collapse;\n";
122    s << " border-spacing: 0;\n";
123    s << " margin-top: 1em;\n";
124    s << " width: 100%;\n";
125    s << "}\n";
126    s << "\n";
127    s << "table.listings th {\n";
128    s << " text-align: left;\n";
129    s << " padding: 0 1em .1em 0;\n";
130    s << " font-size: 12px\n";
131    s << "}\n";
132    s << "table.listings thead { background: #f7f7f0 }\n";
133    s << "table.listings thead th {\n";
134    s << " border: 1px solid #d7d7d7;\n";
135    s << " border-bottom-color: #999;\n";
136    s << " font-size: 11px;\n";
137    s << " font-wheight: bold;\n";
138    s << " padding: 2px .5em;\n";
139    s << " vertical-align: bottom;\n";
140    s << "}\n";
141    s << "\n";
142    s << "table.listings tbody td a:hover, table.listing tbody th a:hover {\n";
143    s << " background-color: transparent;\n";
144    s << "}\n";
145    s << "\n";
146    s << "table.listings tbody td, table.listing tbody th {\n";
147    s << " border: 1px dotted #ddd;\n";
148    s << " padding: .33em .5em;\n";
149    s << " vertical-align: top;\n";
150    s << "}\n";
151    s << "\n";
152    s << "table.listings tbody td a:hover, table.listing tbody th a:hover {\n";
153    s << " background-color: transparent;\n";
154    s << "}\n";
155    s << "table.listings tbody tr { border-top: 1px solid #ddd }\n";
156    s << "table.listings tbody tr.light { background-color: #fcfcfc }\n";
157    s << "table.listings tbody tr.dark { background-color: #f7f7f7 }\n";
158    s << "table.listings tbody tr:hover { background: #eed }\n";
159    s << "table.listings tbody td { text-align: left }\n";
160    s << "\n";
161    s << ".sep { color: #666}\n";
162    s << "\n";
163    s << "\n";
164  }
165
166
167  std::string pwd(void)
168  {
169    char buffer[MAXPATHLEN];
170    if (!getcwd(buffer, MAXPATHLEN))
171      throw std::runtime_error("Failed to get current working directory");
172    return std::string(buffer);
173  }
174
175}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.