擬似lsの実装
Posted feedbacks - C++
変数宣言以外はC書式(だと思う)の単純な文字列比較です。
パスリスト中のパス指定は終端セパレータを許し、"aaa/bbb"と"aaa/bbb/"は等価として扱っています(空文字列として扱うほうが楽な気もするけど、ファイルパスっぽいし)。 出力時セパレータがつくかどうかは、パスリストの出現順に依存。
重複チェックのスピードがorz
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 | #include <stdio.h>
#include <string.h>
#ifndef MAX_PATH
#define MAX_PATH 256
#endif
static const char PATH_SEPARATOR = '/';
size_t add_separator(char* szPath)
{
size_t n = strlen(szPath);
if ((n > 0) &&
(szPath[n - 1] != PATH_SEPARATOR))
{
szPath[n] = PATH_SEPARATOR; n++;
szPath[n] = '\0';
}
return n;
}
void ls(const char** pszList, const char* szPath)
{
char szParent[MAX_PATH + 1];
strcpy(szParent, szPath);
size_t nParent = add_separator(szParent);
for (int i = 0; pszList[i] != NULL; i++)
{
if ((strnicmp(pszList[i], szParent, nParent) != 0) ||
(pszList[i][nParent] == '\0'))
{
continue;
}
char szChild[MAX_PATH + 1];
strcpy(szChild, pszList[i]);
char* pe = strchr(&(szChild[nParent]), PATH_SEPARATOR);
if (pe != NULL)
{
pe[0] = '\0';
}
size_t nChild = strlen(szChild);
bool bExist = false;
for (int j = (i - 1); (j >= 0) && (bExist == false); j--)
{
bExist = ((strnicmp(pszList[j], szChild, nChild) == 0) &&
((pszList[j][nChild] == PATH_SEPARATOR) ||
(pszList[j][nChild] == '\0' ) ) );
}
if (bExist != false)
continue;
if (pe != NULL)
{
pe[0] = PATH_SEPARATOR;
pe[1] = '\0';
}
fprintf(stdout, "%s\n", &(szChild[nParent]));
}
}
int main(int argc, char* argv[])
{
const char* pszList[] = {
"aaa/bbb", "aaa/ccc", "aaa/ddd/eee", "bbb/ddd/eee", NULL };
ls(pszList, "aaa/");
fprintf(stdout, "\r\n");
ls(pszList, "aaa/ddd/");
fprintf(stdout, "\r\n");
return 0;
}
|
文字列配列中に空文字列が含まれている("aaa//bbb"等)場合に混乱するのと、単純に'/'で分割して階層構造データを作って検索するものと解答に差異が出そうなので修正。 パスリスト中の"aaa/bbb/"は、空文字列が子にあるものとして処理するようにしました。
引数のパスに空文字列を指定した場合は、ルート要素を列挙。"/"だけの場合、"/aaa"や"/bbb"といった空文字列の子要素を表示します。
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 | #include <stdio.h>
#include <string.h>
#ifndef MAX_PATH
#define MAX_PATH 256
#endif
static const char PATH_SEPARATOR = '/';
void ls(const char** pszList, const char* szPath)
{
char szParent[MAX_PATH + 1];
strcpy(szParent, szPath);
size_t nParent = strlen(szParent);
if ((nParent > 1) && (szPath[nParent - 1] != PATH_SEPARATOR))
{
szParent[nParent] = PATH_SEPARATOR; nParent++;
szParent[nParent] = '\0';
}
for (int i = 0; pszList[i] != NULL; i++)
{
if (strnicmp(pszList[i], szPath, nParent) != 0)
continue;
char szChild[MAX_PATH + 1];
strcpy(szChild, pszList[i]);
char* ps = &(szChild[nParent]);
char* pe = strchr(ps, PATH_SEPARATOR);
if (pe != NULL)
pe[0] = '\0';
size_t nChild = strlen(szChild);
bool bExist = false;
for (int j = (i - 1); (j >= 0) && (bExist == false); j--)
{
bExist = ((strnicmp(pszList[j], szChild, nChild) == 0) &&
((pszList[j][nChild] == PATH_SEPARATOR) ||
(pszList[j][nChild] == '\0' )));
}
if (bExist != false)
continue;
if (pe != NULL)
fprintf(stdout, "[%s]%c\r\n", ps, PATH_SEPARATOR);
else
fprintf(stdout, "[%s]\r\n", ps);
}
}
int main(int argc, char* argv[])
{
const char* pszList[] = {
"aaa/bbb", "aaa/ccc", "aaa/ddd/eee", "bbb/ddd/eee", NULL
};
ls(pszList, "aaa/");
fprintf(stdout, "---\r\n");
ls(pszList, "aaa/ddd/");
fprintf(stdout, "---\r\n");
return 0;
}
|
普通に。
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 | #include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <sstream>
class Search
{
private:
std::vector < std::string > & m_vs;
const std::string & m_p;
public:
Search(std::vector < std::string > & vs, const std::string & p) : m_vs(vs), m_p(p) {}
void operator () (const std::string & s)
{
if (0 == s.compare(0, m_p.size(), m_p)) {
std::string tmp = s.substr(m_p.size());
std::string::size_type st = tmp.find("/");
m_vs.push_back((std::string::npos != st) ? tmp.substr(0, st + 1) : tmp);
}
}
};
void ls(const std::vector < std::string > & path_list, const std::string & path)
{
std::vector < std::string > vs;
std::for_each(path_list.begin(), path_list.end(), Search(vs, path));
std::ostringstream oss;
copy(vs.begin(), vs.end(), std::ostream_iterator < std::string > (oss, ", "));
std::cout << oss.str().substr(0, oss.str().size() - 2) << std::endl;
}
/*
int main(void)
{
std::vector < std::string > path_list;
path_list.push_back("aaa/bbb");
path_list.push_back("aaa/ccc");
path_list.push_back("aaa/ddd/eee");
path_list.push_back("bbb/ddd/eee");
ls(path_list, "aaa/");
ls(path_list, "aaa/ddd/");
}
*/
|





ところてん
#4212()
Rating1/3=0.33
[ reply ]