Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 02 / task3_temp.cpp
blob: 6bee5efb7ec6eded931da32b87ce9712c429017c (plain) (blame)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <fstream>
#pragma warning(disable:4996)
using namespace std;

const size_t capacity = 1024;

enum class Speciality
{
	computerScience,
	softwareEngineering
};

struct StudentRecord
{
	char firstName[capacity];
	char lastName[capacity];

	char fnRaw[capacity];

	//parsed by fnRaw
	Speciality studentSpeciality;
	int fn;
	//

	char email[capacity];
};

struct StudentCollection
{
	StudentRecord* data;
};

size_t getLinesCount(const char* fileName)
{
	ifstream file(fileName);

	if (!file.is_open())
		return 0;

	size_t count = 0;

	while (!file.eof())
	{
		char buff[capacity];
		file.getline(buff, capacity);
		count++;
	}

	file.close();

	return count;

}

//Angel,

void parseField(char* dest, const char*& source)
{
	while (*source != ',' && *source != '\0')
	{
		*dest = *source;
		dest++;
		source++;
	}
	source++;
	*dest = '\0';
}

void parseFn(StudentRecord& currentStudent)
{
	if (strlen(currentStudent.fnRaw) < 5)
		return;

	if (currentStudent.fnRaw[4] == '6')
		currentStudent.studentSpeciality = Speciality::softwareEngineering;
	else if (currentStudent.fnRaw[4] == '8')
		currentStudent.studentSpeciality = Speciality::computerScience;

	currentStudent.fn = atoi(currentStudent.fnRaw + 5);

}

void parseStudent(StudentRecord& currentStudent, const char* line)
{
	parseField(currentStudent.firstName, line);
	parseField(currentStudent.lastName, line);
	parseField(currentStudent.fnRaw, line);
	parseField(currentStudent.email, line);
	parseFn(currentStudent);
}

void printStudent(const StudentRecord& st)
{
	std::cout << "First name: " << st.firstName << ", second name: " << st.lastName << ", speciality: ";

	if (st.studentSpeciality == Speciality::computerScience)
		std::cout << "computer science, ";
	else
		std::cout << "software engineering, ";

	std::cout << "number: " << st.fn << ", email: " << st.email << std::endl;

}

size_t initStudentCollection(StudentCollection& collection, const char* fileName)
{
	size_t studentsCount = getLinesCount(fileName) - 1;

	collection.data = new StudentRecord[studentsCount];

	ifstream file(fileName);
	if (!file.is_open())
	{
		cout << "error";
		return 0;
	}

	char buff[capacity];
	file.getline(buff, capacity);
	for (int i = 0; i < studentsCount; i++)
	{
		file.getline(buff, capacity);
		parseStudent(collection.data[i], buff);
	}

	return studentsCount;
}

void saveToFile(const char* fileName, const StudentCollection& collection, size_t studentsCount)
{
	ofstream file(fileName);
	if (!file.is_open())
		return;
	file << "Ime, Familiya, Fakulteten nomer, Imeyl adres" << std::endl; //header;
	for (int i = 0; i < studentsCount; i++)
		file << collection.data[i].firstName << "," << collection.data[i].lastName << "," << collection.data[i].fnRaw << "," << collection.data[i].email << std::endl;
}
int main()
{
	const char* fileName = "lists/all_students.csv";
	
	StudentCollection collection;
	size_t studentsCount = initStudentCollection(collection, fileName);
	
	//changes to students email

	saveToFile(fileName, collection, studentsCount);
	delete[] collection.data;
}