summaryrefslogtreecommitdiffstats
path: root/Bachelor/Verteilte Systeme/Praktikum2/loesung_neu/phonebook_server.c
blob: de673e03c8f6c4f292c6850be067d437d8f35d30 (plain)
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
/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "phonebook.h"
#include <stdio.h>
#include <string.h>

//phonebookEntry myphonebook[50];
//int i=0;

int *
add_1_svc(phonebookEntry *argp, struct svc_req *rqstp)
{
	static int  result;

	/*
	 * insert server code here
	 */
	FILE* fp;
	fp=fopen("phonebook.dat","a+");
	/*strcat(myphonebook[i].name,argp->name);
	strcat(myphonebook[i].name,"\0");
	strcat(myphonebook[i].number,argp->number);
	strcat(myphonebook[i].number,"\0");*/
	fprintf(fp,"%s;%s\n",argp->name,argp->number);
	printf("%s;%s\n",argp->name,argp->number);
	//printf("Added: %s %s\n",myphonebook[i].name,myphonebook[i].number);
	//i++;
	fclose(fp);
	return &result;
}

str_number *
search_1_svc(str_name *argp, struct svc_req *rqstp)
{
	static str_number  result;
	static str_name aktName;
	static str_number aktNumber;
	char temp[80];
	int n=0;
	/*
	 * insert server code here
	 */
	strcat(result.number,"-1");
	printf("search string: %s\n",argp->name);
	/*for (n=0;n<i;n++)
	{	
		printf("actual entry: %s\n",myphonebook[n].name);
		if (strcmp(argp->name,myphonebook[n].name) == 0)
		{
			strcpy(result.number,myphonebook[n].number);
			break;
		}
	}*/
	FILE *fp;
	fp=fopen("phonebook.dat","r");
	while(!feof(fp))
	{	
		strcpy(temp,"\0");
		strcpy(aktName.name,"\0");
		strcpy(aktNumber.number,"\0");
		fscanf(fp,"%s",&temp);
		printf("Read from file: %s ",temp);
		strcat(aktName.name,strtok(temp,";"));
		strcat(aktName.name,"\0");
		strcat(aktNumber.number,strtok(NULL,";"));
		strcat(aktNumber.number,"\0");
		//printf("actual entry: %s\n",myphonebook[n].name);
		printf("actual entry: %s %s\n",aktName.name,aktNumber.number);
		if(strcmp(aktName.name,argp->name) == 0)
		{
			strcpy(result.number,aktNumber.number);
			fclose(fp);
			return &result;
		}
	}
	strcpy(result.number,"-1");	
	fclose(fp);
	
	return &result;
}