• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • KDevelop Utility Library
 

KDevelop Utility Library

  • lib
  • util
filetemplate.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Sandy Meier <smeier@kdevelop.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "filetemplate.h"
21
22#include <tqdatetime.h>
23#include <tqfile.h>
24#include <tqfileinfo.h>
25#include <tqregexp.h>
26#include <tqtextstream.h>
27
28#include <kstandarddirs.h>
29
30#include "kdevplugin.h"
31#include "kdevproject.h"
32#include "domutil.h"
33
34
35bool FileTemplate::exists(KDevPlugin *part, const TQString &name, Policy p)
36{
37 //TQString fileName = (p == Default) ?
38 // (part->project()->projectDirectory() + "/templates/" + name) : name;
39
40 return TQFile::exists( fullPathForName(part,name,p) );
41}
42
43TQString FileTemplate::read(KDevPlugin *part, const TQString &name, Policy p)
44{
45
46 //KDevProject *project = part->project();
47 //TQString fileName = (p == Default) ? (project->projectDirectory() +
48// "/templates/" + name) : name;
49
50 return readFile(part, fullPathForName(part, name, p) );
51}
52
53TQString FileTemplate::readFile(KDevPlugin *part, const TQString &fileName)
54{
55 TQDomDocument dom;
56
57 TQFile f(fileName);
58 if (!f.open(IO_ReadOnly))
59 return TQString();
60 TQTextStream stream(&f);
61 TQString str = stream.read();
62 if (part->projectDom())
63 {
64 dom = *part->projectDom();
65 }
66
67 return makeSubstitutions( dom, str );
68}
69
70TQString FileTemplate::makeSubstitutions( TQDomDocument & dom, const TQString & text )
71{
72 TQString author = DomUtil::readEntry(dom, "/general/author");
73 TQString email = DomUtil::readEntry(dom, "/general/email");
74 TQString version = DomUtil::readEntry(dom, "/general/version");
75 TQString appname = DomUtil::readEntry(dom, "/general/projectname");
76 TQString date = TQDate::currentDate().toString();
77 TQString year = TQString::number(TQDate::currentDate().year());
78
79 TQString str = text;
80 str.replace(TQRegExp("\\$EMAIL\\$"),email);
81 str.replace(TQRegExp("\\$AUTHOR\\$"),author);
82 str.replace(TQRegExp("\\$VERSION\\$"),version);
83 str.replace(TQRegExp("\\$DATE\\$"),date);
84 str.replace(TQRegExp("\\$YEAR\\$"),year);
85 str.replace(TQRegExp("\\$APPNAME\\$"),appname);
86 str.replace(TQRegExp("\\$APPNAME\\$"),appname);
87 str.replace(TQRegExp("\\$APPNAMEUC\\$"),appname.upper());
88 str.replace(TQRegExp("\\$APPNAMELC\\$"),appname.lower());
89
90 return str;
91}
92
93
94bool FileTemplate::copy(KDevPlugin *part, const TQString &name,
95 const TQString &dest, Policy p)
96{
97 TQString text = read(part, name, p);
98
99 TQFile f(dest);
100 if (!f.open(IO_WriteOnly))
101 return false;
102
103 TQFileInfo fi(f);
104 TQString module = fi.baseName();
105 TQString basefilename = fi.baseName(true);
106 text.replace(TQRegExp("\\$MODULE\\$"),module);
107 text.replace(TQRegExp("\\$FILENAME\\$"),basefilename);
108
109 TQTextStream stream(&f);
110 stream << text;
111 f.close();
112
113 return true;
114}
115
116TQString FileTemplate::fullPathForName(KDevPlugin *part, const TQString &name,
117 Policy p) {
118 // if Policy is not default, full path is just the name
119 if (p!=Default) return name;
120
121 TQString fileName;
122 // first try project-specific
123 if (part->project())
124 {
125 fileName = (part->project()->projectDirectory() + "/templates/" + name);
126 if (TQFile::exists(fileName)) return fileName;
127 }
128
129 // next try global
130 TQString globalName = ::locate("data", "kdevfilecreate/file-templates/" + name);
131 return globalName.isNull() ? fileName : globalName;
132}
DomUtil::readEntry
static TQString readEntry(const TQDomDocument &doc, const TQString &path, const TQString &defaultEntry=TQString())
Reads a string entry.
Definition domutil.cpp:43
FileTemplate::read
static TQString read(KDevPlugin *part, const TQString &name, Policy p=Default)
Reads a template with the given name (e.g.
Definition filetemplate.cpp:43
FileTemplate::copy
static bool copy(KDevPlugin *part, const TQString &name, const TQString &dest, Policy p=Default)
Copies a file template with the given name to the file with the name dest and - while copying - perfo...
Definition filetemplate.cpp:94
FileTemplate::Policy
Policy
Policy of finding file templates.
Definition filetemplate.h:41
FileTemplate::Default
@ Default
Checks for templates in project and also for global filecreate templates.
Definition filetemplate.h:42
FileTemplate::readFile
static TQString readFile(KDevPlugin *part, const TQString &fileName)
Reads a template with the given URL and makes variable substitutions (like $AUTHOR$ etc....
Definition filetemplate.cpp:53
FileTemplate::exists
static bool exists(KDevPlugin *part, const TQString &name, Policy p=Default)
Definition filetemplate.cpp:35
FileTemplate::makeSubstitutions
static TQString makeSubstitutions(TQDomDocument &dom, const TQString &text)
Makes variable substitutions on a text, based on a specified TQDomDocument describing a KDevelop proj...
Definition filetemplate.cpp:70
FileTemplate::fullPathForName
static TQString fullPathForName(KDevPlugin *part, const TQString &name, Policy p=Default)
Translates a template name into a full path, or suggests a full path for the template in the project ...
Definition filetemplate.cpp:116
domutil.h
Utility functions to operate on DOM.
filetemplate.h
FileTemplate class with utility methods to work with file templates.

KDevelop Utility Library

Skip menu "KDevelop Utility Library"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

KDevelop Utility Library

Skip menu "KDevelop Utility Library"
  • buildtools
  •   lib
  •     base
  •     parsers
  •       autotools
  •       qmake
  •     widgets
  •   api
  • languages
  •   lib
  •     debugger
  •     designer_integration
  •     interfaces
  • lib
  •   catalog
  •   interfaces
  •     extensions
  •     external
  •     extras
  •   util
  •   widgets
  •     propeditor
  • parts
  •   documentation
  •     interfaces
  • src
  •   profileengine
  •     lib
Generated for KDevelop Utility Library by doxygen 1.9.8
This website is maintained by Timothy Pearson.