|
|
|
@ -12,7 +12,7 @@ using namespace std; |
|
|
|
|
namespace marcelb { |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asynchronous file reading |
|
|
|
|
* Asynchronous file reading with callback after read complete |
|
|
|
|
*/ |
|
|
|
|
template<typename Callback> |
|
|
|
|
void asynco_read(string path, Callback&& callback) { |
|
|
|
@ -40,8 +40,32 @@ void asynco_read(string path, Callback&& callback) { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asynchronous file reading |
|
|
|
|
*/ |
|
|
|
|
future<string> asynco_read(string path) { |
|
|
|
|
return asynco( [&path] () { |
|
|
|
|
string content; |
|
|
|
|
string line; |
|
|
|
|
ifstream file (path); |
|
|
|
|
if (file.is_open()) { |
|
|
|
|
line.clear(); |
|
|
|
|
while ( getline (file,line) ) { |
|
|
|
|
content += line + "\n"; |
|
|
|
|
} |
|
|
|
|
file.close(); |
|
|
|
|
return content; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
else { |
|
|
|
|
throw runtime_error("Unable to open file");
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asynchronous file writing |
|
|
|
|
* Asynchronous file writing with callback after write complete |
|
|
|
|
*/ |
|
|
|
|
template<typename Callback> |
|
|
|
|
void asynco_write(string path, string content, Callback&& callback) { |
|
|
|
@ -63,6 +87,25 @@ void asynco_write(string path, string content, Callback&& callback) { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asynchronous file writing with callback after write complete |
|
|
|
|
*/ |
|
|
|
|
future<void> asynco_write(string path, string content) { |
|
|
|
|
return asynco( [&path, &content] () { |
|
|
|
|
ofstream file (path); |
|
|
|
|
if (file.is_open()) { |
|
|
|
|
file << content; |
|
|
|
|
file.close(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
throw runtime_error("Unable to open file");
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endif |