📄 auto_add_points.cpp
字号:
/* * $File: auto_add_points.cpp * $Author: Jiakai -- gy_jk@126.com * $Date: Thu Nov 27 16:27:27 2008 *//*Copyright (C) (2008) (Jiakai) <gy_jk@126.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 any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "auto_add_points.h"#include <sys/types.h>#include <dirent.h>using namespace std;typedef vector<string> Str_array;//-----------------------Function Statements---------------------------static bool list_dir(const char *dir, Str_array &files); //it only lists regular filesstatic void adjust_io(Point_info &pi);static void sort(Str_array &array, Str_array &extra_info);/*0. %common%-%var%-%id_in% %common%-%var%-%id_out%1. %common%-%id_in%-%var%-%common% %common%-%id_out%-%var%-%common%*/static int judge_type(const Str_array &array);static unsigned int get_common_left(const string &str1, const string &str2);static bool str_right_cmp(const string &right, const string &str);//---------------------------------------------------------------------bool auto_add_points(const char *dir, Point_info_array &ans){ ans.clear(); Str_array files; if (!list_dir(dir, files) || files.size() % 2 || files.size() <= 4) return false; Str_array files_orig; for (Str_array::iterator i = files.begin(); i != files.end(); i ++) { files_orig.push_back(*i); for (string::iterator j = (*i).begin(); j != (*i).end(); j ++) if (*j >= 'A' && *j <= 'Z') *j += 'a' - 'A'; } ans.resize(files.size() / 2); sort(files, files_orig); if (judge_type(files) == 0) { for (Str_array::size_type i = 0; i < ans.size(); i ++) { ans[i].input = files_orig[i * 2]; ans[i].output = files_orig[i * 2 + 1]; } } else { for (Str_array::size_type i = 0; i < ans.size(); i ++) { ans[i].input = files_orig[i]; ans[i].output = files_orig[i + ans.size()]; } } for (Str_array::size_type i = 0; i < ans.size(); i ++) adjust_io(ans[i]); return true;}int judge_type(const Str_array &array){ unsigned int maxlen = get_common_left(array[0], array[1]); string id_in(array[0].substr(maxlen)), id_out(array[1].substr(maxlen)); if (str_right_cmp(id_in, array[2]) && str_right_cmp(id_out, array[3])) return 0; return 1;}bool str_right_cmp(const string &right, const string &str){ if (right.length() > str.length()) return false; return str.substr(str.length() - right.length()) == right;}unsigned int get_common_left(const string &str1, const string &str2){ string::const_iterator p1 = str1.begin(), p2 = str2.begin(); unsigned int ans = 0; while (*p1 == *p2) { ans ++; p1 ++; p2 ++; } return ans;}void sort(Str_array &array, Str_array &extra_info){ for (Str_array::size_type i = 0; i < array.size(); i ++) for (int j = array.size() - 2; j >= int(i); j --) if (array[j] > array[j + 1]) { swap(array[j], array[j + 1]); swap(extra_info[j], extra_info[j + 1]); }}bool list_dir(const char *dirpath, Str_array &files){ files.clear(); DIR *dir = opendir(dirpath); if (dir == NULL) return false; dirent *pdirent; while ((pdirent = readdir(dir)) != NULL) { if (pdirent->d_type != DT_REG || pdirent->d_name[0] == '.') continue; files.push_back(string(pdirent->d_name)); } closedir(dir); return true;}void adjust_io(Point_info &pi){ string::size_type pos = pi.input.length() - 1; while (pos >= 0 && pi.input[pos] != '.') pos --; string tmp = pi.input.substr(pos + 1); if (tmp.find("in", 0) != string::npos) return; if (tmp.find("ou", 0) != string::npos || tmp.find("ans", 0) != string::npos) swap(pi.input, pi.output);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -