// Demo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class dateObj {
public:
dateObj(const string &s);
~dateObj();
private:
unsigned year = 0;
unsigned month = 0;
unsigned day = 0;
enum type { dateyear, datemonth, dateday };
vector<string> ml = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October","November","December" };
vector<string> aml = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
unsigned _trans(const string &s);
void _match(const string &s, dateObj::type);
bool _hasSlash(const string &s);
};
// January 1, 1900, 1/1/1900, Jan 1, 1900
dateObj::dateObj(const string &s) {
_match(s, datemonth);
_match(s, dateyear);
_match(s, dateday);
cout << month << " " << day << " " << year << endl;
}
unsigned dateObj::_trans(const string &s) {
for (int i = 0; i < 12; ++i) {
if (s.compare(ml[i]) == 0) { return ++i; }
if (s.compare(aml[i]) == 0) { return ++i; }
}
return 255;
}
void dateObj::_match(const string &s, dateObj::type t) {
if (!_hasSlash(s)) {
if (t == dateObj::type::datemonth) {
auto pos = s.find_first_of(' ');
if (pos == string::npos) {
cout << "Not a validate string, cannot found the month" << endl;
return;
}
else {
string tmp(s, 0, pos - 0);
month = _trans(tmp);
}
}
else if (t == dateObj::type::dateday) {
auto posBeg = s.find_first_of(' ');
auto posEnd = s.find_first_of(",");
if (posBeg == string::npos || posEnd == string::npos) {
cout << "Not a validate string, cannot found the day" << endl;
return;
}
string tmp(s, posBeg, posEnd - posBeg);
day = stoi(tmp);
}
else if (t == dateObj::type::dateyear) {
auto posBeg = s.find_first_of(",");
size_t posEnd = s.size();
if (posBeg == string::npos) {
cout << "Not a validate string, cannot found the year" << endl;
return;
}
string tmp(s, posBeg, posEnd - posBeg);
year = stoi(tmp.substr(tmp.find_first_of("0123456789")));
}
else {
cout << "Invalide date type" << endl;
}
}
else {
string tmp = s;
if (t == dateObj::type::datemonth) {
auto posEnd = s.find_first_of('/');
if (posEnd == string::npos) {
cout << "Not a validate string, cannot found the month" << endl;
return;
}
string monthStr(s, 0, posEnd);
month = _trans(monthStr);
}
else if (t == dateObj::type::dateday) {
auto posBeg = s.find_first_of('/');
auto posEnd = s.find_first_of('/', posBeg + 1);
if (posBeg == string::npos || posEnd == string::npos) {
cout << "Not a validate string, cannot found the day" << endl;
return;
}
string dayStr(s, posBeg, posEnd - posBeg);
day = stoi(dayStr.substr(dayStr.find_first_of("0123456789")));
}
else if (t == dateObj::type::dateyear) {
auto posTmp = s.find_first_of('/');
auto posBeg = s.find_first_of('/', posTmp + 1);
auto posEnd = s.size();
if (posBeg == string::npos || posTmp == string::npos) {
cout << "Not a validate string, cannot found the year" << endl;
return;
}
string yearStr(s, posBeg, posEnd);
year = stoi(yearStr.substr(yearStr.find_first_of("0123456789")));
}
else {
cout << "Invalide date type" << endl;
}
}
}
bool dateObj::_hasSlash(const string &s) {
return (s.find_first_of('/') != string::npos);
}
dateObj::~dateObj() {
cout << "destruct" << endl;
}
int main()
{
ifstream f;
f.open("test.txt");
string date;
if (f) {
getline(f, date);
dateObj d = dateObj(date);
f.close()
}
else {
cout << "Cannot open file" << endl;
f.close();
}
return 0;
}