Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
SermetPekin committed Nov 5, 2024
1 parent 298344c commit a0042ed
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 67 deletions.
91 changes: 57 additions & 34 deletions include/shorten.h
Original file line number Diff line number Diff line change
@@ -1,103 +1,128 @@
#ifndef getShortFilenames_
#ifndef getShortFilenames_

#define getShortFilenames_
#define getShortFilenames_

#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <sstream>


#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>

std::string normalizeDelimiters(const std::string& input) {
std::string normalizeDelimiters(const std::string &input)
{
std::string result = input;


std::replace(result.begin(), result.end(), '\n', '-');


std::string normalized;
bool lastWasHyphen = false;
for (char c : result) {
if (c == '-') {
if (!lastWasHyphen) {
for (char c : result)
{
if (c == '-')
{
if (!lastWasHyphen)
{
normalized += c;
lastWasHyphen = true;
}
} else {
}
else
{
normalized += c;
lastWasHyphen = false;
}
}

// Remove trailing hyphen if present
if (!normalized.empty() && normalized.back() == '-') {
if (!normalized.empty() && normalized.back() == '-')
{
normalized.pop_back();
}

return normalized;
}

int mainAA() {
int mainAA()
{
std::string input = "TP.DK.USD.A\nTP.DK.EUR.A\nTP.DK.CHF.A-TP.DK.GBP.A";
std::string output = normalizeDelimiters(input);
std::cout << "Normalized string: " << output << std::endl;

return 0;
}



// Helper function to capitalize the first letter of each word
std::string capitalizeWords(const std::string& input) {
std::string capitalizeWords(const std::string &input)
{
std::string result;
bool capitalizeNext = true;

for (char c : input) {
if (std::isalnum(c)) { // Process alphanumeric characters only
if (capitalizeNext) {
for (char c : input)
{
if (std::isalnum(c))
{ // Process alphanumeric characters only
if (capitalizeNext)
{
result += std::toupper(c);
capitalizeNext = false;
} else {
}
else
{
result += std::tolower(c);
}
} else {
capitalizeNext = true; // Reset on non-alphanumeric characters
}
else
{
capitalizeNext = true; // Reset on non-alphanumeric characters
}
}
return result;
}

// Function to generate a short, concise filename
std::string getShortFilename(const std::string& input) {
std::string getShortFilename(const std::string &input)
{

if (input.size() < 9)
{

return input;
}

std::string cleaned;

// Step 1: Remove special characters and keep alphanumeric only
for (char c : input) {
if (std::isalnum(c)) {
for (char c : input)
{
if (std::isalnum(c))
{
cleaned += c;
} else if (c == '-' || c == '_' || c == '.') {
cleaned += ' '; // Replace delimiters with spaces
}
else if (c == '-' || c == '_' || c == '.')
{
cleaned += ' '; // Replace delimiters with spaces
}
}

// Step 2: Capitalize words to create a title-like format
cleaned = capitalizeWords(cleaned);

// Step 3: Limit length (optional, set to 15 characters as an example)
if (cleaned.length() > 15) {
if (cleaned.length() > 15)
{
cleaned = cleaned.substr(0, 15);
}

return cleaned;
}

int E1() {
int E1()
{
std::string test_index("TP.DK.USD.A-TP.DK.EUR.A-TP.DK.CHF.A-TP.DK.GBP.A-TP.DK.JPY.A");
std::string test_index2("TP.DK.USD.A\nTP.DK.EUR.A\nTP.DK.CHF.A-TP.DK.GBP.A");
std::string bie_yssk("bie_yssk");
Expand All @@ -109,6 +134,4 @@ int E1() {
return 0;
}



#endif // getShortFilenames_
14 changes: 8 additions & 6 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ namespace evds
// .................................................................................... Config
struct Config
{
std::string start_date = "01-01-2000";
std::string end_date = "31-12-2100";
std::string frequency = "daily";
std::string aggregation = "none";
bool test = false ;

std::string start_date = "01-01-2000";
std::string end_date = "31-12-2100";
std::string frequency = "daily";
std::string aggregation = "none";
bool test = false;
std::string frequency = "default"; // | monthly | weekly | annually | semimonthly | semiannually | business
std::string formulas = "default"; // | level | percentage_change | difference | year_to_year_percent_change | year_to_year_differences |
std::string aggregation = "default"; // | avg |min | max | first | last | sum
bool cache = true;
};

Expand Down
110 changes: 83 additions & 27 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,90 +16,146 @@

// load_env

struct ParseArgsOptions {
struct ParseArgsOptions
{
int argc;
char** argv;
std::vector<std::string>& indexes;
char **argv;
std::vector<std::string> &indexes;
};

std::unordered_map<std::string, std::string> parseArgs(ParseArgsOptions& options) {
std::unordered_map<std::string, std::string> parseArgs(ParseArgsOptions &options)
{
std::unordered_map<std::string, std::string> args;

bool namedArgsFound = false;

for (int i = 1; i < options.argc; ++i) {
for (int i = 1; i < options.argc; ++i)
{
std::string arg = options.argv[i];

if (arg.rfind("--", 0) == 0) {
if (arg.rfind("--", 0) == 0)
{
namedArgsFound = true;
std::string key = arg.substr(2);
if (i + 1 < options.argc) {
args[key] = options.argv[++i];
} else {
args[key] = "true";
if (i + 1 < options.argc)
{
args[key] = options.argv[++i];
}
} else if (!namedArgsFound) {
else
{
args[key] = "true";
}
}
else if (!namedArgsFound)
{
std::istringstream iss(arg);
std::string index;
while (std::getline(iss, index, ',')) {
options.indexes.push_back(index);
while (std::getline(iss, index, ','))
{
options.indexes.push_back(index);
}
}
}

return args;
}


#include <unordered_map>
#include <string>
#include <functional>

void setConfigOptions(const std::unordered_map<std::string, std::string> &args, Config &config) {
std::unordered_map<std::string, std::function<void(const std::string&)>> configSetters = {
{"cache", [&](const std::string &val) { config.cache = (val == "true"); }},
{"test", [&](const std::string &val) { config.test = (val == "true"); }},
{"start_date", [&](const std::string &val) { config.start_date = val; }},
{"end_date", [&](const std::string &val) { config.end_date = val; }},
{"frequency", [&](const std::string &val) { config.frequency = val; }},
{"formulas", [&](const std::string &val) { config.formulas = val; }},
{"aggregation", [&](const std::string &val) { config.aggregation = val; }}
};


for (const auto &arg : args) {
if (configSetters.count(arg.first)) {
configSetters[arg.first](arg.second);
}
}
}


int main(int argc, char *argv[])
{
// Initialize configuration with defaults
evds::Config config;
config.cache = false;
config.start_date = "01-01-2020";
config.end_date = "31-12-2025";
config.test = false ;
config.test = false;
config.frequency = "default"; // | monthly | weekly | annually | semimonthly | semiannually | business
config.formulas = "default"; // | level | percentage_change | difference | year_to_year_percent_change | year_to_year_differences |
config.aggregation = "default"; // | avg |min | max | first | last | sum

// Parse command-line arguments
std::vector<std::string> indexes;
ParseArgsOptions poptions{argc, argv, indexes}; // Pass indexes by reference
ParseArgsOptions poptions{argc, argv, indexes}; // Pass indexes by reference

auto args = parseArgs(poptions);
/*
./evdscpp --cache true --test false --start_date 01-12-2020
*/

// Set config options if provided
if (args.count("cache")) {

if (args.count("cache"))
{
config.cache = args["cache"] == "true";
}
if (args.count("test")) {
if (args.count("test"))
{
config.test = args["test"] == "true";
}

if (args.count("start_date")) {
if (args.count("start_date"))
{
config.start_date = args["start_date"];
}
if (args.count("end_date")) {
if (args.count("end_date"))
{
config.end_date = args["end_date"];
}

if (args.count("frequency"))
{
config.frequency = args["frequency"];
}
if (args.count("formulas"))
{
config.formulas = args["formulas"];
}
if (args.count("aggregation"))
{
config.aggregation = args["aggregation"];
}




if (poptions.indexes.empty()) {
if (poptions.indexes.empty())
{
std::cerr << "No indexes provided." << std::endl;
return EXIT_FAILURE;
}

DataFrame df_current;

for ( auto& x : poptions.indexes)
for (auto &x : poptions.indexes)
{
auto df = get_series(x, config);

df_current = df;
std::string f_name = getShortFilename(x);

df.to_csv("data_" + f_name + ".csv" , ',');

df.to_csv("data_" + f_name + ".csv", ',');
}

return EXIT_SUCCESS;
Expand Down

0 comments on commit a0042ed

Please sign in to comment.