MCPcopy Create free account
hub / github.com/DFHack/dfhack / orders_import_command

Function orders_import_command

plugins/orders.cpp:913–971  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

911}
912
913static command_result orders_import_command(color_ostream & out, const std::string & name)
914{
915 std::string fname = name;
916 bool is_library = false;
917 if (0 == name.find("library/")) {
918 is_library = true;
919 fname = name.substr(8);
920 }
921
922 if (!is_safe_filename(out, fname))
923 {
924 return CR_WRONG_USAGE;
925 }
926
927 auto filename((is_library ? ORDERS_LIBRARY_DIR : ORDERS_DIR) / (fname + ".json"));
928 Json::Value orders;
929
930 {
931 std::ifstream file(filename);
932
933 if (!file.good())
934 {
935 out << COLOR_LIGHTRED << "Cannot find orders file: " << filename << std::endl;
936 return CR_FAILURE;
937 }
938
939 try
940 {
941 file >> orders;
942 }
943 catch (const std::exception & e)
944 {
945 out << COLOR_LIGHTRED << "Error reading orders file: " << filename << ": " << e.what() << std::endl;
946 return CR_FAILURE;
947 }
948
949 if (!file.good())
950 {
951 out << COLOR_LIGHTRED << "Error reading orders file: " << filename << std::endl;
952 return CR_FAILURE;
953 }
954 }
955
956 if (orders.type() != Json::arrayValue)
957 {
958 out << COLOR_LIGHTRED << "Invalid orders file: " << filename << ": expected array" << std::endl;
959 return CR_FAILURE;
960 }
961
962 try
963 {
964 return orders_import(out, orders);
965 }
966 catch (const std::exception & e)
967 {
968 out << COLOR_LIGHTRED << "Error reading orders file: " << filename << ": " << e.what() << std::endl;
969 return CR_FAILURE;
970 }

Callers 1

orders_commandFunction · 0.85

Calls 5

is_safe_filenameFunction · 0.85
orders_importFunction · 0.85
findMethod · 0.45
whatMethod · 0.45
typeMethod · 0.45

Tested by

no test coverage detected