diff options
| author | Bernard Xiong <[email protected]> | 2013-01-08 05:05:02 -0800 |
|---|---|---|
| committer | Bernard Xiong <[email protected]> | 2013-01-08 05:05:02 -0800 |
| commit | 72782e920325808bbc58fc2344fc52033479b627 (patch) | |
| tree | 45badbfa9818b8d1722463949beba0f88d1cbd9f /examples/file | |
| parent | b3cf278502e605fa03861a2da5685b4c8d72879e (diff) | |
convert end of line
Diffstat (limited to 'examples/file')
| -rw-r--r-- | examples/file/listdir.c | 112 | ||||
| -rw-r--r-- | examples/file/readspeed.c | 128 | ||||
| -rw-r--r-- | examples/file/readwrite.c | 246 | ||||
| -rw-r--r-- | examples/file/seekdir.c | 100 | ||||
| -rw-r--r-- | examples/file/writespeed.c | 144 |
5 files changed, 365 insertions, 365 deletions
diff --git a/examples/file/listdir.c b/examples/file/listdir.c index 991351fc6c..cf91bc77d4 100644 --- a/examples/file/listdir.c +++ b/examples/file/listdir.c @@ -1,56 +1,56 @@ -/*
- * File : listdir.c
- * This file is part of RT-TestCase in RT-Thread RTOS
- * COPYRIGHT (C) 2010, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2010-02-10 Bernard first version
- */
-#include <rtthread.h>
-#include <dfs_posix.h>
-
-static char fullpath[256];
-void list_dir(const char* path)
-{
- DIR *dir;
-
- dir = opendir(path);
- if (dir != RT_NULL)
- {
- struct dirent* dirent;
- struct stat s;
-
- do
- {
- dirent = readdir(dir);
- if (dirent == RT_NULL) break;
- rt_memset(&s, 0, sizeof(struct stat));
-
- /* build full path for each file */
- rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
-
- stat(fullpath, &s);
- if ( s.st_mode & DFS_S_IFDIR )
- {
- rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
- }
- else
- {
- rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size);
- }
- } while (dirent != RT_NULL);
-
- closedir(dir);
- }
- else rt_kprintf("open %s directory failed\n", path);
-}
-
-#ifdef RT_USING_FINSH
-#include <finsh.h>
-FINSH_FUNCTION_EXPORT(list_dir, list directory);
-#endif
+/* + * File : listdir.c + * This file is part of RT-TestCase in RT-Thread RTOS + * COPYRIGHT (C) 2010, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2010-02-10 Bernard first version + */ +#include <rtthread.h> +#include <dfs_posix.h> + +static char fullpath[256]; +void list_dir(const char* path) +{ + DIR *dir; + + dir = opendir(path); + if (dir != RT_NULL) + { + struct dirent* dirent; + struct stat s; + + do + { + dirent = readdir(dir); + if (dirent == RT_NULL) break; + rt_memset(&s, 0, sizeof(struct stat)); + + /* build full path for each file */ + rt_sprintf(fullpath, "%s/%s", path, dirent->d_name); + + stat(fullpath, &s); + if ( s.st_mode & DFS_S_IFDIR ) + { + rt_kprintf("%s\t\t<DIR>\n", dirent->d_name); + } + else + { + rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size); + } + } while (dirent != RT_NULL); + + closedir(dir); + } + else rt_kprintf("open %s directory failed\n", path); +} + +#ifdef RT_USING_FINSH +#include <finsh.h> +FINSH_FUNCTION_EXPORT(list_dir, list directory); +#endif diff --git a/examples/file/readspeed.c b/examples/file/readspeed.c index d83fdcb20f..52f15b627b 100644 --- a/examples/file/readspeed.c +++ b/examples/file/readspeed.c @@ -1,64 +1,64 @@ -/*
- * File : readspeed.c
- * This file is part of RT-TestCase in RT-Thread RTOS
- * COPYRIGHT (C) 2010, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2010-02-10 Bernard first version
- */
-
-#include <rtthread.h>
-#include <dfs_posix.h>
-
-void readspeed(const char* filename, int block_size)
-{
- int fd;
- char *buff_ptr;
- rt_size_t total_length;
- rt_tick_t tick;
-
- fd = open(filename, 0, O_RDONLY);
- if (fd < 0)
- {
- rt_kprintf("open file:%s failed\n", filename);
- return;
- }
-
- buff_ptr = rt_malloc(block_size);
- if (buff_ptr == RT_NULL)
- {
- rt_kprintf("no memory\n");
- close(fd);
-
- return;
- }
-
- tick = rt_tick_get();
- total_length = 0;
- while (1)
- {
- int length;
- length = read(fd, buff_ptr, block_size);
-
- if (length <= 0) break;
- total_length += length;
- }
- tick = rt_tick_get() - tick;
-
- /* close file and release memory */
- close(fd);
- rt_free(buff_ptr);
-
- /* calculate read speed */
- rt_kprintf("File read speed: %d byte/s\n", total_length /tick * RT_TICK_PER_SECOND);
-}
-
-#ifdef RT_USING_FINSH
-#include <finsh.h>
-FINSH_FUNCTION_EXPORT(readspeed, perform file read test);
-#endif
+/* + * File : readspeed.c + * This file is part of RT-TestCase in RT-Thread RTOS + * COPYRIGHT (C) 2010, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2010-02-10 Bernard first version + */ + +#include <rtthread.h> +#include <dfs_posix.h> + +void readspeed(const char* filename, int block_size) +{ + int fd; + char *buff_ptr; + rt_size_t total_length; + rt_tick_t tick; + + fd = open(filename, 0, O_RDONLY); + if (fd < 0) + { + rt_kprintf("open file:%s failed\n", filename); + return; + } + + buff_ptr = rt_malloc(block_size); + if (buff_ptr == RT_NULL) + { + rt_kprintf("no memory\n"); + close(fd); + + return; + } + + tick = rt_tick_get(); + total_length = 0; + while (1) + { + int length; + length = read(fd, buff_ptr, block_size); + + if (length <= 0) break; + total_length += length; + } + tick = rt_tick_get() - tick; + + /* close file and release memory */ + close(fd); + rt_free(buff_ptr); + + /* calculate read speed */ + rt_kprintf("File read speed: %d byte/s\n", total_length /tick * RT_TICK_PER_SECOND); +} + +#ifdef RT_USING_FINSH +#include <finsh.h> +FINSH_FUNCTION_EXPORT(readspeed, perform file read test); +#endif diff --git a/examples/file/readwrite.c b/examples/file/readwrite.c index 17d173c353..6f25249c61 100644 --- a/examples/file/readwrite.c +++ b/examples/file/readwrite.c @@ -1,123 +1,123 @@ -/*
- * 代码清单:文件读写例子
- *
- * 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作。
- */
-
-#include <rtthread.h>
-#include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */
-
-#define TEST_FN "/test.dat"
-
-/* 测试用的数据和缓冲 */
-static char test_data[120], buffer[120];
-
-/* 文件读写测试 */
-void readwrite(const char* filename)
-{
- int fd;
- int index, length;
-
- /* 只写 & 创建 打开 */
- fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
- if (fd < 0)
- {
- rt_kprintf("open file for write failed\n");
- return;
- }
-
- /* 准备写入数据 */
- for (index = 0; index < sizeof(test_data); index ++)
- {
- test_data[index] = index + 27;
- }
-
- /* 写入数据 */
- length = write(fd, test_data, sizeof(test_data));
- if (length != sizeof(test_data))
- {
- rt_kprintf("write data failed\n");
- close(fd);
- return;
- }
-
- /* 关闭文件 */
- close(fd);
-
- /* 只写并在末尾添加打开 */
- fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0);
- if (fd < 0)
- {
- rt_kprintf("open file for append write failed\n");
- return;
- }
-
- length = write(fd, test_data, sizeof(test_data));
- if (length != sizeof(test_data))
- {
- rt_kprintf("append write data failed\n");
- close(fd);
- return;
- }
- /* 关闭文件 */
- close(fd);
-
- /* 只读打开进行数据校验 */
- fd = open(TEST_FN, O_RDONLY, 0);
- if (fd < 0)
- {
- rt_kprintf("check: open file for read failed\n");
- return;
- }
-
- /* 读取数据(应该为第一次写入的数据) */
- length = read(fd, buffer, sizeof(buffer));
- if (length != sizeof(buffer))
- {
- rt_kprintf("check: read file failed\n");
- close(fd);
- return;
- }
-
- /* 检查数据是否正确 */
- for (index = 0; index < sizeof(test_data); index ++)
- {
- if (test_data[index] != buffer[index])
- {
- rt_kprintf("check: check data failed at %d\n", index);
- close(fd);
- return;
- }
- }
-
- /* 读取数据(应该为第二次写入的数据) */
- length = read(fd, buffer, sizeof(buffer));
- if (length != sizeof(buffer))
- {
- rt_kprintf("check: read file failed\n");
- close(fd);
- return;
- }
-
- /* 检查数据是否正确 */
- for (index = 0; index < sizeof(test_data); index ++)
- {
- if (test_data[index] != buffer[index])
- {
- rt_kprintf("check: check data failed at %d\n", index);
- close(fd);
- return;
- }
- }
-
- /* 检查数据完毕,关闭文件 */
- close(fd);
- /* 打印结果 */
- rt_kprintf("read/write done.\n");
-}
-
-#ifdef RT_USING_FINSH
-#include <finsh.h>
-/* 输出函数到finsh shell命令行中 */
-FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
-#endif
+/* + * 代码清单:文件读写例子 + * + * 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作。 + */ + +#include <rtthread.h> +#include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */ + +#define TEST_FN "/test.dat" + +/* 测试用的数据和缓冲 */ +static char test_data[120], buffer[120]; + +/* 文件读写测试 */ +void readwrite(const char* filename) +{ + int fd; + int index, length; + + /* 只写 & 创建 打开 */ + fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0); + if (fd < 0) + { + rt_kprintf("open file for write failed\n"); + return; + } + + /* 准备写入数据 */ + for (index = 0; index < sizeof(test_data); index ++) + { + test_data[index] = index + 27; + } + + /* 写入数据 */ + length = write(fd, test_data, sizeof(test_data)); + if (length != sizeof(test_data)) + { + rt_kprintf("write data failed\n"); + close(fd); + return; + } + + /* 关闭文件 */ + close(fd); + + /* 只写并在末尾添加打开 */ + fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0); + if (fd < 0) + { + rt_kprintf("open file for append write failed\n"); + return; + } + + length = write(fd, test_data, sizeof(test_data)); + if (length != sizeof(test_data)) + { + rt_kprintf("append write data failed\n"); + close(fd); + return; + } + /* 关闭文件 */ + close(fd); + + /* 只读打开进行数据校验 */ + fd = open(TEST_FN, O_RDONLY, 0); + if (fd < 0) + { + rt_kprintf("check: open file for read failed\n"); + return; + } + + /* 读取数据(应该为第一次写入的数据) */ + length = read(fd, buffer, sizeof(buffer)); + if (length != sizeof(buffer)) + { + rt_kprintf("check: read file failed\n"); + close(fd); + return; + } + + /* 检查数据是否正确 */ + for (index = 0; index < sizeof(test_data); index ++) + { + if (test_data[index] != buffer[index]) + { + rt_kprintf("check: check data failed at %d\n", index); + close(fd); + return; + } + } + + /* 读取数据(应该为第二次写入的数据) */ + length = read(fd, buffer, sizeof(buffer)); + if (length != sizeof(buffer)) + { + rt_kprintf("check: read file failed\n"); + close(fd); + return; + } + + /* 检查数据是否正确 */ + for (index = 0; index < sizeof(test_data); index ++) + { + if (test_data[index] != buffer[index]) + { + rt_kprintf("check: check data failed at %d\n", index); + close(fd); + return; + } + } + + /* 检查数据完毕,关闭文件 */ + close(fd); + /* 打印结果 */ + rt_kprintf("read/write done.\n"); +} + +#ifdef RT_USING_FINSH +#include <finsh.h> +/* 输出函数到finsh shell命令行中 */ +FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test); +#endif diff --git a/examples/file/seekdir.c b/examples/file/seekdir.c index 831023f8eb..1cf8b5fd78 100644 --- a/examples/file/seekdir.c +++ b/examples/file/seekdir.c @@ -1,50 +1,50 @@ -/*
- * File : seekdir.c
- * This file is part of RT-TestCase in RT-Thread RTOS
- * COPYRIGHT (C) 2011, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2011-06-02 Bernard first version
- */
-#include <dfs_posix.h>
-
-void seekdir_test(void)
-{
- DIR * dirp;
- long save3 = 0;
- long cur;
- int i = 0;
- struct dirent *dp;
-
- dirp = opendir ("/");
- save3 = telldir(dirp);
- for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
- {
- rt_kprintf("direntry: %s\n", dp->d_name);
-
- /* ���������Ŀ¼���Ŀ¼ָ�� */
- if (i++ == 3)
- {
- save3 = telldir(dirp);
- }
- }
-
- /* �ص��ղű���ĵ�����Ŀ¼���Ŀ¼ָ�� */
- seekdir (dirp, save3);
- rt_kprintf("seek dientry to: %d\n", save3);
- for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
- {
- rt_kprintf("direntry: %s\n", dp->d_name);
- }
-
- /* �ر�Ŀ¼ */
- closedir (dirp);
-}
-
-#include <finsh.h>
-FINSH_FUNCTION_EXPORT(seekdir_test, perform directory seek test);
+/* + * File : seekdir.c + * This file is part of RT-TestCase in RT-Thread RTOS + * COPYRIGHT (C) 2011, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2011-06-02 Bernard first version + */ +#include <dfs_posix.h> + +void seekdir_test(void) +{ + DIR * dirp; + long save3 = 0; + long cur; + int i = 0; + struct dirent *dp; + + dirp = opendir ("/"); + save3 = telldir(dirp); + for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp)) + { + rt_kprintf("direntry: %s\n", dp->d_name); + + /* ���������Ŀ¼���Ŀ¼ָ�� */ + if (i++ == 3) + { + save3 = telldir(dirp); + } + } + + /* �ص��ղű���ĵ�����Ŀ¼���Ŀ¼ָ�� */ + seekdir (dirp, save3); + rt_kprintf("seek dientry to: %d\n", save3); + for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp)) + { + rt_kprintf("direntry: %s\n", dp->d_name); + } + + /* �ر�Ŀ¼ */ + closedir (dirp); +} + +#include <finsh.h> +FINSH_FUNCTION_EXPORT(seekdir_test, perform directory seek test); diff --git a/examples/file/writespeed.c b/examples/file/writespeed.c index 5fa2f44452..ab6946888f 100644 --- a/examples/file/writespeed.c +++ b/examples/file/writespeed.c @@ -1,72 +1,72 @@ -/*
- * File : writespeed.c
- * This file is part of RT-TestCase in RT-Thread RTOS
- * COPYRIGHT (C) 2010, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2010-02-10 Bernard first version
- */
-#include <rtthread.h>
-#include <dfs_posix.h>
-
-void writespeed(const char* filename, int total_length, int block_size)
-{
- int fd, index, length;
- char *buff_ptr;
- rt_tick_t tick;
-
- fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
- if (fd < 0)
- {
- rt_kprintf("open file:%s failed\n", filename);
- return;
- }
-
- buff_ptr = rt_malloc(block_size);
- if (buff_ptr == RT_NULL)
- {
- rt_kprintf("no memory\n");
- close(fd);
-
- return;
- }
-
- /* prepare write data */
- for (index = 0; index < block_size; index++)
- {
- buff_ptr[index] = index;
- }
- index = 0;
-
- /* get the beginning tick */
- tick = rt_tick_get();
- while (index < total_length / block_size)
- {
- length = write(fd, buff_ptr, block_size);
- if (length != block_size)
- {
- rt_kprintf("write failed\n");
- break;
- }
-
- index ++;
- }
- tick = rt_tick_get() - tick;
-
- /* close file and release memory */
- close(fd);
- rt_free(buff_ptr);
-
- /* calculate write speed */
- rt_kprintf("File write speed: %d byte/s\n", total_length / tick * RT_TICK_PER_SECOND);
-}
-
-#ifdef RT_USING_FINSH
-#include <finsh.h>
-FINSH_FUNCTION_EXPORT(writespeed, perform file write test);
-#endif
+/* + * File : writespeed.c + * This file is part of RT-TestCase in RT-Thread RTOS + * COPYRIGHT (C) 2010, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2010-02-10 Bernard first version + */ +#include <rtthread.h> +#include <dfs_posix.h> + +void writespeed(const char* filename, int total_length, int block_size) +{ + int fd, index, length; + char *buff_ptr; + rt_tick_t tick; + + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0); + if (fd < 0) + { + rt_kprintf("open file:%s failed\n", filename); + return; + } + + buff_ptr = rt_malloc(block_size); + if (buff_ptr == RT_NULL) + { + rt_kprintf("no memory\n"); + close(fd); + + return; + } + + /* prepare write data */ + for (index = 0; index < block_size; index++) + { + buff_ptr[index] = index; + } + index = 0; + + /* get the beginning tick */ + tick = rt_tick_get(); + while (index < total_length / block_size) + { + length = write(fd, buff_ptr, block_size); + if (length != block_size) + { + rt_kprintf("write failed\n"); + break; + } + + index ++; + } + tick = rt_tick_get() - tick; + + /* close file and release memory */ + close(fd); + rt_free(buff_ptr); + + /* calculate write speed */ + rt_kprintf("File write speed: %d byte/s\n", total_length / tick * RT_TICK_PER_SECOND); +} + +#ifdef RT_USING_FINSH +#include <finsh.h> +FINSH_FUNCTION_EXPORT(writespeed, perform file write test); +#endif |
