Date:2015-04-26 21:13:03 (5 years 1 month 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
1818 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
1919 ***************************************************************************/
2020
21#include <sstream>
2221#include <sys/stat.h>
2322#include <sys/types.h>
2423#include <dirent.h>
...... 
391390        title = title.substr(0, pos);
392391    }
393392
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);
402394    INFO("Adding link: '%s'\n", linkpath.c_str());
403395
404396    string dirPath = path;
...... 
541533        return false;
542534    }
543535
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);
552537
553538    if (rename(file.c_str(), newFileName.c_str())) {
554539        WARNING("Link file move from '%s' to '%s' failed: %s\n",
src/utilities.cpp
3232#include <dirent.h>
3333#include <fstream>
3434#include <iostream>
35#include <sstream>
3536#include <strings.h>
3637#include <unistd.h>
3738
...... 
160161    return access(file.c_str(), F_OK) == 0;
161162}
162163
164string 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
163177int constrain(int x, int imin, int imax) {
164178    return min(imax, max(imin, x));
165179}
src/utilities.h
8080
8181bool fileExists(const std::string &file);
8282
83/**
84 * Constructs a non-existing path in a given directory based on the given name.
85 */
86std::string uniquePath(std::string const& dir, std::string const& name);
87
8388int constrain(int x, int imin, int imax);
8489
8590int evalIntConf(ConfIntHash& hash, const std::string &key, int def, int imin, int imax);

Archive Download the corresponding diff file



interactive