Exemplo n.º 1
0
    void
    StdOutputRedirector::init(const Arguments & theArguments) {
        if (theArguments.haveOption("--std-logfile")) {
            const char * myEnv = ::getenv(LOG_WRAPAROUND_FILESIZE);
            if (myEnv) {
                string mylogFileSize(myEnv);
                _myMaximumFileSize = asl::as<long>(mylogFileSize);
            }
            myEnv = ::getenv(LOG_CREATE_ON_EACH_RUN);
            if (myEnv) {
                string myTmpStr(myEnv);
                _myLogInOneFileFlag = !(strcmp(toLowerCase(myTmpStr).c_str(), "true") == 0);
            }
            myEnv = ::getenv(LOG_REMOVE_OLD_ARCHIVE);
            if (myEnv) {
                string myTmpStr(myEnv);
                _myRemoveOldArchiveFlag = (strcmp(toLowerCase(myTmpStr).c_str(), "true") == 0);
            }

            myEnv = ::getenv(LOG_WRAPAROUND_CHECK_SEC);
            if (myEnv) {
                string myTmpStr(myEnv);
                _myFileSizeCheckFrequInSec = asl::as<long>(myTmpStr);
            }

            std::string myFilenameWithTimestamp = expandString(theArguments.getOptionArgument("--std-logfile"),
                                     _myOutputFilename);
            if (!_myLogInOneFileFlag) {
                _myOutputFilename = myFilenameWithTimestamp;
            }
            // for syncing c like stderr & c++ cerr
            // default is true, not syncing is supposted to be faster, so maybe we should disable it
            //ios_base::sync_with_stdio(false);

            redirect();

            // write a timestamp
            cout <<  ourAppStartMessage << _myOutputFilename << endl;
            cout << "Timestamp: " << getCurrentTimeString() << endl;
            cout << "---------" << endl;

            // remove all but latest archives
            if (_myRemoveOldArchiveFlag) {
                removeoldArchives();
            }

        }
    }
Exemplo n.º 2
0
int main(int argc, char *argv[]) {

    Arguments::AllowedOption myOptions[] = {
        {"-o", "%s"},
        {"-p", ""},
        {"-d", ""},
        {"-s", ""},
        {"-l", ""},
        {"-e", "%s"},
        {"-b", "%s"},
        {"-E", "%s"},
        {"-B", "%s"},
        {"-m", "%s"},
        {"-O", "%s"},
        {"-k", "%s"},
        {"-K", "%s"},
        {"-x", ""},
        {"", ""}
    };

    try {
        Arguments myArguments;
        myArguments.addAllowedOptions(myOptions);

        if (myArguments.parse(argc,argv) && myArguments.getCount()>=0) {
            string keywords;
            string begin_keywords;
            string end_keywords;
            string dictionary;
            int minOverlap = 2;
            int maxOverlap = 3;
            if (myArguments.haveOption("-o")) {
                minOverlap = asl::as<int>(myArguments.getOptionArgument("-o"));
            }
            if (myArguments.haveOption("-O")) {
                maxOverlap = asl::as<int>(myArguments.getOptionArgument("-O"));
            }
            if (myArguments.haveOption("-m")) {
                maxResultLen = asl::as<int>(myArguments.getOptionArgument("-m"));
            }
            if (myArguments.haveOption("-K")) {
                keywords = readFile(myArguments.getOptionArgument("-K"), keywords);
            }
            if (myArguments.haveOption("-k")) {
                if (keywords.size()) keywords.append("\n");
                keywords.append(myArguments.getOptionArgument("-k"));
            }
            if (myArguments.haveOption("-B")) {
                if (!readFile(myArguments.getOptionArgument("-B"), begin_keywords)) {
                    cerr << "Could not read begin keywords file '" << myArguments.getOptionArgument("-B") << "'" << endl;
                    exit(1);
                }
            }
            if (myArguments.haveOption("-b")) {
                if (begin_keywords.size()) begin_keywords.append("\n");
                begin_keywords.append(myArguments.getOptionArgument("-b"));
            }
             if (myArguments.haveOption("-E")) {
                if (!readFile(myArguments.getOptionArgument("-E"), end_keywords)) {
                    cerr << "Could not read end keywords file '" << myArguments.getOptionArgument("-E") << "'" << endl;
                    exit(1);
                }
            }
            if (myArguments.haveOption("-e")) {
                if (end_keywords.size()) end_keywords.append("\n");
                end_keywords.append(myArguments.getOptionArgument("-e"));
            }
            shortForm = myArguments.haveOption("-s");
            std::vector<string> myKeyWordList = splitString(keywords, " \n\r\t,:");
            std::vector<string> myBeginKeyWordList = splitString(begin_keywords, " \n\r\t,:");
            std::vector<string> myEndKeyWordList = splitString(end_keywords, " \n\r\t,:");
            std::vector<string> myDictWordList;
            for (int i = 0; i < myArguments.getCount();++i) {
                cerr << "reading dictionary file '" << myArguments.getArgument(i) << "'" << endl;
                if (readFile(myArguments.getArgument(i), dictionary)) {
                    std::vector<string> myNewDict = splitString(dictionary);
                    myDictWordList.insert(myDictWordList.end(), myNewDict.begin(), myNewDict.end());
                } else {
                    cerr << "Could not read dictionary file '" << myArguments.getArgument(i) << "'" << endl;
                    exit(1);
                }
            }

            if (myArguments.haveOption("-l")) {
                cerr << "Converting dictionary and keywords to lower case" << endl;
                for (unsigned int i = 0; i < myKeyWordList.size();++i) {
                    myKeyWordList[i] = toLowerCase(myKeyWordList[i]);
                }
                for (unsigned int i = 0; i < myBeginKeyWordList.size();++i) {
                    myBeginKeyWordList[i] = toLowerCase(myBeginKeyWordList[i]);
                }
                for (unsigned int i = 0; i < myEndKeyWordList.size();++i) {
                    myEndKeyWordList[i] = toLowerCase(myEndKeyWordList[i]);
                }
                for (unsigned int i = 0; i < myDictWordList.size();++i) {
                    myDictWordList[i] = toLowerCase(myDictWordList[i]);
                }
            }
            if (myArguments.haveOption("-p")) {
                cout << "Keywords:" << endl;
                for (unsigned int i = 0; i < myKeyWordList.size();++i) {
                    cout << "'"<<myKeyWordList[i]<<"'" << endl;
                }
                cout << "Begin Keywords:" << endl;
                for (unsigned int i = 0; i < myBeginKeyWordList.size();++i) {
                    cout << "'"<<myBeginKeyWordList[i]<<"'" << endl;
                }
                cout << "End Keywords:" << endl;
                for (unsigned int i = 0; i < myEndKeyWordList.size();++i) {
                    cout << "'"<<myEndKeyWordList[i]<<"'" << endl;
                }
            }
            if (myArguments.haveOption("-d")) {
                cout << "Dictionary:" << endl;
                for (unsigned int i = 0; i < myDictWordList.size();++i) {
                    cout << "'"<<myDictWordList[i]<<"'" << endl;
                }
            }
            if (myArguments.haveOption("-x")) {
                cout << "Begin Keywords:" << endl;
                for (unsigned int i = 0; i < myBeginKeyWordList.size();++i) {
                    for (unsigned int j = 0; j < myEndKeyWordList.size();++j) {
                        cout << myBeginKeyWordList[i]<<myEndKeyWordList[j] << endl;
                    }
                }
                return 0;
            }
            for (unsigned int i = 0; i < myKeyWordList.size();++i) {
                const string & myKeyWord = myKeyWordList[i];
                for (int l = asl::minimum(myKeyWord.size(), (size_t)maxOverlap); l>=minOverlap; --l) {
                    std::string myFront = myKeyWord.substr(0, l);
                    findWordsEndingWithStartOf(myDictWordList, myFront, myKeyWord);
                    std::string myBack = myKeyWord.substr(myKeyWord.size()-l);
                    findWordsStartingWithEndof(myDictWordList, myBack, myKeyWord);
                }
            }
            bool matchBeginAndEnd = myBeginKeyWordList.size() && myEndKeyWordList.size();
            std::vector<string> myBeginMatches;
            for (unsigned int i = 0; i < myBeginKeyWordList.size();++i) {
                const string & myKeyWord = myBeginKeyWordList[i];
                for (int l = asl::minimum(myKeyWord.size(), (size_t)maxOverlap); l>=minOverlap; --l) {
                    std::string myBack = myKeyWord.substr(myKeyWord.size()-l);
                    std::vector<string> myMatches = findWordsStartingWithEndof(myDictWordList, myBack, myKeyWord, matchBeginAndEnd);
                    myBeginMatches.insert(myBeginMatches.end(), myMatches.begin(), myMatches.end());
                 }
            }
            if (matchBeginAndEnd) {
                cerr << "Found " << myBeginMatches.size() << " words in first path." << endl;
                myDictWordList = myBeginMatches;
            }
            std::vector<string> myEndMatches;
            for (unsigned int i = 0; i < myEndKeyWordList.size();++i) {
                const string & myKeyWord = myEndKeyWordList[i];
                for (int l = asl::minimum(myKeyWord.size(), (size_t)maxOverlap); l>=minOverlap; --l) {
                    std::string myFront = myKeyWord.substr(0, l);
                    std::vector<string> myMatches = findWordsEndingWithStartOf(myDictWordList, myFront, myKeyWord);
                    myEndMatches.insert(myEndMatches.end(), myMatches.begin(), myMatches.end());
                }
            }
             cerr << "Found " << ourCounter << " matching words." << endl;
        } else {
            cerr << "usage: namesearch  [-o minoverlap]  [-p] [-l] [-s] [-m maxresultlen] [-k keyword] dictionaryfile [keywordfile]" << endl;
            cerr << "    -o minoverlap  ::  set minimum word overlap (default = 2)" << endl;
            cerr << "    -O maxoverlap  ::  set maximum word overlap (default = 3)" << endl;
            cerr << "    -p             :  print keywords and dictionary" << endl;
            cerr << "    -s             :  short form, print matching words without source" << endl;
            cerr << "    -l             :  treat dictionary and keywords as lower case" << endl;
            cerr << "    -x             :  just print cross product of begin and end keywords" << endl;
            cerr << "    -m maxresultlen:  limit maximum result word length" << endl;
            cerr << "    -k keywords    : use this keywords from command line (comma or colon separated)" << endl;
            cerr << "    -K keywordfile :load keywords from file" << endl;
            cerr << "    -e keywords    : keywords to match at end of word" << endl;
            cerr << "    -E keywordfile : keywords to match at end of word" << endl;
            cerr << "    -b keywords    : keywords to match at begin of word" << endl;
            cerr << "    -B keywordfile : keywords to match at begin of word" << endl;
             return -1;
        }
    }
    catch (asl::Exception & ex) {
        cerr << "### ERROR: " << ex << endl;
        return -1;
    }
    return 0;
}