-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
261 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#include "fileio.h" | ||
|
||
#include <unistd.h> | ||
#include <sys/stat.h> | ||
|
||
#include "log.h" | ||
#include "head.h" | ||
|
||
|
||
bool exists(char *path) { | ||
struct stat st; | ||
int rc = stat(path, &st); | ||
return rc == 0; | ||
} | ||
|
||
FILE *openFile(char *fileName, char *modes) { | ||
return fopen(fileName, modes); | ||
} | ||
|
||
int closeFile(FILE *file) { | ||
return fclose(file); | ||
} | ||
|
||
void createDirectory(char *dirname) { | ||
struct stat st; | ||
|
||
if (exists(dirname)) { | ||
stat(dirname, &st); | ||
if (S_ISDIR(st.st_mode)) | ||
return; | ||
removeFile(dirname); | ||
} | ||
#ifdef __WIN32__ | ||
mkdir(dirname); | ||
#else | ||
mkdir(dirname, 0777); | ||
#endif | ||
} | ||
|
||
|
||
void recursivelyCreateFileDirIfNotExists(char *fpath) { | ||
char *p; | ||
int ch, len; | ||
bool loopFlag = true; | ||
|
||
/* Check each level from the deepest, stop when it exists */ | ||
len = strlen(fpath); | ||
for (p=fpath+len; p>fpath && loopFlag; p--) { | ||
if (*p!=FILE_PATH_SEPARATOR) | ||
continue; | ||
ch = *p; *p = 0; /* Truncate here, remember the char */ | ||
if (directoryExists(fpath)) { | ||
loopFlag=false; | ||
} | ||
*p = ch; /* Restore the char */ | ||
} | ||
/* Create each of the remaining levels */ | ||
for (p+=2; *p; p++) { | ||
if (*p!=FILE_PATH_SEPARATOR) | ||
continue; | ||
ch = *p; *p = 0; | ||
createDirectory(fpath); | ||
*p = ch; | ||
} | ||
} | ||
|
||
|
||
void removeFile(char *fileName) { | ||
log_trace("removing file '%s'", fileName); | ||
unlink(fileName); | ||
} | ||
|
||
int fileStatus(char *path, struct stat *statP) { | ||
struct stat st; | ||
int return_value; | ||
|
||
return_value = stat(path, &st); /* Returns 0 on success */ | ||
if (statP != NULL) | ||
*statP = st; | ||
return return_value; | ||
} | ||
|
||
time_t fileModificationTime(char *path) { | ||
struct stat st; | ||
if (fileStatus(path, &st) !=0) | ||
return 0; /* File not found? */ | ||
return st.st_mtime; | ||
} | ||
|
||
size_t fileSize(char *path) { | ||
struct stat st; | ||
if (fileStatus(path, &st) !=0) | ||
return 0; /* File not found? */ | ||
return st.st_size; | ||
} | ||
|
||
bool isDirectory(char *path) { | ||
return directoryExists(path); | ||
} | ||
|
||
bool directoryExists(char *path) { | ||
struct stat st; | ||
int statResult; | ||
|
||
statResult = stat(path, &st); | ||
return statResult==0 && S_ISDIR(st.st_mode); | ||
} | ||
|
||
bool fileExists(char *fullPath) { | ||
struct stat st; | ||
int statResult; | ||
|
||
statResult = stat(fullPath, &st); | ||
return statResult==0 && S_ISREG(st.st_mode); | ||
} | ||
|
||
size_t readFile(FILE *file, void *buffer, size_t size, size_t count) { | ||
return fread(buffer, size, count, file); | ||
} | ||
|
||
size_t writeFile(FILE *file, void *buffer, size_t size, size_t count) { | ||
return fwrite(buffer, size, count, file); | ||
} | ||
|
||
int readChar(FILE *file) { | ||
return getc(file); | ||
} | ||
|
||
char *getEnv(const char *variable) { | ||
return getenv(variable); | ||
} | ||
|
||
char *getCwd(char *buffer, size_t size) { | ||
return getcwd(buffer, size); | ||
} | ||
|
||
const char **getFilesInCurrentDirectory(void) { | ||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef FILEIO_H_INCLUDED | ||
#define FILEIO_H_INCLUDED | ||
|
||
#include <stdio.h> | ||
#include <stdbool.h> | ||
#include <sys/stat.h> | ||
|
||
|
||
extern bool exists(char *path); | ||
extern int fileStatus(char *path, struct stat *statbuf); | ||
extern time_t fileModificationTime(char *path); | ||
extern size_t fileSize(char *path); | ||
extern FILE *openFile(char *fileName, char *modes); | ||
extern int closeFile(FILE *file); | ||
extern void createDirectory(char *dirname); | ||
extern void recursivelyCreateFileDirIfNotExists(char *fpath); | ||
extern void removeFile(char *dirname); | ||
extern bool isDirectory(char *fullPath); | ||
extern bool directoryExists(char *fullPath); | ||
extern bool fileExists(char *fullPath); | ||
extern size_t readFile(FILE *file, void *buffer, size_t size, size_t count); | ||
extern size_t writeFile(FILE *file, void *buffer, size_t size, size_t count); | ||
extern int readChar(FILE *file); | ||
extern const char **getFilesInCurrentDirectory(void); | ||
|
||
extern char *getEnv(const char *variable); | ||
extern char *getCwd(char *buf, size_t size); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* -*- c -*-*/ | ||
#include <cgreen/mocks.h> | ||
#include "fileio.h" | ||
|
||
|
||
const char **getFilesInCurrentDirectory(void) { | ||
return (const char **)mock(); | ||
} | ||
|
||
bool isDirectory(char *fullPath) { | ||
return (bool)mock(fullPath); | ||
} | ||
|
||
bool directoryExists(char *fullPath) { | ||
return (bool)mock(fullPath); | ||
} | ||
|
||
bool fileExists(char *fullPath) { | ||
return (bool)mock(fullPath); | ||
} | ||
|
||
int fileStatus(char *path, struct stat *statbuf) { | ||
return (int)mock(path, statbuf); | ||
} | ||
|
||
time_t fileModificationTime(char *path) { | ||
return (time_t)mock(path); | ||
} | ||
|
||
size_t fileSize(char *path) { | ||
return (size_t)mock(path); | ||
} | ||
|
||
FILE *openFile(char *fileName, char *modes) { | ||
return (FILE *)mock(fileName, modes); | ||
} | ||
|
||
int closeFile(FILE *file) { | ||
return (int)mock(file); | ||
} | ||
|
||
void createDirectory(char *dirName) { | ||
mock(dirName); | ||
} | ||
|
||
void recursivelyCreateFileDirIfNotExists(char *fpath) { | ||
mock(fpath); | ||
} | ||
|
||
void removeFile(char *fileName) { | ||
mock(fileName); | ||
} | ||
|
||
size_t readFile(FILE *file, void *buffer, size_t size, size_t count) { | ||
return (size_t)mock(buffer, size, count, file); | ||
} | ||
|
||
size_t writeFile(FILE *file, void *buffer, size_t size, size_t count) { | ||
return (size_t)mock(buffer, size, count, file); | ||
} | ||
|
||
int readChar(FILE *file) { | ||
return (int)mock(file); | ||
} | ||
|
||
char *getEnv(const char *variable) { | ||
return (char *)mock(variable); | ||
} | ||
|
||
char *getCwd(char *buffer, size_t size) { | ||
return (char *)mock(buffer, size); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
#include "filemanager.h" | ||
|
||
#include <stdlib.h> | ||
|
||
#include "common.h" | ||
#include "fileio.h" | ||
|
||
|
||
FileTable getFilesFromCompilationDatabase(void) { | ||
const char **fileNameTable = getFilesInCurrentDirectory(); | ||
UNUSED fileNameTable; | ||
|
||
FileTable fileTable = (FileTable)malloc(sizeof(FileTableElement)); | ||
UNUSED fileTable; | ||
|
||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters