Skip to content

Commit

Permalink
Added new functional for FileSystem library.
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirBalun committed Sep 22, 2018
1 parent 4c00405 commit b879520
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 39 deletions.
45 changes: 34 additions & 11 deletions README.md
@@ -1,7 +1,7 @@
<p align="center">
<img
alt="WINC"
src="https://downloader.disk.yandex.ru/preview/53f91952a196156792dce7a6813a208da3c579194dca7312e29eb0e7182c19af/5b9c2e78/QIu_UHwQI7Su_15ipwApzqJ1eYAnQJo8Hb4pgrYptYSM2WFuiVeUN_LkQVZ9Sy4EH8NDhSF1E2xCxrTUcxChjg%3D%3D?uid=0&filename=WINC.png&disposition=inline&hash=&limit=0&content_type=image%2Fpng&tknv=v2&size=1680x938"
src="https://downloader.disk.yandex.ru/preview/0383daf829045ac6e18f018fb959e47ce446412b4730ff30155678342baddcb7/5ba6b58e/QIu_UHwQI7Su_15ipwApzqJ1eYAnQJo8Hb4pgrYptYSM2WFuiVeUN_LkQVZ9Sy4EH8NDhSF1E2xCxrTUcxChjg%3D%3D?uid=0&filename=WINC.png&disposition=inline&hash=&limit=0&content_type=image%2Fpng&tknv=v2&size=2048x2048"
width="250"
/>
</p>
Expand All @@ -19,21 +19,44 @@ libraries for working with:

## Short examples

One of the collection from the *Utils* library:
One of the example collection from the *Utils* library:

~~~C
list_t list = init_list();
push_back_list(list, "world");
push_back_list(list, "!!!");
push_front_list(list, "Hello ");

FOR_EACH_LIST(iterator, list)
int main(int argc, char** argv)
{
printf("%s", (char*) list_get(iterator));
list_t* list = init_list();
push_back_list(list, "world");
push_back_list(list, "!!!");
push_front_list(list, "Hello ");

FOR_EACH_LIST(iterator, list)
{
printf("%s", (char*) list_get(iterator));
}

destroy_list(list);
return EXIT_SUCCESS;
}

destroy_list(list);
~~~
Example of the file system iterator from the *FileSystem* library:
~~~C
void iteration_callback(const char* element)
{
printf("%s\n", element);
}
int main(int argc, char** argv)
{
char* user_dir = get_user_directory();
printf("Iteration in the user directory:\n");
path_iterate(user_dir, iteration_callback);
free(user_dir);
return EXIT_SUCCESS;
}
~~~

## How to build WINC

Expand Down
3 changes: 2 additions & 1 deletion file_system/examples/CMakeLists.txt
Expand Up @@ -16,8 +16,9 @@

cmake_minimum_required (VERSION 3.8)

set (EXAMPLES FileExample PathExample)
set (EXAMPLES DirectoryExample FileExample PathExample)

add_executable (DirectoryExample directory_example.c ../sources/directory.c ../sources/path.c)
add_executable (FileExample file_example.c ../sources/file.c)
add_executable (PathExample path_example.c ../sources/path.c)

Expand Down
50 changes: 50 additions & 0 deletions file_system/examples/directory_example.c
@@ -0,0 +1,50 @@
/*
* Copyright 2018 Vladimir Balun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdio.h>

#include "winc/file_system/directory.h"
#include "winc/file_system/path.h"

int main()
{
char* user_dir = get_user_directory();
if (!is_exist_dir(user_dir))
{
fprintf(stderr, "User directory is absent.");
free(user_dir);
return SYSTEM_ERROR;
}

const char* new_catalog = "/tmp_directory_for_example";
char test_dir[strlen(user_dir) + strlen(new_catalog) + 1];
strcpy(test_dir, user_dir);
strcat(test_dir, new_catalog);

if (create_dir(test_dir))
printf("Directory: \"%s\" was created.\n", test_dir);
else
fprintf(stderr, "Directory: \"%s\" wasn't created.\n", test_dir);

if (remove_dir(test_dir))
printf("Directory: \"%s\" was deleted.\n", test_dir);
else
fprintf(stderr, "Directory: \"%s\" wasn't deleted.\n", test_dir);

free(user_dir);

return EXIT_SUCCESS;
}
11 changes: 5 additions & 6 deletions file_system/examples/file_example.c
Expand Up @@ -30,30 +30,29 @@ int main()
append_to_file(source_file, data);
if (!copy_file(source_file, copied_file))
{
printf("File wasn't copied.");
fprintf(stderr, "File \"%s\" wasn't copied.\n", source_file);
return EXIT_FAILURE;
}
}
else
{
printf("File wasn't created.");
fprintf(stderr, "File \"%s\" wasn't created.\n", source_file);
return EXIT_FAILURE;
}

char* data_from_copied_file = read_file(copied_file);
printf("Copied data: %s\n", data_from_copied_file);
if (data_from_copied_file)
free(data_from_copied_file);
free(data_from_copied_file);

rename_file(copied_file, renamed_file);
const char* new_data = "New hello world!";
rewrite_file(renamed_file, new_data);
printf("New size of renamed file: %lu\n", get_file_size(renamed_file));

if (!remove_file(source_file))
printf("File wasn't deleted.");
fprintf(stderr, "File \"%s\" wasn't deleted.\n", source_file);
if(!remove_file(renamed_file))
printf("File wasn't deleted.");
fprintf(stderr, "File \"%s\" wasn't deleted.\n", renamed_file);

return EXIT_SUCCESS;
}
11 changes: 10 additions & 1 deletion file_system/examples/path_example.c
Expand Up @@ -18,14 +18,23 @@

#include "winc/file_system/path.h"

void iteration_callback(const char* elem)
{
printf("%s\n", elem);
}

int main()
{
char* user_dir = get_user_directory();
printf("User directory: %s\n", user_dir);
free(user_dir);

char* curr_dir = get_current_directory();
printf("Current working directory: %s\n", curr_dir);

printf("Iteration in the user directory:\n");
path_iterate(user_dir, iteration_callback);

free(user_dir);
free(curr_dir);

return EXIT_SUCCESS;
Expand Down
2 changes: 2 additions & 0 deletions file_system/include/winc/file_system/directory.h
Expand Up @@ -21,6 +21,8 @@
extern "C" {
#endif // __cplusplus

#include <stdio.h>

#include "path.h"
#include "winc/utils/typedef.h"

Expand Down
4 changes: 4 additions & 0 deletions file_system/include/winc/file_system/path.h
Expand Up @@ -22,6 +22,7 @@ extern "C" {
#endif // __cplusplus

#include <stdlib.h>
#include <string.h>

#include "winc/utils/allocation.h"
#include "winc/utils/typedef.h"
Expand All @@ -31,6 +32,9 @@ bool is_file(const char* path);

char* get_user_directory();
char* get_current_directory();
char* get_file_name_from_path(char *path);

void path_iterate(const char* path, void(*func)(const char*));

#ifdef __cplusplus
}
Expand Down
15 changes: 5 additions & 10 deletions file_system/sources/directory.c
Expand Up @@ -16,6 +16,11 @@

#include "winc/file_system/directory.h"

bool rename_dir(const char* old_dir_name, const char* new_dir_name)
{
return rename(old_dir_name, new_dir_name) == 0;
}

#ifdef __unix__

#include <sys/types.h>
Expand Down Expand Up @@ -44,11 +49,6 @@
return stat(dir_name, &st) == 0;
}

bool rename_dir(const char* old_dir_name, const char* new_dir_name)
{
// Soon...
}

#elif defined(_WIN32) || defined(WIN32)

#include <Windows.h>
Expand All @@ -68,9 +68,4 @@
// Soon...
}

bool rename_dir(const char* old_dir_name, const char* new_dir_name)
{
// Soon...
}

#endif // __unix__ and WIN32
10 changes: 5 additions & 5 deletions file_system/sources/file.c
Expand Up @@ -32,7 +32,7 @@ char* read_file(const char* file_name)
return NULL;

long int size = get_file_size(file_name);
char* data = MALLOC(sizeof(char) * (size_t)(size + 1));
char* data = MALLOC(sizeof(char) * (size + 1));
fread(data, 1, (size_t)size, file);
data[size] = '\0';
fclose(file);
Expand Down Expand Up @@ -70,15 +70,15 @@ void append_to_file(const char* file_name, const char* data)

bool create_file(const char* full_file_name)
{
return creat(full_file_name, S_IRUSR | S_IWUSR) != -1;
return creat(full_file_name, S_IRUSR | S_IWUSR) == 0;
}

bool copy_file(const char* from_file_name, const char* to_file_name)
{
int from_file, to_file;
if ((from_file = open(from_file_name, O_RDONLY)) == -1)
if ((from_file = open(from_file_name, O_RDONLY)) != 0)
return false;
if ((to_file = creat(to_file_name, 0660)) == -1)
if ((to_file = creat(to_file_name, 0660)) != 0)
{
close(from_file);
return false;
Expand Down Expand Up @@ -119,7 +119,7 @@ void append_to_file(const char* file_name, const char* data)
long int get_file_size(const char* file_name)
{
struct stat st;
if (stat(file_name, &st))
if (stat(file_name, &st) == 0)
return st.st_size;
else
return -1;
Expand Down
39 changes: 34 additions & 5 deletions file_system/sources/path.c
Expand Up @@ -19,25 +19,24 @@
#ifdef __unix__

#include <unistd.h>
#include <dirent.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>

#include <stdlib.h>
#include <string.h>

bool is_directory(const char* path)
{
struct stat buff;
stat(path, &buff);
return S_ISREG(buff.st_mode);
return S_ISDIR(buff.st_mode);
}

bool is_file(const char* path)
{
struct stat buff;
stat(path, &buff);
return S_ISDIR(buff.st_mode);
return S_ISREG(buff.st_mode);
}

char* get_user_directory()
Expand All @@ -63,6 +62,26 @@
return NULL;
}

char* get_file_name_from_path(char *path)
{
return basename(path);
}

void path_iterate(const char* path, void(*func)(const char*))
{
DIR *dir;
struct dirent *ent;
if ((dir = opendir(path)) != NULL)
{
while ((ent = readdir(dir)) != NULL)
{
if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0)
func(ent->d_name);
}
closedir(dir);
}
}

#elif defined(_WIN32) || defined(WIN32)

#include <Windows.h>
Expand All @@ -87,4 +106,14 @@
// Soon...
}

char* get_file_name_from_path(const char* path)
{
// Soon...
}

void path_iterate(const char* path, void(*func)(const char*))
{
// Soon...
}

#endif // __unix__ and WIN32

0 comments on commit b879520

Please sign in to comment.