File: smsaddr.cc

package info (click to toggle)
sms-pl 2.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 376 kB
  • ctags: 355
  • sloc: cpp: 2,618; perl: 109; makefile: 103; sh: 26
file content (112 lines) | stat: -rw-r--r-- 2,239 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <pwd.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include "cgdbm.h"
#include "sms.h"

using namespace std;

void ShowHelp()
{
	cerr << "Usage:\n\n\tsmsaddr <options>\n";
	cerr << "\tsmsaddr -a ja 602000000\n";
	cerr << "\tsmsaddr -s ja\n\n"; 
	cerr << "-a nick phone : add nickname to address book\n";
	cerr << "-r nick       : remove nickname\n";
	cerr << "-s nick       : search for nickname\n";
	cerr << "-l            : print every item\n"; 
	cerr << "-h            : show this help screen\n";
	cerr << "-V            : show version information\n";
	cerr << "smsaddr " << SMS_VERSION << ", use at your own risk!" << endl;
}

string AddrBookFileName()
{
	string file;
	struct passwd *pwd;
	char *path;

	if ((pwd = getpwuid(geteuid()))) {
		if ((path = getenv("CONFIG_DIR")) && *path)
			file = string(pwd->pw_dir) + "/" + path + "/" + SMS_ADDRBOOK;
		else
			file = string(pwd->pw_dir) + "/." + SMS_ADDRBOOK;
	}
	return file;
}

int main(int argc, char *argv[])
{
	int ch;
	Cgdbm addr;
	
	addr.Open(AddrBookFileName(), 0, GDBM_WRCREAT);
	
	try {
		while ((ch = getopt(argc, argv, "hVa:r:ls:")) != -1) {
			switch (ch) {
			case 'h':
				ShowHelp();
				exit(0);
				break;
			case 'V':
				cout << "sms " << SMS_VERSION << endl;
				exit(0);
				break;
			case 'l':
				// wylistowanie calosci
				try {
					Cdatum key, val;
					key = addr.begin();
					for(;;) {
						val = addr[key];
						cout << string(key) << " => " << string(val) << endl;
						key = addr.next();
					}
					
				}
				catch(...) {
				}
				break;
			case 'a':
				// dodanie rekordu
				if (optind <= argc) {
					Cdatum key(optarg), val(argv[optind]);
					addr.replace(key, val);
					optind++;
				 } else cerr << "Insufficient data" << endl;
				break;
			case 'r':
				{
					// usuniecie rekordu
					Cdatum key(optarg);
					addr.Delete(key);
				}
				break;
			case 's':
				// szukanie rekordow
				try {
					Cdatum key(optarg), val;
					val = addr[key];
					cout << string(key) << " => " << string(val) << endl;
				}
				catch (...) {
				}
				break;
			default:
				ShowHelp();
				exit(0);
			}
		}
	}
	catch (...) {
		cerr << "Exception caught!" << endl;
	}
	
	argc -= optind;
	argv -= optind;
	
	return 0;
}