• 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 
35 bool 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 
43 TQString 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 
53 TQString FileTemplate::readFile(KDevPlugin *part, const TQString &fileName)
54 {
55  TQDomDocument &dom = *part->projectDom();
56 
57  TQFile f(fileName);
58  if (!f.open(IO_ReadOnly))
59  return TQString();
60  TQTextStream stream(&f);
61  TQString str = stream.read();
62 
63  return makeSubstitutions( dom, str );
64 }
65 
66 TQString FileTemplate::makeSubstitutions( TQDomDocument & dom, const TQString & text )
67 {
68  TQString author = DomUtil::readEntry(dom, "/general/author");
69  TQString email = DomUtil::readEntry(dom, "/general/email");
70  TQString version = DomUtil::readEntry(dom, "/general/version");
71  TQString appname = DomUtil::readEntry(dom, "/general/projectname");
72  TQString date = TQDate::currentDate().toString();
73  TQString year = TQString::number(TQDate::currentDate().year());
74 
75  TQString str = text;
76  str.replace(TQRegExp("\\$EMAIL\\$"),email);
77  str.replace(TQRegExp("\\$AUTHOR\\$"),author);
78  str.replace(TQRegExp("\\$VERSION\\$"),version);
79  str.replace(TQRegExp("\\$DATE\\$"),date);
80  str.replace(TQRegExp("\\$YEAR\\$"),year);
81  str.replace(TQRegExp("\\$APPNAME\\$"),appname);
82  str.replace(TQRegExp("\\$APPNAME\\$"),appname);
83  str.replace(TQRegExp("\\$APPNAMEUC\\$"),appname.upper());
84  str.replace(TQRegExp("\\$APPNAMELC\\$"),appname.lower());
85 
86  return str;
87 }
88 
89 
90 bool FileTemplate::copy(KDevPlugin *part, const TQString &name,
91  const TQString &dest, Policy p)
92 {
93  TQString text = read(part, name, p);
94 
95  TQFile f(dest);
96  if (!f.open(IO_WriteOnly))
97  return false;
98 
99  TQFileInfo fi(f);
100  TQString module = fi.baseName();
101  TQString basefilename = fi.baseName(true);
102  text.replace(TQRegExp("\\$MODULE\\$"),module);
103  text.replace(TQRegExp("\\$FILENAME\\$"),basefilename);
104 
105  TQTextStream stream(&f);
106  stream << text;
107  f.close();
108 
109  return true;
110 }
111 
112 TQString FileTemplate::fullPathForName(KDevPlugin *part, const TQString &name,
113  Policy p) {
114  // if Policy is not default, full path is just the name
115  if (p!=Default) return name;
116 
117  TQString fileName;
118  // first try project-specific
119  if (part->project())
120  {
121  fileName = (part->project()->projectDirectory() + "/templates/" + name);
122  if (TQFile::exists(fileName)) return fileName;
123  }
124 
125  // next try global
126  TQString globalName = ::locate("data", "kdevfilecreate/file-templates/" + name);
127  return globalName.isNull() ? fileName : globalName;
128 }
filetemplate.h
FileTemplate class with utility methods to work with file templates.
FileTemplate::Default
Checks for templates in project and also for global filecreate templates.
Definition: filetemplate.h:42
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::Policy
Policy
Policy of finding file templates.
Definition: filetemplate.h:41
domutil.h
Utility functions to operate on DOM.
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::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:112
DomUtil::readEntry
static TQString readEntry(const TQDomDocument &doc, const TQString &path, const TQString &defaultEntry=TQString())
Reads a string entry.
Definition: domutil.cpp:43
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:66
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:90
FileTemplate::exists
static bool exists(KDevPlugin *part, const TQString &name, Policy p=Default)
Definition: filetemplate.cpp:35

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.8.13
This website is maintained by Timothy Pearson.