-
Notifications
You must be signed in to change notification settings - Fork 1
/
address.h
95 lines (84 loc) · 2.33 KB
/
address.h
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
/* address.h */
/* DO NOT CHANGE ANYTHING */
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#ifndef __ADDRESS_H_
#define __ADDRESS_H_
struct Entry{
Entry(std::string name, int* addr) :
name_(name),
addr_(addr){}
std::string name_{};
int* addr_ = nullptr;
};
class Address{
public:
/**
* @brief Parse the arguments input by user
*
* Three commands supported:
*
* >add <name> <init value>
* Corresponds to add_entry(std::string name, int value)
*
* >del <address>
* Corresponds to del_entry(int* address)
* If del * is entered, it is treated as deleting all addresses
*
* >chg <address> <new value>
* Corresponds to chg_entry(int* address, int value)
*
* @param arguments Arguement input
* @return Whether the arguements are valid
*/
static bool parse_arg(const std::vector<std::string>& arguments);
/**
* @brief Add a variable to our memory
* @param name Variable name
* @param value Initialzed value
* @return Whether the action is successful
*/
static bool add_entry(const std::string& name, const int& value);
/**
* @brief Delete a variable from our memory
* @param address Variable address
* @return Whether the action is successful
*/
static bool del_entry(int* address);
/**
* @brief Change the value of a variable in our memory
* @param address Variable address
* @param value New value
* @return Whether the action is successful
*/
static bool chg_entry(int* address, const int& value);
/**
* @brief Print entered entries in our memory
* In format of:
* <address> "\t" <name> "\t" <value>
* as specified in main.cpp
*/
static void print_data();
/**
* @brief Convert a string to an integer
* @param str Input string
* @param isHex Whether the integer is considered a hexadecimal
* @return int Converted integer
* @return bool Whether the action is successful
*/
static std::pair<long long int, bool> strtoint(const std::string& str, bool isHex = false){
long long int result;
std::istringstream convert(str);
if (!isHex){
if (!(convert >> result)) return std::make_pair(0, false);
} else {
if (!(convert >> std::hex >> result)) return std::make_pair(0, false);
}
return std::make_pair(result, true);
}
private:
static std::vector<Entry> entries;
};
#endif