File: kfilereplacelib.cpp

package info (click to toggle)
kdewebdev 1%3A3.3.2-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 33,200 kB
  • ctags: 13,047
  • sloc: cpp: 165,029; sh: 9,033; perl: 3,315; makefile: 1,618; xml: 497; ansic: 83
file content (126 lines) | stat: -rw-r--r-- 4,466 bytes parent folder | download
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
/***************************************************************************
                           kfilereplacelib.cpp  -  File library, derived from filelib.cpp
                                      -------------------
    begin                : lun mai  3 20:19:52 CEST 1999

    copyright            : (C) 1999 by Franois Dupoux
                                (C) 2003 Andras Mantia <amantia@kde.org>
                                (C) 2004 Emiliano Gulmini <emi_barbarossa@yahoo.it>
    email                : dupoux@dupoux.com
                               
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#include <errno.h>

#include "kfilereplacelib.h"

/**
 Create the text with a size in Bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes from a 64 bits number
 Parameters::.....* qwSize: 64 bits number of the size in bytes
 Return values:...* formatted text size
*/
QString KFileReplaceLib::formatSize(QWORD qwSize)
{
  QString strSize;
  double dSize;

  QWORD llKiloB = ((QWORD) 1024);
  QWORD llMegaB = ((QWORD) 1024) * ((QWORD) 1024);
  QWORD llGigaB = ((QWORD) 1024) * ((QWORD) 1024) * ((QWORD) 1024);
  QWORD llTeraB = ((QWORD) 1024) * ((QWORD) 1024) * ((QWORD) 1024) * ((QWORD) 1024);

  if (qwSize < llKiloB) // In Bytes
    {
      strSize = i18n("%n byte", "%n bytes", (unsigned long)qwSize);
    }
  else if (qwSize < llMegaB) // In KiloBytes
    {
      dSize = ((double) qwSize) / ((double) 1024.0);
      strSize.sprintf(i18n("%.2f KB"), (float) dSize);
    }
  else if (qwSize < llGigaB) // In MegaBytes
    {
      dSize = ((double) qwSize) / ((double) (1024.0 * 1024.0));
      strSize.sprintf(i18n("%.2f MB"), (float) dSize);
    }
  else if (qwSize < llTeraB)// In GigaBytes
    {
      dSize = ((double) qwSize) / ((double) (1024.0 * 1024.0 * 1024.0));
      strSize.sprintf(i18n("%.2f GB"), (float) dSize);
    }
  else // In TeraBytes
    {
      dSize = ((double) qwSize) / ((double) (1024.0 * 1024.0 * 1024.0 * 1024.0));
      strSize.sprintf(i18n("%.2f TB"), (float) dSize);
    }

  return strSize;
}

/**
 Format a path, from a path and a filename, or another sub-path (avoid double '/' risks)
 Parameters::.....* szBasePath: fist path (can be "/" if root, or "/usr/bin/" or "/usr/bin" for example)
 .................* szFilename: second path (can be "/doc/html/", or "doc/html/" or "doc/html/index.html" for example)
 Return values:...* Full valid path (without double "/")
*/
QString KFileReplaceLib::formatFullPath(const QString& szBasePath, const QString &szFilename)
{
  QString strFullPath = szBasePath;
  QString fileName = szFilename;

  if (fileName.startsWith("/")) // skip beginning '/'
    fileName = fileName.remove(0,1);

  if (strFullPath.endsWith("/"))
    strFullPath.append(fileName);
  else 
    strFullPath.append("/"+fileName);
  
  return strFullPath;
}

/**
 Add an extension to a filename, or a filepath
 Parameters::.....* strFilename: filename or filepath (it can have already the extension)
 .................* szExtension: extension to add without "." (ex: "html", "kfr")
 Return values:...* Filename / Filepath with the extension
*/
QString KFileReplaceLib::addFilenameExtension(const QString& strFilename, const QString& szExtension)
{
  QString strFullExtension;
  QString fileName = strFilename;

  strFullExtension = ".";
  strFullExtension.append(szExtension);

  // filename cannot contain ".ext" ==> Add it
  if(fileName.length() <= strFullExtension.length())
   fileName.append(strFullExtension);
  else // filename can contain ".ext"
  {
   if (fileName.right(strFullExtension.length()) != strFullExtension)
    fileName.append(strFullExtension);
  }

  return fileName;
}