Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

working on linux, should work on mac, no way it's going to work on wi…

…ndows
  • Loading branch information...
commit 0296dbf5bef6471f47f890fe33864152fbc94d4c 0 parents
@arturoc authored
2,556 .cproject
2,556 additions, 0 deletions not shown
3  .gitignore
@@ -0,0 +1,3 @@
+Debug*/
+Release*/
+bin/
82 .project
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>ofxFileSystem</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
+ <triggers>clean,full,incremental,</triggers>
+ <arguments>
+ <dictionary>
+ <key>?name?</key>
+ <value></value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.append_environment</key>
+ <value>true</value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.autoBuildTarget</key>
+ <value>all</value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.buildArguments</key>
+ <value></value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.buildCommand</key>
+ <value>make</value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.buildLocation</key>
+ <value>${workspace_loc:/ofAddons/Debug}</value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.cleanBuildTarget</key>
+ <value>clean</value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.contents</key>
+ <value>org.eclipse.cdt.make.core.activeConfigSettings</value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.enableAutoBuild</key>
+ <value>false</value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.enableCleanBuild</key>
+ <value>true</value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.enableFullBuild</key>
+ <value>true</value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.fullBuildTarget</key>
+ <value>all</value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.stopOnError</key>
+ <value>true</value>
+ </dictionary>
+ <dictionary>
+ <key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
+ <value>true</value>
+ </dictionary>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.cdt.core.cnature</nature>
+ <nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
+ <nature>org.eclipse.cdt.core.ccnature</nature>
+ <nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
+ </natures>
+</projectDescription>
21 example/main.cpp
@@ -0,0 +1,21 @@
+#include "ofMain.h"
+#include "testApp.h"
+
+
+
+
+ int main()
+ {
+ // this kicks off the running of my app
+ // can be OF_WINDOW or OF_FULLSCREEN
+ // pass in width and height too:
+ ofSetupOpenGL(320,200, OF_WINDOW); // <-------- setup the GL context
+
+ // this is my "app" :
+ testApp *APP = new testApp;;
+
+ ofRunApp(APP);
+
+ }
+
+
97 example/testApp.cpp
@@ -0,0 +1,97 @@
+#include "testApp.h"
+
+
+//--------------------------------------------------------------
+void testApp::setup(){
+ cout << "testing file copy" << endl;
+ file.openReadOnly("folder1/prueba.txt");
+ file.copyTo("folder2/copia.txt");
+ cout << endl;
+
+ cout << "----------------------------------------------" << endl;
+ cout << "testing file name and path" << endl;
+ cout << file.getName() << " - path: " << file.getPath() << endl;
+ cout << endl;
+
+ cout << "----------------------------------------------" << endl;
+ cout << "testing text file content" << endl;
+ cout << file.getTextContent()<< endl;
+ file.close();
+ cout << endl;
+
+
+ cout << "----------------------------------------------" << endl;
+ cout << "testing binary content" << endl;
+ file.openReadOnly("folder1/ofw-logo.gif");
+ vector<char> image = file.getContent();
+ cout << "size of read file: " << image.size() << endl;
+ file.close();
+ cout << endl;
+
+
+ cout << "----------------------------------------------" << endl;
+ cout << "testing dir listing" << endl;
+ dir.open("folder1");
+ cout << "directories:" << endl;
+ for(int i=0; i<dir.getNumDirs(); i++){
+ cout << dir.getDirName(i) << endl;
+ }
+ cout << endl;
+ cout << "files:" << endl;
+ for(int i=0; i<dir.getNumFiles(); i++){
+ cout << dir.getFileName(i) << endl;
+ }
+ cout << endl;
+
+ cout << "----------------------------------------------" << endl;
+ cout << "testing dir copy" << endl;
+ dir.copyTo("folder3");
+ dir.copyTo("folder4");
+ dir.close();
+
+ cout << "----------------------------------------------" << endl;
+ cout << "testing dir removing" << endl;
+ dir.open("folder4");
+ dir.remove();
+}
+
+//--------------------------------------------------------------
+void testApp::update(){
+
+}
+
+//--------------------------------------------------------------
+void testApp::draw(){
+
+
+}
+
+
+//--------------------------------------------------------------
+void testApp::keyPressed(int key){
+
+
+}
+
+//--------------------------------------------------------------
+void testApp::keyReleased(int key){
+
+}
+
+//--------------------------------------------------------------
+void testApp::mouseMoved(int x, int y ){
+}
+
+//--------------------------------------------------------------
+void testApp::mouseDragged(int x, int y, int button){
+}
+
+//--------------------------------------------------------------
+void testApp::mousePressed(int x, int y, int button){
+}
+
+//--------------------------------------------------------------
+void testApp::mouseReleased(){
+}
+
+
30 example/testApp.h
@@ -0,0 +1,30 @@
+#ifndef _TEST_APP
+#define _TEST_APP
+
+#include "ofMain.h"
+
+#include "ofxFile.h"
+#include "ofxDir.h"
+
+class testApp : public ofBaseApp{
+
+ public:
+
+ void setup();
+ void update();
+ void draw();
+
+ void keyPressed(int key);
+ void keyReleased(int key);
+ void mouseMoved(int x, int y );
+ void mouseDragged(int x, int y, int button);
+ void mousePressed(int x, int y, int button);
+ void mouseReleased();
+
+
+ ofxFile file;
+ ofxDir dir;
+};
+
+#endif
+
282 src/ofxDir.cpp
@@ -0,0 +1,282 @@
+/*
+ * ofxDir.cpp
+ *
+ * Created on: 29/11/2009
+ * Author: reactable
+ */
+
+#include "ofxDir.h"
+
+#ifdef TARGET_WIN32
+ #include <stdio.h>
+ #include <iostream>
+ #include <string.h>
+#else
+ #include <dirent.h>
+#endif
+
+static std::string::size_type idx;
+static string getExt(string filename){
+ idx = filename.rfind('.');
+
+ if(idx != std::string::npos){
+ return filename.substr(idx+1);
+ }
+ else{
+ return "";
+ }
+}
+
+static string strToLower(string myStr){
+ transform( myStr.begin(), myStr.end(), myStr.begin(), ::tolower );
+ return myStr;
+}
+
+ofxDir::ofxDir() {
+ // TODO Auto-generated constructor stub
+
+}
+
+ofxDir::~ofxDir() {
+ // TODO Auto-generated destructor stub
+}
+
+bool ofxDir::open(string directory){
+ directory = ofToDataPath(directory,true);
+
+ files.clear();
+ directories.clear();
+
+ if(directory.length() <= 0)return 0;
+
+ //if the trailing slash was not added - then add it
+ if( directory[directory.length()-1] != '/'){
+ directory = directory + "/";
+ }
+
+ DIR *dir = NULL;
+ struct dirent *entry;
+
+ //open the directory
+ ofLog(OF_LOG_VERBOSE, "ofxDir - attempting to open %s", directory.c_str());
+ dir = opendir(directory.c_str());
+
+ if(dir == NULL){
+ //try to create it
+ mkdir(directory.c_str(),S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
+ dir = opendir(directory.c_str());
+ if(dir==NULL){
+ ofLog(OF_LOG_ERROR, "ofxDir - error opening directory");
+ return false;
+ }
+ }
+
+ ofLog(OF_LOG_VERBOSE, "ofxDir - success opening directory");
+
+
+ string entry_name = "";
+ string ext = "";
+
+ //by default we don't skip files unless we are checking extensions
+ bool skip = false;
+
+ while ((entry = readdir(dir)) != NULL){
+
+ //turn it into a C++ string
+ entry_name = entry->d_name;
+
+ //lets get the length of the string here as we query it again
+ int fileLen = entry_name.length();
+
+ if(fileLen <= 0)continue; //if the name is not existant
+ if(entry_name[0] == '.')continue; //ignore invisible files, ./ and ../
+
+ #ifdef _DIRENT_HAVE_D_TYPE
+ if(entry->d_type==DT_DIR){
+ directories.push_back(entry_name);
+ continue;
+ }
+ #endif
+
+ skip = false;
+
+ if(allowedFileExt.size() > 0){
+ //we will skip this files unless it has an allowed extension
+ skip = true;
+ for(int i = 0; i < (int)allowedFileExt.size(); i++){
+
+ //if the wildecard * has been entered for an ext type then don't check any extensions
+ if( allowedFileExt[i] == "*"){ skip = false; break; }
+
+
+ int extLen = allowedFileExt[i].length();
+
+ //the extension has to be shorter than the filename - simple check
+ if(extLen >= fileLen) continue;
+
+ //lets get the ext as lowercase
+ ext = strToLower( getExt(entry_name) );
+
+ //if no ext - then skip this ext check
+ if( ext == "" )continue;
+
+ //if we find a match then stop checking and approve this file
+ if(ext == allowedFileExt[i]){
+ skip = false;
+ break;
+ }
+ }
+ }
+
+ if(skip) continue;
+
+ files.push_back(entry_name);
+
+ //ofLog(OF_LOG_VERBOSE, "ofxDirList - listing %s ", nameArray.back().c_str());
+ }
+
+ this->path=directory;
+ name = this->path.substr(this->path.rfind('/')+1);
+ //ofLog(OF_LOG_VERBOSE, "ofxDirList - listed %i files in %s", nameArray.size(), directory.c_str());
+ return true;
+}
+
+void ofxDir::close(){
+ files.clear();
+ directories.clear();
+ allowedFileExt.clear();
+ name.clear();
+ path.clear();
+}
+
+int ofxDir::getNumDirs(){
+ return directories.size();
+}
+
+int ofxDir::getNumFiles(){
+ return files.size();
+}
+
+ofxFile ofxDir::getFile(int num){
+ ofxFile file;
+ if(num < (int)files.size())
+ file.openReadOnly(path + files[num]);
+ return file;
+}
+
+ofxFile ofxDir::getFile(const string & name){
+ ofxFile file;
+ file.openReadOnly(path + name);
+ return file;
+}
+
+string ofxDir::getFileName(int num){
+ return files[num];
+}
+
+string ofxDir::getFilePath(int num){
+ return path + files[num];
+}
+
+ofxDir ofxDir::getDir(int num){
+ ofxDir dir;
+ if(num < (int)files.size())
+ dir.open(path + directories[num]);
+ return dir;
+}
+
+ofxDir ofxDir::getDir(const string & name){
+ ofxDir dir;
+ dir.open(path + name);
+ return dir;
+}
+
+string ofxDir::getDirName(int num){
+ return directories[num];
+}
+
+string ofxDir::getDirPath(int num){
+ return path + directories[num];
+}
+
+bool ofxDir::fileExists(const string & name){
+ for(unsigned i=0; i<files.size(); i++){
+ if(files[i]==name) return true;
+ }
+ return false;
+}
+
+bool ofxDir::dirExists(const string & name){
+ for(unsigned i=0; i<directories.size(); i++){
+ if(directories[i]==name) return true;
+ }
+ return false;
+}
+
+bool ofxDir::createFile(const string & name){
+ ofxFile file;
+ return file.openReadOnly(path + name);
+}
+
+bool ofxDir::createDir(const string & name){
+ if(name.find("/")!=string::npos){
+ ofLog(OF_LOG_ERROR,"can only create directories directly in this path");
+ return false;
+ }
+ return mkdir((path + name).c_str(),S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)!=-1;
+}
+
+bool ofxDir::remove(const string & name){
+ if(name=="."){
+ for(unsigned i=0;i<files.size(); i++){
+ unlink((path + files[i]).c_str());
+ }
+ for(unsigned i=0;i<directories.size();i++){
+ ofxDir dir;
+ dir.open(path + directories[i]);
+ dir.remove();
+ }
+ rmdir(path.c_str());
+ return true;
+ }else{
+ if(name.find("/")!=string::npos){
+ ofLog(OF_LOG_ERROR,"only names in this directory allowed");
+ return false;
+ }
+ if(fileExists(name)){
+ unlink((path + name).c_str());
+ }
+ if(dirExists(name)){
+ getDir(name).remove(".");
+ }
+ return true;
+ }
+}
+
+bool ofxDir::copyTo(string path){
+ path = ofToDataPath(path,true);
+ if(path.length() <= 0)return false;
+
+ //if the trailing slash was not added - then add it
+ if( path[path.length()-1] != '/'){
+ path = path + "/";
+ }
+ if(mkdir(path.c_str(),S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH )==-1){
+ ofLog(OF_LOG_ERROR, "cannot create directory " + path);
+ return false;
+ }
+ bool allCopied=true;
+ for(unsigned i=0; i<files.size(); i++){
+ if(!getFile(getFileName(i)).copyTo(path + getFileName(i))){
+ ofLog(OF_LOG_WARNING, "cannot copy " + getFilePath(i) + " to " + path + getFileName(i));
+ allCopied = false;
+ }
+ }
+ for(unsigned i=0;i<directories.size(); i++){
+ if(!getDir(getDirName(i)).copyTo(path + getDirName(i))){
+ ofLog(OF_LOG_WARNING, "cannot copy " + getDirPath(i) + " to " + path + getDirName(i));
+ allCopied = false;
+ }
+ }
+ return allCopied;
+}
52 src/ofxDir.h
@@ -0,0 +1,52 @@
+/*
+ * ofxDir.h
+ *
+ * Created on: 29/11/2009
+ * Author: reactable
+ */
+
+#ifndef OFXDIR_H_
+#define OFXDIR_H_
+
+#include "ofMain.h"
+#include "ofxFile.h"
+
+class ofxDir {
+public:
+ ofxDir();
+ virtual ~ofxDir();
+
+ bool open(string path);
+ void close();
+
+ int getNumDirs();
+ int getNumFiles();
+
+ ofxFile getFile(int num);
+ ofxFile getFile(const string & name);
+ string getFileName(int num);
+ string getFilePath(int num);
+
+ ofxDir getDir(int num);
+ ofxDir getDir(const string & name);
+ string getDirName(int num);
+ string getDirPath(int num);
+
+ bool fileExists(const string & name);
+ bool dirExists(const string & name);
+
+ bool createFile(const string & name);
+ bool createDir(const string & name);
+
+ bool remove(const string & name = ".");
+ bool copyTo(string path);
+
+private:
+ vector<string> files;
+ vector<string> directories;
+ vector <string> allowedFileExt;
+ string path;
+ string name;
+};
+
+#endif /* OFXDIR_H_ */
116 src/ofxFile.cpp
@@ -0,0 +1,116 @@
+/*
+ * ofxFile.cpp
+ *
+ * Created on: 29/11/2009
+ * Author: reactable
+ */
+
+#include "ofxFile.h"
+
+ofxFile::ofxFile() {
+ file = NULL;
+}
+
+ofxFile::~ofxFile() {
+ close();
+}
+
+bool ofxFile::openReadOnly(const string & path){
+ if(file && file->is_open()){
+ if(!close()) return false;
+ }
+
+ file = new ifstream(ofToDataPath(path,true).c_str());
+
+ if(!file || !file->is_open()){
+ ofLog(OF_LOG_ERROR,"cannot open file " + ofToDataPath(path,true));
+ close();
+ return false;
+ }else{
+ this->path=ofToDataPath(path,true);
+ name = this->path.substr(this->path.rfind('/')+1);
+ return true;
+ }
+}
+//bool openWrite(const string & path);
+//bool openAppend(const string & path);
+
+bool ofxFile::close(){
+ if(!file) return true;
+ file->close();
+ if(file->is_open()){
+ ofLog(OF_LOG_ERROR,"cannot close " + path);
+ return false;
+ }
+ path = "";
+ name = "";
+ delete file;
+ file = NULL;
+ return true;
+}
+
+long ofxFile::size(){
+ if(!file) return -1;
+ filebuf *pbuf=file->rdbuf();
+
+ // get file size using buffer's members
+ long size=pbuf->pubseekoff (0,ios::end,ios::in);
+ pbuf->pubseekpos (0,ios::in);
+
+ return size;
+}
+
+vector<char> ofxFile::getContent(){
+ if(!file) return vector<char>();
+ unsigned filesize = size();
+ char * buffer = new char[filesize];
+ filebuf *pbuf=file->rdbuf();
+ pbuf->pubseekpos (0,ios::in);
+ // get file data
+ pbuf->sgetn (buffer,filesize);
+ return vector<char>(buffer,buffer+filesize);
+}
+
+long ofxFile::getContent(char*buffer){
+ if(!file) return 0;
+ int filesize = size();
+ filebuf *pbuf=file->rdbuf();
+ pbuf->pubseekpos (0,ios::in);
+ // get file data
+ pbuf->sgetn (buffer,filesize);
+ //TODO: can this be different to size()
+ return filesize;
+}
+
+string ofxFile::getTextContent(){
+ if(!file) return string();
+ stringbuf str;
+ file->seekg(0,ios::beg);
+ while(file->good()) *file >> &str;
+ return str.str();
+}
+
+bool ofxFile::copyTo(const string & newPath){
+ if(!file){
+ ofLog(OF_LOG_ERROR,"cannot copy, no opened file yet");
+ return false;
+ }
+ ofstream copy(ofToDataPath(newPath,true).c_str());
+ if(!copy.is_open()){
+ ofLog(OF_LOG_ERROR,"cannot copy " + path + " to " + newPath);
+ return false;
+ }
+ file->seekg(0,ios::beg);
+ copy << file->rdbuf();
+ copy.close();
+ return true;
+}
+
+string ofxFile::getPath(){
+ return path;
+}
+
+string ofxFile::getName(){
+ return name;
+}
+
44 src/ofxFile.h
@@ -0,0 +1,44 @@
+/*
+ * ofxFile.h
+ *
+ * Created on: 29/11/2009
+ * Author: reactable
+ */
+
+#ifndef OFXFILE_H_
+#define OFXFILE_H_
+
+#include "ofMain.h"
+#include <iostream>
+#include <fstream>
+
+class ofxFile {
+public:
+ ofxFile();
+ virtual ~ofxFile();
+
+ bool openReadOnly(const string & path);
+ //bool openWrite(const string & path);
+ //bool openAppend(const string & path);
+
+ bool close();
+
+ long size();
+
+ //easier but slower to use, for performance or really big files use char*
+ vector<char> getContent();
+ long getContent(char*buffer);
+ string getTextContent();
+
+ bool copyTo(const string & newPath);
+
+ string getPath();
+ string getName();
+
+private:
+ ifstream * file;
+ string path;
+ string name;
+};
+
+#endif /* OFXFILE_H_ */
Please sign in to comment.
Something went wrong with that request. Please try again.