A terminal style view onto the filesystem, with the concept of current working directory. File systems can be mounted to root using libraries such as LocalFileSystem and SDFileSystem. Initial version only allows changing the current working directory and retrieving a directory listing. Required libraries: <<library /users/Phlaphead/libraries/Util/latest>> library.

Files at this revision

API Documentation at this revision

Comitter:
Phlaphead
Date:
Sun Jun 19 13:35:48 2011 +0000
Commit message:
Initial revision. Incomplete version, allows manipulation of working directory and listing files only.

Changed in this revision

File.cpp Show annotated file Show diff for this revision Revisions of this file
File.h Show annotated file Show diff for this revision Revisions of this file
FileSystemInterface.cpp Show annotated file Show diff for this revision Revisions of this file
FileSystemInterface.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/File.cpp	Sun Jun 19 13:35:48 2011 +0000
@@ -0,0 +1,8 @@
+
+#include "File.h"
+
+File::File(string& name, FileType type)
+{
+    m_name = name;
+    m_type = type;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/File.h	Sun Jun 19 13:35:48 2011 +0000
@@ -0,0 +1,65 @@
+/*
+Copyright (c) 2011 Robert Ellis (holistic [at] robellis [dot] org [dot] uk)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef FILE_H
+#define FILE_H
+
+#include <string>
+using namespace std;
+
+/**
+ * Enumeration of file types.
+ */
+enum FileType { TYPE_FILE, TYPE_DIR };
+
+/**
+ * File class to hold information about a file.
+ */
+class File
+{
+public:
+
+    /**
+     * Constructor.
+     */
+    File(string& name, FileType type);
+    
+    /**
+     * Get file name.
+     */
+    string& getName() { return m_name; }
+    
+    /**
+     * Get type. One of TYPE_FILE or TYPE_DIR.
+     */
+    FileType getType() { return m_type; }
+    
+private:
+
+    string m_name;
+    FileType m_type;
+
+};
+
+
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FileSystemInterface.cpp	Sun Jun 19 13:35:48 2011 +0000
@@ -0,0 +1,143 @@
+
+#include "FileSystemInterface.h"
+#include "mbed.h"
+#include "stringUtils.h"
+
+#define __DEBUG
+#include "dbg/dbg.h"
+
+FileSystemInterface::FileSystemInterface()
+{
+    m_workDirString = getPathAsString(m_workDir);
+}
+
+string FileSystemInterface::getPathAsString(vector<string>& path)
+{
+    string s_path = "/";
+    
+    for(vector<string>::iterator it = path.begin(); it != path.end(); it++)
+    {
+        string& directory = *it;
+        s_path += directory;
+        s_path += "/";
+    }
+    
+    return s_path;
+}
+
+
+string FileSystemInterface::getWorkingDirectory()
+{
+    return m_workDirString;
+}
+
+bool FileSystemInterface::changeWorkingDirectory(const string& dir)
+{
+    bool successful = false;
+    
+    DBG("%d\r\n", dir.find_first_of("/"));
+    
+    if(dir.compare("/") == 0)
+    {
+        //Root path
+        m_workDir.clear();
+        successful = true;
+    }
+    else if(dir.find_first_of("/") == 0)
+    {
+        //Absolute path
+        if(isDir(dir))
+        {
+            m_workDir.clear();
+            m_workDir = tokenize(dir, "/");
+            successful = true;
+        }
+    }
+    else if(dir.compare("..") == 0)
+    {
+        //Parent directory
+        if(!m_workDir.empty())
+        {
+            m_workDir.pop_back();
+            successful = true;
+        }
+    }
+    else
+    {
+        //Relative Path
+        string path = m_workDirString + dir;
+        if(isDir(path))
+        {
+            m_workDir.push_back(dir);
+            successful = true;
+        }
+    }
+    
+    if(successful)
+    {
+        m_workDirString = getPathAsString(m_workDir);
+    }
+    
+    return successful;
+}
+
+
+list<File>& FileSystemInterface::getFileList()
+{
+    DIR* directory;
+    struct dirent* entry;
+    FileType type;
+    m_ls.clear();
+    
+    directory = opendir(m_workDirString.c_str());
+    if (directory != NULL) 
+    {
+        while ((entry = readdir(directory)) != NULL) 
+        {
+            string name = entry->d_name;
+            string path = m_workDirString + name;
+            
+            if(isFile(path))
+            {
+                type = TYPE_FILE;
+            }
+            else
+            {
+                type = TYPE_DIR;
+            }
+            
+            m_ls.push_back(File(name, type));
+        }
+    } 
+    else 
+    {
+        //printf("Could not open directory!\n");
+    }
+    
+    closedir(directory);
+    return m_ls;
+}
+
+
+bool FileSystemInterface::isFile(const string& path)
+{
+    FILE *fp = fopen(path.c_str(), "r");
+    DBG("%s : %d\r\n", path.c_str(), fp);
+    bool isFile = (fp != NULL);
+    if(fp)
+    {
+        fclose(fp);
+    }
+    return isFile;
+}
+
+bool FileSystemInterface::isDir(const string& path)
+{
+    DIR *dir = opendir(path.c_str());
+    bool isDir = (dir != NULL);
+    if(dir)
+    {
+        closedir(dir);
+    }
+    return isDir;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FileSystemInterface.h	Sun Jun 19 13:35:48 2011 +0000
@@ -0,0 +1,77 @@
+/*
+Copyright (c) 2011 Robert Ellis (holistic [at] robellis [dot] org [dot] uk)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef FILE_SYSTEM_INTERFACE_H
+#define FILE_SYSTEM_INTERFACE_H
+
+#include "File.h"
+
+#include <string>
+#include <vector>
+#include <list>
+using namespace std;
+
+/**
+ * A terminal style view onto the filesystem, with the concept of current working directory.
+ */
+class FileSystemInterface
+{
+public:
+
+    /**
+     * Constructor.
+     */
+    FileSystemInterface();
+    
+    /**
+     * Get the current working directory.
+     */
+    string getWorkingDirectory();
+    
+    /**
+     * Change the working directory.
+     * @param dir The directory to change to. Can be relative to the current working directory or absolute (begins with '/'). '..' moves to the parent directory.
+     */
+    bool changeWorkingDirectory(const string& dir);
+    
+    /**
+     * Get a list of files and directories in the current working directory.
+     */
+    list<File>& getFileList();
+    
+    
+private:
+
+    string getPathAsString(vector<string>& path);
+    bool isFile(const string& path);
+    bool isDir(const string& path);
+
+
+    vector<string> m_workDir; //current working path
+    string m_workDirString;
+    list<File> m_ls;
+
+};
+
+
+
+#endif