Skip to content

Instantly share code, notes, and snippets.

@alirezaarzehgar
Created June 23, 2024 11:53
Show Gist options
  • Save alirezaarzehgar/eb693704aaf2b4636ac3f1920ff67fda to your computer and use it in GitHub Desktop.
Save alirezaarzehgar/eb693704aaf2b4636ac3f1920ff67fda to your computer and use it in GitHub Desktop.
contact list
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Database {
fstream table;
public:
static vector<string> record2vector(string s)
{
vector<string> out;
int pos = 0;
string token;
while ((pos = s.find(",")) != string::npos) {
token = s.substr(0, pos);
out.push_back(token);
s.erase(0, pos + 1);
}
out.push_back(s);
return out;
}
void reload()
{
table.close();
table.open(tablename, ios::out|ios::in|ios::app);
}
string tablename;
int init(string tblname)
{
tablename = tblname+".csv";
table.open(tablename, ios::out|ios::in|ios::app);
if (!table)
return -1;
return 0;
}
void create(string record)
{
table.seekp(ios::end);
table << record << endl;
table.flush();
}
vector<string> select(int index, string value)
{
vector<string> out, vrec;
table.seekg(ios::beg);
string rec;
while(1) {
table >> rec;
if (table.eof())
break;
vrec = record2vector(rec);
if (vrec[index] == value || index < 0)
out.push_back(rec);
}
reload();
return out;
}
int update(int index, string value, string record)
{
string text;
table.seekg(ios::beg);
string line;
vector<string> vrec;
while(1) {
table >> line;
if(table.eof())
break;
vrec = record2vector(line);
if (vrec[index] == value)
line = record;
text.append(line+"\n");
}
remove(tablename.c_str());
reload();
table << text;
table.flush();
return 0;
}
int del(int index, string value)
{
string text;
table.seekg(ios::beg);
string line;
vector<string> vrec;
while(1) {
table >> line;
if(table.eof())
break;
vrec = record2vector(line);
if (vrec[index] != value)
text.append(line+"\n");
}
remove(tablename.c_str());
reload();
table << text;
table.flush();
return 0;
}
~Database()
{
table.close();
}
};
enum {
/* user model */
FUSER_USERNAME = 0,
FUSER_PHONE,
};
Database users;
int main()
{
users.init("users");
while (1) {
char opt;
int index;
string record, srchval;
vector<string> vrecs;
cout << "[1] Create user" << endl
<< "[2] Search user" << endl
<< "[3] Update user" << endl
<< "[4] Delete user" << endl
<< "[e] Exit" << endl << endl
<< "Choose option: ";
cin >> opt;
switch (opt)
{
case '1':
cout << "Enter (Format: username,phone): ";
cin >> record;
users.create(record);
break;
case '2':
cout << "Enter search index [username=0,phone=1] (-1 for show all): ";
cin >> index;
if (index >= 0) {
cout << "Enter search value: ";
cin >> record;
}
cout << "username,phone" << endl;
vrecs = users.select(index, record);
for (auto &&r : vrecs) {
cout << r << endl;
}
break;
case '3':
cout << "Enter search index [username=0,phone=1]: ";
cin >> index;
cout << "Enter search value: ";
cin >> srchval;
cout << "Enter new data (Format: username,phone): ";
cin >> record;
users.update(index, srchval, record);
break;
case '4':
cout << "Enter search index [username=0,phone=1]: ";
cin >> index;
cout << "Enter search value: ";
cin >> srchval;
users.del(index, srchval);
break;
case 'e':
return 0;
default:
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment