Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
ogs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Yuhao Liu
ogs
Commits
37315cbe
Commit
37315cbe
authored
12 years ago
by
Lars Bilke
Browse files
Options
Downloads
Plain Diff
Merge pull request #39 from endJunction/ImproveDropFileExtension
Improve drop file extension
parents
78c28811
0838a99f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
BaseLib/FileTools.cpp
+18
-21
18 additions, 21 deletions
BaseLib/FileTools.cpp
BaseLib/FileTools.h
+12
-11
12 additions, 11 deletions
BaseLib/FileTools.h
Tests/BaseLib/TestFilePathStringManipulation.cpp
+52
-0
52 additions, 0 deletions
Tests/BaseLib/TestFilePathStringManipulation.cpp
with
82 additions
and
32 deletions
BaseLib/FileTools.cpp
+
18
−
21
View file @
37315cbe
/**
* \file
* \author Lars Bilke
* \date Apr. 2010
* \brief Filename manipulation routines implementation.
*
* \copyright
* Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*
* \file FileTools.cpp
*
* Created on 2010-04-26 by Lars Bilke
*
*/
#include
"FileTools.h"
...
...
@@ -18,7 +19,6 @@
namespace
BaseLib
{
/**
* Returns true if given file exists. From http://www.techbytes.ca/techbyte103.html
*/
...
...
@@ -32,13 +32,10 @@ bool IsFileExisting(const std::string &strFilename)
intStat
=
stat
(
strFilename
.
c_str
(),
&
stFileInfo
);
if
(
intStat
==
0
)
{
// We were able to get the file attributes
// so the file obviously exists.
blnReturn
=
true
;
}
else
{
// We were not able to get the file attributes.
// This may mean that we don't have permission to
// access the folder which contains this file. If you
...
...
@@ -46,9 +43,8 @@ bool IsFileExisting(const std::string &strFilename)
// return values of stat which will give you
// more details on why stat failed.
blnReturn
=
false
;
}
return
(
blnReturn
)
;
return
blnReturn
;
}
/**
...
...
@@ -56,8 +52,8 @@ bool IsFileExisting(const std::string &strFilename)
*/
void
truncateFile
(
std
::
string
const
&
filename
)
{
std
::
ofstream
ofs
(
filename
.
c_str
(),
std
::
ios_base
::
trunc
);
ofs
.
close
();
std
::
ofstream
ofs
(
filename
.
c_str
(),
std
::
ios_base
::
trunc
);
ofs
.
close
();
}
/** Finds the position of last file path separator.
...
...
@@ -78,22 +74,25 @@ size_t findLastDot(std::string const& path)
return
path
.
find_last_of
(
"."
);
}
/** Returns a string with file extension as found by getFileExtension()
* dropped.
*/
std
::
string
dropFileExtension
(
std
::
string
const
&
filename
)
{
// Look for dots in filename.
const
size_t
p
=
findLastDot
(
filename
);
if
(
p
==
std
::
string
::
npos
)
return
filename
;
// Check position of the last path separator.
const
size_t
s
=
findLastPathSeparator
(
filename
);
if
(
s
!=
std
::
string
::
npos
&&
p
<
s
)
return
filename
;
return
filename
.
substr
(
0
,
p
);
}
std
::
string
extractBaseName
(
std
::
string
const
&
pathname
)
{
const
size_t
p
=
findLastPathSeparator
(
pathname
);
return
pathname
.
substr
(
p
+
1
);
return
pathname
.
substr
(
p
+
1
);
}
std
::
string
extractBaseNameWithoutExtension
(
std
::
string
const
&
pathname
)
...
...
@@ -113,14 +112,14 @@ std::string getFileExtension(const std::string &path)
bool
hasFileExtension
(
std
::
string
const
&
extension
,
std
::
string
const
&
filename
)
{
std
::
string
ext
=
stringToUpper
(
extension
);
// Copy for modification.
std
::
string
ext
=
stringToUpper
(
extension
);
// Copy for modification.
std
::
string
file_ext
=
stringToUpper
(
getFileExtension
(
filename
));
return
ext
==
file_ext
;
}
std
::
string
copyPathToFileName
(
const
std
::
string
&
file_name
,
const
std
::
string
&
source
)
const
std
::
string
&
source
)
{
// check if file_name already contains a full path
const
size_t
pos
=
findLastPathSeparator
(
file_name
);
...
...
@@ -135,6 +134,4 @@ std::string extractPath(std::string const& pathname)
const
size_t
pos
=
findLastPathSeparator
(
pathname
);
return
pathname
.
substr
(
0
,
pos
+
1
);
}
}
// end namespace BaseLib
This diff is collapsed.
Click to expand it.
BaseLib/FileTools.h
+
12
−
11
View file @
37315cbe
/**
* \file
* \author Lars Bilke
* \date Apr. 2010
* \brief Filename manipulation routines.
*
* \copyright
* Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*
* \file FileTools.h
*
* Created on 2010-04-26 by Lars Bilke
*
*/
#ifndef FILETOOLS_H
#define FILETOOLS_H
#include
<string>
#include
<fstream>
#include
<string>
namespace
BaseLib
{
/**
* \brief Returns true if given file exists. From http://www.techbytes.ca/techbyte103.html
*
...
...
@@ -37,7 +36,7 @@ bool IsFileExisting(const std::string &strFilename);
*/
template
<
typename
T
>
void
writeValueBinary
(
std
::
ostream
&
out
,
T
const
&
val
)
{
out
.
write
((
const
char
*
)
&
val
,
sizeof
(
T
));
out
.
write
((
const
char
*
)
&
val
,
sizeof
(
T
));
}
/**
...
...
@@ -73,7 +72,8 @@ std::string getFileExtension(std::string const& filename);
* insensitive done by converting to upper case with the std::toupper()
* function.
*/
bool
hasFileExtension
(
std
::
string
const
&
extension
,
std
::
string
const
&
filename
);
bool
hasFileExtension
(
std
::
string
const
&
extension
,
std
::
string
const
&
filename
);
/** Returns a string with file extension as found by getFileExtension()
* dropped.
...
...
@@ -84,7 +84,8 @@ std::string dropFileExtension(std::string const& filename);
* Checks if file_name already contains a qualified path and if not copies the
* path from source.
*/
std
::
string
copyPathToFileName
(
const
std
::
string
&
file_name
,
const
std
::
string
&
source
);
std
::
string
copyPathToFileName
(
const
std
::
string
&
file_name
,
const
std
::
string
&
source
);
/**
* Extracts the path of a pathname.
...
...
This diff is collapsed.
Click to expand it.
Tests/BaseLib/TestFilePathStringManipulation.cpp
+
52
−
0
View file @
37315cbe
...
...
@@ -34,6 +34,58 @@ TEST(BaseLib, FindLastPathSeparatorUnix)
ASSERT_EQ
(
BaseLib
::
findLastPathSeparator
(
"/path/path/path/"
),
15
);
}
TEST
(
BaseLib
,
DropFileExtensionWin
)
{
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"file"
),
"file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
file"
),
"
\\
file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"path
\\
"
),
"path
\\
"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
path
\\
"
),
"
\\
path
\\
"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"path
\\
file"
),
"path
\\
file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
path
\\
file"
),
"
\\
path
\\
file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
path
\\
path
\\
file"
),
"
\\
path
\\
path
\\
file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
path
\\
path
\\
path
\\
"
),
"
\\
path
\\
path
\\
path
\\
"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"file.ext"
),
"file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
file.ext"
),
"
\\
file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"path.ext
\\
"
),
"path.ext
\\
"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
path.ext
\\
"
),
"
\\
path.ext
\\
"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"path
\\
file.ext"
),
"path
\\
file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
path
\\
file.ext"
),
"
\\
path
\\
file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
path
\\
path
\\
file.ext"
),
"
\\
path
\\
path
\\
file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
path
\\
path
\\
path.ext
\\
"
),
"
\\
path
\\
path
\\
path.ext
\\
"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"path.wrong
\\
file.ext"
),
"path.wrong
\\
file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
path.wrong
\\
file.ext"
),
"
\\
path.wrong
\\
file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
path.wrong0
\\
path.wrong
\\
file.ext"
),
"
\\
path.wrong0
\\
path.wrong
\\
file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"
\\
path.wrong0
\\
path.wrong
\\
path.ext
\\
"
),
"
\\
path.wrong0
\\
path.wrong
\\
path.ext
\\
"
);
}
TEST
(
BaseLib
,
DropFileExtensionUnix
)
{
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"file"
),
"file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/file"
),
"/file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"path/"
),
"path/"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/path/"
),
"/path/"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"path/file"
),
"path/file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/path/file"
),
"/path/file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/path/path/file"
),
"/path/path/file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/path/path/path/"
),
"/path/path/path/"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"file.ext"
),
"file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/file.ext"
),
"/file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"path.ext/"
),
"path.ext/"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/path.ext/"
),
"/path.ext/"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"path/file.ext"
),
"path/file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/path/file.ext"
),
"/path/file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/path/path/file.ext"
),
"/path/path/file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/path/path/path.ext/"
),
"/path/path/path.ext/"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"path.wrong/file.ext"
),
"path.wrong/file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/path.wrong/file.ext"
),
"/path.wrong/file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/path.wrong0/path.wrong/file.ext"
),
"/path.wrong0/path.wrong/file"
);
ASSERT_EQ
(
BaseLib
::
dropFileExtension
(
"/path.wrong0/path.wrong/path.ext/"
),
"/path.wrong0/path.wrong/path.ext/"
);
}
TEST
(
BaseLib
,
GetFileExtensionWin
)
{
ASSERT_EQ
(
BaseLib
::
getFileExtension
(
"file"
),
""
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment