Directry file listing class Instance will be a vector of string class which holds the file name of given path.

Dependents:   ika_shouyu_poppoyaki DirectoryList_Hello ika_shouyu_poppoyaki eVY1_SMF_player ... more

Information

日本語版がこのページ下半分にあります!

Japanese version is available lower half of this page.

What is this?

A class to list files of a directory (folder).

When an instance is made, it access to the directory information and keeps the file names as vector of strings.

This class library can be used for any file system which is supported by mbed.

  • LocalFileSystem
  • MSCFileSystem (USB mass storage class)
  • SD card (for the SD card operation, sample code is available.).

How to use?

Very easy to use.

  1. Make a instance with path to the directory
  2. Check error status
  3. The file names are available as vector of strings inside of instance.

#include "mbed.h"
#include "DirectoryList.h"

LocalFileSystem local( "local" );

int main(void)
{
    DirectoryList   dir( "/local" );

    if ( dir.error_check() )
        error( "directory could not be opened\r\n" );

    for ( int i = 0; i < dir.size(); i++ )
        printf( "%s\r\n", dir[ i ].c_str() );
}

Import programDirectoryList_Hello

Sample code for DirectoryList class library

Information

The file names are in 8.3 format (old DOS type file name fomat: 8 characters file name with 3 characters suffix).




これはナニ?

ディレクトリ(フォルダ)のリスト・クラスです

このインスタンスが作られるとき,ディレクトリの情報にアクセスしファイル名を文字列のベクタとして保持します.

このクラス・ライブラリは,mbedがサポートしているファイル・システムであれば,そのどれにも使うことができます. This class can be used for any file system which is supported by mbed.

  • LocalFileSystem
  • MSCFileSystem (USBマスストレージ・クラス)
  • SD card (SDカード操作のサンプルコードも用意されています.).

どうやって使う?

とても簡単に使うことができます.

  1. ディレクトリのパスを指定してインスタンスを作る
  2. エラーが起きなかったかチェック
  3. インスタンス内部にファイル名のリストがStringsのベクタとして保持されます

#include "mbed.h"
#include "DirectoryList.h"

LocalFileSystem local( "local" );

int main(void)
{
    DirectoryList   dir( "/local" );

    if ( dir.error_check() )
        error( "directory could not be opened\r\n" );

    for ( int i = 0; i < dir.size(); i++ )
        printf( "%s\r\n", dir[ i ].c_str() );
}

Import programDirectoryList_Hello

Sample code for DirectoryList class library

Information

ファイル名は8.3フォーマット(古いDOS形式のファイル名フォーマット: 8文字のファイル名+3文字の拡張子)となります.

Committer:
MACRUM
Date:
Fri Jul 17 06:05:59 2015 +0000
Revision:
1:0233769f991c
Parent:
0:3cd1685a4c22
Add DirHandle.h inclusion

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 0:3cd1685a4c22 1 /**
okano 0:3cd1685a4c22 2 * DirectoryList library
okano 0:3cd1685a4c22 3 *
okano 0:3cd1685a4c22 4 * @author Tedd OKANO
okano 0:3cd1685a4c22 5 * @version 0.1
okano 0:3cd1685a4c22 6 * @date Jan-2015
okano 0:3cd1685a4c22 7 *
okano 0:3cd1685a4c22 8 * A simple directory listing interface
okano 0:3cd1685a4c22 9 */
okano 0:3cd1685a4c22 10
okano 0:3cd1685a4c22 11 #include "mbed.h"
okano 0:3cd1685a4c22 12 #include "DirectoryList.h"
okano 0:3cd1685a4c22 13 #include <algorithm>
okano 0:3cd1685a4c22 14
okano 0:3cd1685a4c22 15 DirectoryList::DirectoryList( char *path ) : error_flag( NOT_INITIALIZED )
okano 0:3cd1685a4c22 16 {
okano 0:3cd1685a4c22 17 init( string( path ) );
okano 0:3cd1685a4c22 18 }
okano 0:3cd1685a4c22 19
okano 0:3cd1685a4c22 20 DirectoryList::DirectoryList( std::string path ) : error_flag( NOT_INITIALIZED )
okano 0:3cd1685a4c22 21 {
okano 0:3cd1685a4c22 22 init( path );
okano 0:3cd1685a4c22 23 }
okano 0:3cd1685a4c22 24
okano 0:3cd1685a4c22 25 void DirectoryList::init( std::string path )
okano 0:3cd1685a4c22 26 {
okano 0:3cd1685a4c22 27 DIR *dir;
okano 0:3cd1685a4c22 28 struct dirent *dp;
okano 0:3cd1685a4c22 29
okano 0:3cd1685a4c22 30 if ( NULL == (dir = opendir( path.c_str() )) ) {
okano 0:3cd1685a4c22 31 error_flag = ERROR_AT_FILE_OPEN;
okano 0:3cd1685a4c22 32 return;
okano 0:3cd1685a4c22 33 }
okano 0:3cd1685a4c22 34
okano 0:3cd1685a4c22 35 while ( NULL != (dp = readdir( dir )) )
okano 0:3cd1685a4c22 36 this->push_back( dp->d_name );
okano 0:3cd1685a4c22 37
okano 0:3cd1685a4c22 38 closedir( dir );
okano 0:3cd1685a4c22 39
okano 0:3cd1685a4c22 40 std::sort( this->begin(), this->end() );
okano 0:3cd1685a4c22 41 error_flag = NO_ERROR;
okano 0:3cd1685a4c22 42 }
okano 0:3cd1685a4c22 43
okano 0:3cd1685a4c22 44 DirectoryList::~DirectoryList()
okano 0:3cd1685a4c22 45 {
okano 0:3cd1685a4c22 46 }
okano 0:3cd1685a4c22 47
okano 0:3cd1685a4c22 48 int DirectoryList::error_check( void )
okano 0:3cd1685a4c22 49 {
okano 0:3cd1685a4c22 50 return error_flag;
okano 0:3cd1685a4c22 51 }