Add optional return

This commit is contained in:
Nikola Petrov 2023-08-27 20:38:26 +02:00
parent 375af5f3cd
commit 058e24d436
4 changed files with 16 additions and 21 deletions

View File

@ -4,6 +4,7 @@ struct LoginInfoPointer;
class Buffer; class Buffer;
class Cryptography; class Cryptography;
#include <string> #include <string>
#include <optional>
enum class Arg enum class Arg
{ {
@ -22,11 +23,11 @@ void print_args();
Arg get_args(int argc, char** argv, char** label); Arg get_args(int argc, char** argv, char** label);
LoginInfoPointer arg_get(Buffer& decrypted_buffer, const char* label); std::optional<LoginInfoPointer> arg_get(Buffer& decrypted_buffer, const char* label);
LoginInfoPointer arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto); std::optional<LoginInfoPointer> arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto);
LoginInfoPointer arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto); std::optional<LoginInfoPointer> arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto);
void arg_list(Buffer& decrypted_buffer); void arg_list(Buffer& decrypted_buffer);

View File

@ -13,8 +13,6 @@ struct LoginInfoPointer
{ {
const char* label; const char* label;
const char* password; const char* password;
LoginInfoPointer() : label(nullptr), password(nullptr) {}
}; };
struct Index struct Index

View File

@ -56,17 +56,17 @@ Arg get_args(int argc, char** argv, char** label) {
return Arg::Error; return Arg::Error;
} }
LoginInfoPointer arg_get(Buffer& decrypted_buffer, const char* label) std::optional<LoginInfoPointer> arg_get(Buffer& decrypted_buffer, const char* label)
{ {
int pass = find_logininfo_in_buffer(decrypted_buffer, label); int pass = find_logininfo_in_buffer(decrypted_buffer, label);
if (pass < 0) { if (pass < 0) {
printf_s("Password not found\n"); printf_s("Password not found\n");
return LoginInfoPointer(); return {};
} }
return get_logininfo_pointer_from_buffer(decrypted_buffer, pass); return get_logininfo_pointer_from_buffer(decrypted_buffer, pass);
} }
LoginInfoPointer arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto) std::optional<LoginInfoPointer> arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto)
{ {
printf_s("Generating password for %s\n", label); printf_s("Generating password for %s\n", label);
@ -83,7 +83,7 @@ LoginInfoPointer arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer
return get_logininfo_pointer_from_buffer(decrypted_buffer, index->count - 1); return get_logininfo_pointer_from_buffer(decrypted_buffer, index->count - 1);
} }
LoginInfoPointer arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto) std::optional<LoginInfoPointer> arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto)
{ {
printf_s("Input password for %s:", label); printf_s("Input password for %s:", label);
@ -91,9 +91,7 @@ LoginInfoPointer arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, c
if (new_string.empty()) if (new_string.empty())
{ {
printf_s("error getting password\n"); printf_s("error getting password\n");
return {};
return LoginInfoPointer();
} }
int pass = find_logininfo_in_buffer(decrypted_buffer, label); int pass = find_logininfo_in_buffer(decrypted_buffer, label);
@ -213,5 +211,5 @@ void arg_show(Buffer& decrypted_buffer, const char* label) {
if (pass < 0) if (pass < 0)
printf_s("Password not found\n"); printf_s("Password not found\n");
else else
printf_s("Password: %s\n", get_logininfo_pointer_from_buffer(decrypted_buffer, pass)); printf_s("Password: %s\n", get_logininfo_pointer_from_buffer(decrypted_buffer, pass).password);
} }

View File

@ -8,8 +8,6 @@
#ifdef _DEBUG #ifdef _DEBUG
int main() int main()
{ {
const char* label = "facebook"; const char* label = "facebook";
@ -35,7 +33,7 @@ int main()
decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index)); decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index));
} }
LoginInfoPointer login_info; std::optional<LoginInfoPointer> login_info;
switch (args) switch (args)
{ {
@ -72,10 +70,10 @@ int main()
break; break;
} }
if (!login_info.password) return 0; if (!login_info.has_value()) return 0;
printf_s("Password copied to clipboard\n"); printf_s("Password copied to clipboard\n");
put_data_on_clipboard(login_info.password); put_data_on_clipboard(login_info.value().password);
} }
@ -114,7 +112,7 @@ int main(int argc, char** argv)
decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index)); decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index));
} }
LoginInfoPointer login_info; std::optional<LoginInfoPointer> login_info;
switch (args) switch (args)
{ {
@ -151,10 +149,10 @@ int main(int argc, char** argv)
break; break;
} }
if (!login_info.password) return 1; if (!login_info.has_value()) return 1;
printf_s("Password copied to clipboard\n"); printf_s("Password copied to clipboard\n");
put_data_on_clipboard(login_info.password); put_data_on_clipboard(login_info.value().password);
} }