Date: | 2015-04-26 21:13:03 (8 years 7 months ago) |
---|---|
Author: | Maarten ter Huurne |
Commit: | 1b54483f21b359721a276b0a74c92deb4829c533 |
Message: | Put code to construct unique path into new utlity function
uniquePath() Two almost identical versions existed of this code. I generalized the code by taking out the knowledge of sections. No other uses of this code are planned, but it is easier to review in isolation now. |
Files: |
src/menu.cpp (3 diffs) src/utilities.cpp (2 diffs) src/utilities.h (1 diff) |
Change Details
src/menu.cpp | ||
---|---|---|
18 | 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | 19 | ***************************************************************************/ |
20 | 20 | |
21 | #include <sstream> | |
22 | 21 | #include <sys/stat.h> |
23 | 22 | #include <sys/types.h> |
24 | 23 | #include <dirent.h> |
... | ... | |
391 | 390 | title = title.substr(0, pos); |
392 | 391 | } |
393 | 392 | |
394 | string linkpath = sectionDir + "/" + title; | |
395 | int x = 2; | |
396 | while (fileExists(linkpath)) { | |
397 | stringstream ss; | |
398 | ss << sectionDir << '/' << title << x; | |
399 | ss >> linkpath; | |
400 | x++; | |
401 | } | |
393 | string linkpath = uniquePath(sectionDir, title); | |
402 | 394 | INFO("Adding link: '%s'\n", linkpath.c_str()); |
403 | 395 | |
404 | 396 | string dirPath = path; |
... | ... | |
541 | 533 | return false; |
542 | 534 | } |
543 | 535 | |
544 | string newFileName = sectionDir + "/" + linkTitle; | |
545 | unsigned int x = 2; | |
546 | while (fileExists(newFileName)) { | |
547 | string id = ""; | |
548 | stringstream ss; ss << x; ss >> id; | |
549 | newFileName = sectionDir + "/" + linkTitle + id; | |
550 | x++; | |
551 | } | |
536 | string newFileName = uniquePath(sectionDir, linkTitle); | |
552 | 537 | |
553 | 538 | if (rename(file.c_str(), newFileName.c_str())) { |
554 | 539 | WARNING("Link file move from '%s' to '%s' failed: %s\n", |
src/utilities.cpp | ||
---|---|---|
32 | 32 | #include <dirent.h> |
33 | 33 | #include <fstream> |
34 | 34 | #include <iostream> |
35 | #include <sstream> | |
35 | 36 | #include <strings.h> |
36 | 37 | #include <unistd.h> |
37 | 38 | |
... | ... | |
160 | 161 | return access(file.c_str(), F_OK) == 0; |
161 | 162 | } |
162 | 163 | |
164 | string uniquePath(string const& dir, string const& name) | |
165 | { | |
166 | string path = dir + "/" + name; | |
167 | unsigned int x = 2; | |
168 | while (fileExists(path)) { | |
169 | stringstream ss; | |
170 | ss << dir << '/' << name << x; | |
171 | ss >> path; | |
172 | x++; | |
173 | } | |
174 | return path; | |
175 | } | |
176 | ||
163 | 177 | int constrain(int x, int imin, int imax) { |
164 | 178 | return min(imax, max(imin, x)); |
165 | 179 | } |
src/utilities.h | ||
---|---|---|
80 | 80 | |
81 | 81 | bool fileExists(const std::string &file); |
82 | 82 | |
83 | /** | |
84 | * Constructs a non-existing path in a given directory based on the given name. | |
85 | */ | |
86 | std::string uniquePath(std::string const& dir, std::string const& name); | |
87 | ||
83 | 88 | int constrain(int x, int imin, int imax); |
84 | 89 | |
85 | 90 | int evalIntConf(ConfIntHash& hash, const std::string &key, int def, int imin, int imax); |
Branches:
install_locations
master
opkrun
packages