r3346 - in trunk/libmnetutil: include/libmnetutil source
mikaelsv at minisip.org
mikaelsv at minisip.org
Fri Jun 29 18:46:53 CEST 2007
Author: mikaelsv
Date: 2007-06-29 18:46:52 +0200 (Fri, 29 Jun 2007)
New Revision: 3346
Modified:
trunk/libmnetutil/include/libmnetutil/FileDownloader.h
trunk/libmnetutil/include/libmnetutil/FileUrl.h
trunk/libmnetutil/source/FileDownloader.cxx
trunk/libmnetutil/source/FileUrl.cxx
Log:
* Added documentation
Modified: trunk/libmnetutil/include/libmnetutil/FileDownloader.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/FileDownloader.h 2007-06-29 15:23:30 UTC (rev 3345)
+++ trunk/libmnetutil/include/libmnetutil/FileDownloader.h 2007-06-29 16:46:52 UTC (rev 3346)
@@ -49,6 +49,8 @@
/**
* Returns the data pointed to by the URL supplied to the constructor.
*
+ * This function allocates enough memory to keep the entire source file in memory.
+ *
* @param length Pointer to integer that will be used to return
* the length of the returned character array.
*/
@@ -57,6 +59,9 @@
/**
* Fetches \em binary data from LDAP server and saves as files on local computer.
*
+ * This functions has a small memory-footprint as it copies one "chunk" at a time
+ * from the source file.
+ *
* @note The function replaces existing files without informing the user.
* @param filename The path to where the retrieved file should be stored.
*/
Modified: trunk/libmnetutil/include/libmnetutil/FileUrl.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/FileUrl.h 2007-06-29 15:23:30 UTC (rev 3345)
+++ trunk/libmnetutil/include/libmnetutil/FileUrl.h 2007-06-29 16:46:52 UTC (rev 3346)
@@ -36,6 +36,10 @@
* incorrect URI "file:////applib/products/a%2Db/abc%5F9/4148.920a/media/start.swf"
* is one exampel of this, as the URI considered valid by the class.
*
+ * When it says that this class handles "Windows and UNIX addresses" it means that the
+ * class converts the URI to paths appropriate for each operating system. For Windows
+ * URLs this means that all forward-slashes ("/") are converted to back-slashes ("\").
+ *
* The type attribute must be set to FILEURL_TYPE_WINDOWS in order for the function to
* properly handle Windows file paths. Otherwise UNIX paths are assumed/returned.
*
@@ -43,8 +47,21 @@
*/
class FileUrl {
public:
+ /**
+ * Constructor when we already have a URL but it is unclear for which system
+ * (e.g. Windows or Unix) the file adheres.
+ */
FileUrl(const std::string url);
+
+ /**
+ * Constructor when we already have a URL and we know it points to a file in
+ * on a Windows system or Unix system.
+ */
FileUrl(const std::string url, const int32_t type);
+
+ /**
+ * Generic constructor when we want to create a new URL from scratch.
+ */
FileUrl();
/**
@@ -55,7 +72,10 @@
/**
* Tests whether or not the currenct FileUrl instance represents a valid file URL.
*
- * At the moment a file URL is considered invalid only if it doesn't start with "file://".
+ * A URL is considered invalid if:
+ * - it doesn't start with "file://".
+ * - it doesn't have a "host part"
+ * - it doesn't have a "path part"
*/
bool isValid() const;
@@ -79,6 +99,13 @@
std::string getHost() const;
void setHost(const std::string host);
+ /**
+ * Returns the path part of the URL. The returned value conforms to the
+ * path syntax of the operating system specified by the \c type property.
+ *
+ * Thus, if \c type = \c FILEURL_TYPE_WINDOWS then the function may return something like this:
+ * <tt>C:\Program Files\Music\funky_tune.mp3</tt>
+ */
std::string getPath() const;
void setPath(const std::string path);
Modified: trunk/libmnetutil/source/FileDownloader.cxx
===================================================================
--- trunk/libmnetutil/source/FileDownloader.cxx 2007-06-29 15:23:30 UTC (rev 3345)
+++ trunk/libmnetutil/source/FileDownloader.cxx 2007-06-29 16:46:52 UTC (rev 3346)
@@ -30,13 +30,20 @@
int32_t fileLen = 0;
+ // Determine size of file
inStream.seekg(0, std::ios::end);
fileLen = inStream.tellg();
inStream.seekg(0, std::ios::beg);
*length = fileLen;
+
+ // Allocate enough memory to keep the entire file in memory
char* res = new char[*length];
+
+ // Read from file directly into memory
inStream.read(res, *length);
+
+ // Test if the input stream is still good, meaning that no error occurred.
if (!inStream.good()) {
inStream.close();
return NULL;
@@ -51,6 +58,9 @@
}
}
+/**
+ * @todo Merge this function with saveToFile.
+ */
bool FileDownloader::fetch(std::ostream & outStream) {
if (url.isValid()) {
@@ -58,24 +68,30 @@
std::ifstream inStream(url.getPath().c_str(), std::ios::in);
if (inStream.is_open()) {
- int32_t bytesRead = 0;
int32_t fileLen = 0;
- int32_t nextReadLen = BUFFERSIZE;
+ // Determine size of file
inStream.seekg(0, std::ios::end);
fileLen = inStream.tellg();
inStream.seekg(0, std::ios::beg);
+ int32_t bytesRead = 0;
+ int32_t nextReadLen = BUFFERSIZE;
+
char buffer[BUFFERSIZE];
memset(buffer, 0, sizeof(buffer)); // Zero out the buffer used when recieving data
bool error = false;
do {
+ // Test if it is possible to read BUFFERSIZE bytes from the file
if (bytesRead + BUFFERSIZE > fileLen)
nextReadLen = fileLen - bytesRead;
+ // Read bytes from input file and immediately write them to the output file
inStream.read(buffer, nextReadLen);
outStream.write(buffer, nextReadLen);
+
+ // Test if the streams are still OK.
if (!inStream.good() || !outStream.good()) {
error = true;
break;
@@ -94,6 +110,9 @@
}
}
+/**
+ * @todo Merge this function with fetch.
+ */
void FileDownloader::saveToFile(std::string filename) throw (FileDownloaderException) {
std::ofstream file(filename.c_str());
if (file.good()) {
Modified: trunk/libmnetutil/source/FileUrl.cxx
===================================================================
--- trunk/libmnetutil/source/FileUrl.cxx 2007-06-29 15:23:30 UTC (rev 3345)
+++ trunk/libmnetutil/source/FileUrl.cxx 2007-06-29 16:46:52 UTC (rev 3346)
@@ -38,13 +38,17 @@
// Append distinguished name (base DN)
url += '/';
+ // Split path into parts using "\" in Windows and "/" on other system
char sep = (type == FILEURL_TYPE_WINDOWS ? '\\' : '/');
std::vector<std::string> parts = split(path, false, sep, true);
+ // Glue together each "path part" again
for (int i=0; i<parts.size(); i++) {
std::string decPart = percentEncode(parts.at(i));
url += decPart + '/';
}
+
+ // Strip away the final trailing "/"
url = url.substr(0, url.length() - 1);
return url;
@@ -94,8 +98,10 @@
if (lastPos < url.length()) {
std::string restOfUrl = url.substr(lastPos);
+ // Split the "path part of the URL" into pieces, each piece separated by "/" as specified by RFC 1738.
std::vector<std::string> parts = split(restOfUrl, false, '/', true);
+ // Glue the pieces together using an operating-system specific separator
for (int i=0; i<parts.size(); i++) {
std::string decPart = percentDecode(parts.at(i));
if (type == FILEURL_TYPE_WINDOWS) {
@@ -105,6 +111,7 @@
}
}
if (path.length() > 1) {
+ // Remove trailing "path separator character"
path = path.substr(0, path.length() - 1);
} else {
validUrl = false;
More information about the Minisip-devel
mailing list