misc.cpp 3.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/* 
 *  Copyright (C) 1999-2001 Bernd Gehrmann
 *                          bernd@physik.hu-berlin.de
 *
 * This program may be distributed under the terms of the Q Public
 * License as defined by Trolltech AS of Norway and appearing in the
 * file LICENSE.QPL included in the packaging of this file.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */


#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <qfile.h>
#include <qregexp.h>
#include <ctype.h>
#include <kapp.h>
#include <kconfig.h>
#include <kprocess.h>
#include <ktempfile.h>
#include "cervisiapart.h"
#include "misc.h"


void chomp(QCString *line)
{
    int pos;
    if ( (pos = line->find('\n')) != -1 )
	line->truncate(pos);  
}


QString joinLine(const QStringList &list)
{
    QString line;
    for ( QStringList::ConstIterator it = list.begin();
          it != list.end(); ++it )
	{
            line += KShellProcess::quote(*it);
            line += " ";
	}

    if (line.length() > 0)
	line.truncate(line.length()-1);

    return line;
}


// Should be replaceable by QStringList::split
QStringList splitLine(QString line, char delim)
{
    int pos;
    QStringList list;

    line = line.simplifyWhiteSpace();
    while ((pos = line.find(delim)) != -1)
	{
	    list.append(line.left(pos));
	    line = line.mid(pos+1, line.length()-pos-1);
	}
    if (!line.isEmpty())
	list.append(line);
    return list;
}


bool isValidTag(const QString &str)
{
    if (!isalpha(str[0].latin1()))
        return false;

    for (int i = 1; i < (int)str.length(); ++i)
        {
            if (!isgraph(str[i].latin1()) || QString("$,.:;@").contains(str[i]))
                return false;
        }

    return true;
}


// Gives the name (including path) of the cvs command line client
// also gives you global commands
QString cvsClient( QString sRepository )
{
    KConfig *config = CervisiaPart::config();
    config->setGroup("General");
    
    // everybody gets the -f option, unconditionally
    QString sReturn = config->readEntry("CVSPath", "cvs") + " -f";

    // see if there is a specific level set for this repository
    config->setGroup( QString("Repository-") + sRepository );
    int compressionlevel = config->readNumEntry("Compression", -1);

    // if we were left to the default value, then see what the default value should be
    if ( compressionlevel < 0 )
        {
            config->setGroup("General");
            compressionlevel = config->readNumEntry("Compression", 0);
        }

    // we don't need a command line option if there is no compression
    if (compressionlevel > 0)
        {
            sReturn += " -z";
            sReturn += QString::number(compressionlevel);
            sReturn += " ";
        }

    return sReturn;
}


// Gives the user name (real name + mail address) for the changelog entry
QString userName()
{
    char hostname[512];
    
    struct passwd *pw = ::getpwuid(getuid());
    // pw==0 => the system must be really fucked up
    if (!pw)
        return QString();

    // I guess we don't have to support users with longer host names ;-)
131
    gethostname(hostname, sizeof hostname);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    QString res = pw->pw_gecos;
    res += "  <";
    res += pw->pw_name;
    res += "@";
    res += hostname;
    res += ">";

    return res;
}


static QStringList *tempFiles = 0;

void cleanupTempFiles()
{
    if (tempFiles)
        {
            QStringList::Iterator it;
            for (it = tempFiles->begin(); it != tempFiles->end(); ++it)
#if 0
                QFile::remove(*it);
#else
                {
                    QFile f(*it);
                    f.remove();
                }
#endif
            delete tempFiles;
        }
}


QString tempFileName(const QString &suffix)
{
    if (!tempFiles)
        tempFiles = new QStringList;
    
    KTempFile f(QString::null, suffix);
    tempFiles->append(f.name());
    return f.name();
}

// Local Variables:
// c-basic-offset: 4
// End: