Skip to content

Commit 16b3a3f

Browse files
committed
Bug Fixes and Performance Improvements
1 parent 8add509 commit 16b3a3f

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed

src/Bus.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ using namespace std;
1313
// Initialie variables in constructor
1414
Bus::Bus()
1515
{
16-
strcpy(busNo, "");
16+
strlcpy(busNo, "");
1717
maxSeats = 32;
1818
bookedSeats = 0;
1919
busFare = 0.0;
20-
strcpy(source, "");
21-
strcpy(destination, "");
22-
strcpy(sourceTime, "");
23-
strcpy(destinationTime, "");
20+
strlcpy(source, "");
21+
strlcpy(destination, "");
22+
strlcpy(sourceTime, "");
23+
strlcpy(destinationTime, "");
2424
}
2525

2626
// Display bus details

src/Bus.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <iostream>
55

6+
#include "utils.h"
7+
68
using namespace std;
79

810
// BUS CLASS
@@ -82,25 +84,25 @@ class Bus
8284
void setSource(char *s)
8385
{
8486
if (s && s[0])
85-
strcpy(source, s);
87+
strlcpy(source, s);
8688
}
8789

8890
void setDestination(char *d)
8991
{
9092
if (d && d[0])
91-
strcpy(destination, d);
93+
strlcpy(destination, d);
9294
}
9395

9496
void setSourceTime(char *s)
9597
{
9698
if (s && s[0])
97-
strcpy(sourceTime, s);
99+
strlcpy(sourceTime, s);
98100
}
99101

100102
void setDestinationTime(char *d)
101103
{
102104
if (d && d[0])
103-
strcpy(destinationTime, d);
105+
strlcpy(destinationTime, d);
104106
}
105107

106108
void setBusFare(double f)

src/Reservation.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Reservation::Reservation() {}
1616
// Generate a ticket
1717
void Reservation::_generateTicket(char *n, Bus b)
1818
{
19-
strcpy(name, n);
20-
strcpy(pnrNo, generatePNR(99999).c_str());
21-
strcpy(date, getCurrentDate().c_str());
19+
strlcpy(name, n);
20+
strlcpy(pnrNo, generatePNR(99999).c_str());
21+
strlcpy(date, getCurrentDate().c_str());
2222
bus = b;
2323
}
2424

src/Reservation.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <iostream>
55

66
#include "Bus.h"
7+
#include "utils.h"
78

89
// RESERVATION CLASS
910
class Reservation
@@ -49,7 +50,7 @@ class Reservation
4950
void setName(char *n)
5051
{
5152
if (n && n[0])
52-
strcpy(name, n);
53+
strlcpy(name, n);
5354
}
5455
};
5556

src/utils.h

+21-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
#include <cstdlib>
55
#include <string>
6+
#include <cstring>
7+
#include <limits>
68
#include <ctime>
9+
#include <stddef.h>
710

811
using namespace std;
912

@@ -31,8 +34,24 @@ string generatePNR(int n)
3134
string getCurrentDate()
3235
{
3336
time_t t = time(NULL);
34-
tm *tPtr = localtime(&t);
37+
struct tm tStruct;
38+
localtime_s(&tStruct, &t);
3539

36-
return to_string(tPtr->tm_mday) + "-" + to_string((tPtr->tm_mon) + 1) + "-" + to_string((tPtr->tm_year) + 1900);
40+
return std::to_string(tStruct.tm_mday) + "-" + std::to_string(tStruct.tm_mon + 1) + "-" + std::to_string(tStruct.tm_year + 1900);
3741
}
42+
43+
size_t strlcpy(char *dst, const char *src, size_t dstsize = std::numeric_limits<size_t>::max())
44+
{
45+
size_t srclen = std::strlen(src);
46+
size_t copylen = (srclen >= dstsize) ? dstsize - 1 : srclen;
47+
48+
if (dstsize != 0)
49+
{
50+
std::memcpy(dst, src, copylen);
51+
dst[copylen] = '\0'; // Null-terminate the destination buffer
52+
}
53+
54+
return srclen; // Return the total length of the string it tried to create
55+
}
56+
3857
#endif // UTILS_H

0 commit comments

Comments
 (0)