From: Frederic Suter Date: Thu, 10 Aug 2017 13:08:10 +0000 (+0200) Subject: stringify IO X-Git-Tag: v3_17~217^2 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/75d29f0fa7f6c7c590075dd654d8ee70c80ff24a stringify IO --- diff --git a/examples/s4u/actions-storage/s4u_actions-storage.cpp b/examples/s4u/actions-storage/s4u_actions-storage.cpp index 85e71e13f6..85ffb9d975 100644 --- a/examples/s4u/actions-storage/s4u_actions-storage.cpp +++ b/examples/s4u/actions-storage/s4u_actions-storage.cpp @@ -29,7 +29,7 @@ static void log_action(const char* const* action, double date) } } -static simgrid::s4u::File* get_file_descriptor(const char* file_name) +static simgrid::s4u::File* get_file_descriptor(std::string file_name) { std::string full_name = simgrid::s4u::this_actor::getName() + ":" + file_name; @@ -60,11 +60,11 @@ public: /* My actions */ static void open(const char* const* action) { - const char* file_name = action[2]; + std::string file_name = action[2]; double clock = simgrid::s4u::Engine::getClock(); std::string full_name = simgrid::s4u::this_actor::getName() + ":" + file_name; - ACT_DEBUG("Entering Open: %s (filename: %s)", NAME, file_name); + ACT_DEBUG("Entering Open: %s (filename: %s)", NAME, file_name.c_str()); simgrid::s4u::File* file = new simgrid::s4u::File(file_name, NULL); opened_files.insert({full_name, file}); @@ -74,7 +74,7 @@ public: static void read(const char* const* action) { - const char* file_name = action[2]; + std::string file_name = action[2]; sg_size_t size = std::stoul(action[3]); double clock = simgrid::s4u::Engine::getClock(); @@ -88,12 +88,12 @@ public: static void close(const char* const* action) { - const char* file_name = action[2]; + std::string file_name = action[2]; double clock = simgrid::s4u::Engine::getClock(); simgrid::s4u::File* file = get_file_descriptor(file_name); - ACT_DEBUG("Entering Close: %s (filename: %s)", NAME, file_name); + ACT_DEBUG("Entering Close: %s (filename: %s)", NAME, file_name.c_str()); delete file; log_action(action, simgrid::s4u::Engine::getClock() - clock); diff --git a/examples/s4u/io/s4u_io.cpp b/examples/s4u/io/s4u_io.cpp index 378f271419..8c60b637d0 100644 --- a/examples/s4u/io/s4u_io.cpp +++ b/examples/s4u/io/s4u_io.cpp @@ -16,7 +16,7 @@ public: XBT_INFO("Storage info on %s:", simgrid::s4u::Host::current()->getCname()); for (const auto&kv : mounts) { - const char* mountpoint = kv.first.c_str(); + std::string mountpoint = kv.first; simgrid::s4u::Storage* storage = kv.second; // Retrieve disk's information @@ -24,8 +24,8 @@ public: sg_size_t used_size = storage->getSizeUsed(); sg_size_t size = storage->getSize(); - XBT_INFO(" %s (%s) Used: %llu; Free: %llu; Total: %llu.", storage->getName(), mountpoint, used_size, free_size, - size); + XBT_INFO(" %s (%s) Used: %llu; Free: %llu; Total: %llu.", storage->getName(), mountpoint.c_str(), used_size, + free_size, size); } } @@ -36,11 +36,11 @@ public: show_info(mounts); // Open an non-existing file to create it - const char* filename = "/home/tmp/data.txt"; + std::string filename = "/home/tmp/data.txt"; simgrid::s4u::File* file = new simgrid::s4u::File(filename, nullptr); sg_size_t write = file->write(200000); // Write 200,000 bytes - XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, filename); + XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, filename.c_str()); // check that sizes have changed show_info(mounts); @@ -49,22 +49,22 @@ public: const sg_size_t file_size = file->size(); file->seek(0); const sg_size_t read = file->read(file_size); - XBT_INFO("Read %llu bytes on %s", read, filename); + XBT_INFO("Read %llu bytes on %s", read, filename.c_str()); // Now write 100,000 bytes in tmp/data.txt write = file->write(100000); // Write 100,000 bytes - XBT_INFO("Write %llu bytes on %s", write, filename); + XBT_INFO("Write %llu bytes on %s", write, filename.c_str()); simgrid::s4u::Storage* storage = simgrid::s4u::Storage::byName("Disk4"); // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme - const char *newpath = "/home/tmp/simgrid.readme"; - XBT_INFO("Move '%s' to '%s'", file->getPath(), newpath); + std::string newpath = "/home/tmp/simgrid.readme"; + XBT_INFO("Move '%s' to '%s'", file->getPath(), newpath.c_str()); file->move(newpath); // Test attaching some user data to the file file->setUserdata(xbt_strdup("777")); - XBT_INFO("User data attached to the file: %s", (char*)file->getUserdata()); + XBT_INFO("User data attached to the file: %s", static_cast(file->getUserdata())); xbt_free(file->getUserdata()); // Close the file @@ -72,10 +72,10 @@ public: // Now attach some user data to disk1 XBT_INFO("Get/set data for storage element: %s", storage->getName()); - XBT_INFO(" Uninitialized storage data: '%s'", (char*)storage->getUserdata()); + XBT_INFO(" Uninitialized storage data: '%s'", static_cast(storage->getUserdata())); storage->setUserdata(xbt_strdup("Some user data")); - XBT_INFO(" Set and get data: '%s'", (char*)storage->getUserdata()); + XBT_INFO(" Set and get data: '%s'", static_cast(storage->getUserdata())); xbt_free(storage->getUserdata()); } diff --git a/include/simgrid/s4u/File.hpp b/include/simgrid/s4u/File.hpp index 18d2926d66..cdc9b97a8c 100644 --- a/include/simgrid/s4u/File.hpp +++ b/include/simgrid/s4u/File.hpp @@ -25,12 +25,12 @@ namespace s4u { XBT_PUBLIC_CLASS File { public: - File(const char* fullpath, void* userdata); - File(const char* fullpath, sg_host_t host, void* userdata); + File(std::string fullpath, void* userdata); + File(std::string fullpath, sg_host_t host, void* userdata); ~File(); /** Retrieves the path to the file */ - const char* getPath() { return path_; } + const char* getPath() { return path_.c_str(); } /** Simulates a local read action. Returns the size of data actually read */ sg_size_t read(sg_size_t size); @@ -53,23 +53,21 @@ public: /** Retrieves the current file position */ sg_size_t tell(); - /** Rename a file - * - * WARNING: It is forbidden to move the file to another mount point */ - void move(const char* fullpath); + /** Rename a file. WARNING: It is forbidden to move the file to another mount point */ + void move(std::string fullpath); /** Remove a file from disk */ int unlink(); - const char* storage_type; - const char* storageId; + std::string storage_type; + std::string storageId; std::string mount_point; int desc_id = 0; private: surf_file_t pimpl_ = nullptr; - const char* path_ = nullptr; - void* userdata_ = nullptr; + std::string path_; + void* userdata_ = nullptr; }; } } // namespace simgrid::s4u diff --git a/include/simgrid/s4u/Storage.hpp b/include/simgrid/s4u/Storage.hpp index 38e30b7e49..efad7b826d 100644 --- a/include/simgrid/s4u/Storage.hpp +++ b/include/simgrid/s4u/Storage.hpp @@ -28,7 +28,7 @@ public: explicit Storage(surf::StorageImpl * pimpl) : pimpl_(pimpl) {} virtual ~Storage() = default; /** Retrieve a Storage by its name. It must exist in the platform file */ - static Storage* byName(const char* name); + static Storage* byName(std::string name); const char* getName(); const char* getType(); Host* getHost(); diff --git a/src/msg/msg_io.cpp b/src/msg/msg_io.cpp index 776adfe90b..3b06983292 100644 --- a/src/msg/msg_io.cpp +++ b/src/msg/msg_io.cpp @@ -73,7 +73,8 @@ void MSG_file_dump (msg_file_t fd){ "\t\tStorage Id: '%s'\n" "\t\tStorage Type: '%s'\n" "\t\tFile Descriptor Id: %d", - fd->getPath(), fd->size(), fd->mount_point.c_str(), fd->storageId, fd->storage_type, fd->desc_id); + fd->getPath(), fd->size(), fd->mount_point.c_str(), fd->storageId.c_str(), fd->storage_type.c_str(), + fd->desc_id); } /** \ingroup msg_file diff --git a/src/s4u/s4u_file.cpp b/src/s4u/s4u_file.cpp index 043f6d5d8d..35f59e74d7 100644 --- a/src/s4u/s4u_file.cpp +++ b/src/s4u/s4u_file.cpp @@ -17,19 +17,19 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_file,"S4U files"); namespace simgrid { namespace s4u { -File::File(const char* fullpath, void* userdata) : File(fullpath, Host::current(), userdata){}; +File::File(std::string fullpath, void* userdata) : File(fullpath, Host::current(), userdata){}; -File::File(const char* fullpath, sg_host_t host, void* userdata) : path_(fullpath), userdata_(userdata) +File::File(std::string fullpath, sg_host_t host, void* userdata) : path_(fullpath), userdata_(userdata) { // this cannot fail because we get a xbt_die if the mountpoint does not exist Storage* st = nullptr; size_t longest_prefix_length = 0; std::string path; - XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, host->getCname()); + XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath.c_str(), host->getCname()); for (auto mnt : host->getMountedStorages()) { XBT_DEBUG("See '%s'", mnt.first.c_str()); - mount_point = std::string(fullpath).substr(0, mnt.first.size()); + mount_point = fullpath.substr(0, mnt.first.length()); if (mount_point == mnt.first && mnt.first.length() > longest_prefix_length) { /* The current mount name is found in the full path and is bigger than the previous*/ @@ -38,10 +38,10 @@ File::File(const char* fullpath, sg_host_t host, void* userdata) : path_(fullpat } } if (longest_prefix_length > 0) { /* Mount point found, split fullpath into mount_name and path+filename*/ - mount_point = std::string(fullpath).substr(0, longest_prefix_length); - path = std::string(fullpath).substr(longest_prefix_length, strlen(fullpath)); + mount_point = fullpath.substr(0, longest_prefix_length); + path = fullpath.substr(longest_prefix_length, fullpath.length()); } else - xbt_die("Can't find mount point for '%s' on '%s'", fullpath, host->getCname()); + xbt_die("Can't find mount point for '%s' on '%s'", fullpath.c_str(), host->getCname()); pimpl_ = simgrid::simix::kernelImmediate([this, st, path] { return new simgrid::surf::FileImpl(st, path, mount_point); }); @@ -84,7 +84,7 @@ sg_size_t File::tell() return simgrid::simix::kernelImmediate([this] { return pimpl_->tell(); }); } -void File::move(const char* fullpath) +void File::move(std::string fullpath) { simgrid::simix::kernelImmediate([this, fullpath] { pimpl_->move(fullpath); }); } diff --git a/src/s4u/s4u_storage.cpp b/src/s4u/s4u_storage.cpp index 38be6654ba..3890ce3c90 100644 --- a/src/s4u/s4u_storage.cpp +++ b/src/s4u/s4u_storage.cpp @@ -22,7 +22,7 @@ std::map* allStorages() return res; } -Storage* Storage::byName(const char* name) +Storage* Storage::byName(std::string name) { surf::StorageImpl* res = surf::StorageImpl::byName(name); if (res == nullptr) diff --git a/src/surf/FileImpl.cpp b/src/surf/FileImpl.cpp index fbb70a7e8f..ff0a830369 100644 --- a/src/surf/FileImpl.cpp +++ b/src/surf/FileImpl.cpp @@ -92,23 +92,23 @@ int FileImpl::unlink() } } -void FileImpl::move(const char* fullpath) +void FileImpl::move(std::string fullpath) { /* Check if the new full path is on the same mount point */ - if (not strncmp(mount_point_.c_str(), fullpath, mount_point_.size())) { + if (not strncmp(mount_point_.c_str(), fullpath.c_str(), mount_point_.size())) { std::map* content = location_->getContent(); auto sz = content->find(path_); if (sz != content->end()) { // src file exists sg_size_t new_size = sz->second; content->erase(path_); - std::string path = std::string(fullpath).substr(mount_point_.size(), strlen(fullpath)); + std::string path = fullpath.substr(mount_point_.length(), fullpath.length()); content->insert({path.c_str(), new_size}); - XBT_DEBUG("Move file from %s to %s, size '%llu'", path_.c_str(), fullpath, new_size); + XBT_DEBUG("Move file from %s to %s, size '%llu'", path_.c_str(), fullpath.c_str(), new_size); } else { XBT_WARN("File %s doesn't exist", path_.c_str()); } } else { - XBT_WARN("New full path %s is not on the same mount point: %s.", fullpath, mount_point_.c_str()); + XBT_WARN("New full path %s is not on the same mount point: %s.", fullpath.c_str(), mount_point_.c_str()); } } } diff --git a/src/surf/FileImpl.hpp b/src/surf/FileImpl.hpp index 0478ea1097..6b2f9cce25 100644 --- a/src/surf/FileImpl.hpp +++ b/src/surf/FileImpl.hpp @@ -28,7 +28,7 @@ public: sg_size_t tell() { return current_position_; } int seek(sg_offset_t offset, int origin); int unlink(); - void move(const char* fullpath); + void move(std::string fullpath); Action* read(sg_size_t size); Action* write(sg_size_t size); diff --git a/src/surf/StorageImpl.cpp b/src/surf/StorageImpl.cpp index b162a6e2e9..112198994a 100644 --- a/src/surf/StorageImpl.cpp +++ b/src/surf/StorageImpl.cpp @@ -32,7 +32,7 @@ simgrid::xbt::signal storage std::unordered_map* StorageImpl::storages = new std::unordered_map(); -StorageImpl* StorageImpl::byName(const char* name) +StorageImpl* StorageImpl::byName(std::string name) { if (storages->find(name) == storages->end()) return nullptr; diff --git a/src/surf/StorageImpl.hpp b/src/surf/StorageImpl.hpp index da1f082222..b74d624435 100644 --- a/src/surf/StorageImpl.hpp +++ b/src/surf/StorageImpl.hpp @@ -91,7 +91,7 @@ public: /** @brief Public interface */ s4u::Storage piface_; - static StorageImpl* byName(const char* name); + static StorageImpl* byName(std::string name); /** @brief Check if the Storage is used (if an action currently uses its resources) */ bool isUsed() override;