/*
- * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <algorithm>
#include <cstring>
+#include <filesystem>
#include <iostream>
#include <memory>
#include <new>
-#include <sstream>
#include <stdexcept>
#include <string>
+
#include <unistd.h>
#include <cynara-error.h>
const std::string OPTION_MD5 = "md5";
const std::string OPTION_CRYPT = "crypt";
-void printHelp(const char *exeName) {
- static int done = 0;
- if (!done) {
- std::cout << "Usage: " << exeName << " FILE [OPTION]" << std::endl << std::endl <<
- "Options:\n"
- "\t-" << static_cast<char>(CommandLineOption::Algorithm) <<
- " ALGORITHM use selected algorith to count hash. Currently this "
- "tool supports 'crypt' and 'md5'. 'crypt' is used as the default value " <<
- "for backward compatibility." << std::endl <<
- "\t-" << static_cast<char>(CommandLineOption::Help) <<
- " print this help" << std::endl;
- done = 1;
- }
+void printHelp(const std::string &exeName) {
+ std::cout << "Usage: " << exeName << " FILE [OPTION]" << std::endl << std::endl <<
+ "Options:\n"
+ "\t-" << static_cast<char>(CommandLineOption::Algorithm) <<
+ " ALGORITHM use selected algorith to count hash. Currently this "
+ "tool supports 'crypt' and 'md5'. 'crypt' is used as the default value " <<
+ "for backward compatibility." << std::endl <<
+ "\t-" << static_cast<char>(CommandLineOption::Help) <<
+ " print this help" << std::endl;
}
std::string generateCrypt(const std::string &data) {
const char ChecksumGenerator::m_recordSeparator('\n');
const std::string ChecksumGenerator::m_backupFilenameSuffix("~");
-ChecksumGenerator::ChecksumGenerator(int argc, char * const *argv)
- : m_algorithm(OPTION_CRYPT)
-{
+ChecksumGenerator::ChecksumGenerator()
+ : m_showHelp(false), m_exeName("cynara-db-chsgen"), m_algorithm(OPTION_CRYPT)
+{}
+
+void ChecksumGenerator::parseOptions(int argc, char * const *argv) {
optind = 0;
- const char *exeName = "cynara-db-chsgen";
- if (argc > 0 && argv[0])
- exeName = argv[0];
+ if (argc > 0)
+ m_exeName = std::filesystem::path(argv[0]).filename();
int option;
std::stringstream shortOptions;
m_algorithm = optarg;
break;
case CommandLineOption::Help:
- printHelp(exeName);
+ m_showHelp = true;
break;
}
}
}
-
-int ChecksumGenerator::run(void) {
+int ChecksumGenerator::run(int argc, char * const *argv) {
try {
+ parseOptions(argc, argv);
+ if (m_showHelp) {
+ printHelp(m_exeName);
+ return CYNARA_API_SUCCESS;
+ }
openFileStream();
copyFileStream();
printRecord();
throw std::bad_alloc();
}
- std::string basename(::basename(pathnameDuplicate.get()));
- removeBackupSuffix(basename);
+ std::string filename = std::filesystem::path(m_pathname).filename();
+ removeBackupSuffix(filename);
- std::cout << basename << m_fieldSeparator << generate(m_copyStream.str())
- << m_recordSeparator;
+ auto checksum = generate(m_copyStream.str());
+ std::cout << filename << m_fieldSeparator << generate(m_copyStream.str())
+ << m_recordSeparator;
}
void ChecksumGenerator::removeBackupSuffix(std::string &filename) const {
/*
- * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
class ChecksumGenerator {
public:
- ChecksumGenerator(int argc, char * const *argv);
- ~ChecksumGenerator() {}
+ ChecksumGenerator();
- int run(void);
+ int run(int argc, char * const *argv);
private:
const std::string generate(const std::string &data) const;
+ void parseOptions(int argc, char * const *argv);
void openFileStream(void);
void copyFileStream(void);
void printRecord(void) const;
void removeBackupSuffix(std::string &filename) const;
+ bool m_showHelp;
+ std::string m_exeName;
std::ifstream m_inputStream;
std::stringstream m_copyStream;
std::string m_pathname;
/*
- * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
int main(int argc, char **argv) {
try {
- Cynara::ChecksumGenerator chsgen(argc, argv);
- return chsgen.run();
+ Cynara::ChecksumGenerator chsgen;
+ return chsgen.run(argc, argv);
} catch (const std::bad_alloc &) {
std::cerr << "Chsgen could not allocate memory" << std::endl;
return CYNARA_API_OUT_OF_MEMORY;
/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
clearOutput();
prepare_argv({ execName });
- Cynara::ChecksumGenerator chsgen(this->argc(), this->argv());
- const auto ret = chsgen.run();
+ Cynara::ChecksumGenerator chsgen;
+ const auto ret = chsgen.run(this->argc(), this->argv());
getOutput(out, err);
ASSERT_EQ(CYNARA_API_UNKNOWN_ERROR, ret);
prepare_argv({ execName, Cynara::PathConfig::testsPath + "/db3/" + file });
SCOPED_TRACE(file);
- Cynara::ChecksumGenerator chsgen(this->argc(), this->argv());
- const auto ret = chsgen.run();
+ Cynara::ChecksumGenerator chsgen;
+ const auto ret = chsgen.run(this->argc(), this->argv());
getOutput(out, err);
ASSERT_EQ(CYNARA_API_SUCCESS, ret);
Cynara::PathConfig::testsPath + "/db6/" + file + backupFilenameSuffix });
SCOPED_TRACE(file);
- Cynara::ChecksumGenerator chsgen(this->argc(), this->argv());
- const auto ret = chsgen.run();
+ Cynara::ChecksumGenerator chsgen;
+ const auto ret = chsgen.run(this->argc(), this->argv());
getOutput(out, err);
ASSERT_EQ(CYNARA_API_SUCCESS, ret);