File tree 5 files changed +37
-15
lines changed
5 files changed +37
-15
lines changed Original file line number Diff line number Diff line change @@ -13,14 +13,14 @@ using namespace std;
13
13
// Initialie variables in constructor
14
14
Bus::Bus ()
15
15
{
16
- strcpy (busNo, " " );
16
+ strlcpy (busNo, " " );
17
17
maxSeats = 32 ;
18
18
bookedSeats = 0 ;
19
19
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, " " );
24
24
}
25
25
26
26
// Display bus details
Original file line number Diff line number Diff line change 3
3
4
4
#include < iostream>
5
5
6
+ #include " utils.h"
7
+
6
8
using namespace std ;
7
9
8
10
// BUS CLASS
@@ -82,25 +84,25 @@ class Bus
82
84
void setSource (char *s)
83
85
{
84
86
if (s && s[0 ])
85
- strcpy (source, s);
87
+ strlcpy (source, s);
86
88
}
87
89
88
90
void setDestination (char *d)
89
91
{
90
92
if (d && d[0 ])
91
- strcpy (destination, d);
93
+ strlcpy (destination, d);
92
94
}
93
95
94
96
void setSourceTime (char *s)
95
97
{
96
98
if (s && s[0 ])
97
- strcpy (sourceTime, s);
99
+ strlcpy (sourceTime, s);
98
100
}
99
101
100
102
void setDestinationTime (char *d)
101
103
{
102
104
if (d && d[0 ])
103
- strcpy (destinationTime, d);
105
+ strlcpy (destinationTime, d);
104
106
}
105
107
106
108
void setBusFare (double f)
Original file line number Diff line number Diff line change @@ -16,9 +16,9 @@ Reservation::Reservation() {}
16
16
// Generate a ticket
17
17
void Reservation::_generateTicket (char *n, Bus b)
18
18
{
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 ());
22
22
bus = b;
23
23
}
24
24
Original file line number Diff line number Diff line change 4
4
#include < iostream>
5
5
6
6
#include " Bus.h"
7
+ #include " utils.h"
7
8
8
9
// RESERVATION CLASS
9
10
class Reservation
@@ -49,7 +50,7 @@ class Reservation
49
50
void setName (char *n)
50
51
{
51
52
if (n && n[0 ])
52
- strcpy (name, n);
53
+ strlcpy (name, n);
53
54
}
54
55
};
55
56
Original file line number Diff line number Diff line change 3
3
4
4
#include < cstdlib>
5
5
#include < string>
6
+ #include < cstring>
7
+ #include < limits>
6
8
#include < ctime>
9
+ #include < stddef.h>
7
10
8
11
using namespace std ;
9
12
@@ -31,8 +34,24 @@ string generatePNR(int n)
31
34
string getCurrentDate ()
32
35
{
33
36
time_t t = time (NULL );
34
- tm *tPtr = localtime (&t);
37
+ struct tm tStruct;
38
+ localtime_s (&tStruct, &t);
35
39
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 );
37
41
}
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
+
38
57
#endif // UTILS_H
You can’t perform that action at this time.
0 commit comments