Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions sqlcommon/file.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Copyright (c) 2016 SQLines
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// File - File operations

#include <stdio.h>
Expand All @@ -26,7 +26,12 @@
#include <direct.h>
#else
#include <sys/stat.h>
#include <sys/io.h>

#ifdef __APPLE__
#include <sys/uio.h>
#else
#include <sys/io.h>
#endif
#include <unistd.h>

#define _read read
Expand Down Expand Up @@ -92,7 +97,7 @@ bool File::IsDirectory(const char *path)
#if defined(WIN32) || defined(WIN64)

struct _finddata_t fileInfo;
intptr_t findHandle = _findfirst(path, &fileInfo);
intptr_t findHandle = _findfirst(path, &fileInfo);

// Check if a file or directory exists with this name
if(findHandle == -1)
Expand Down Expand Up @@ -166,7 +171,7 @@ void File::FindDir(const char *dir_template, std::string &dir)
#if defined(WIN32) || defined(WIN64)

struct _finddata_t fileInfo;
intptr_t findHandle = _findfirst(dir_template, &fileInfo);
intptr_t findHandle = _findfirst(dir_template, &fileInfo);

// Check if a file or directory exists with this template
if(findHandle != -1)
Expand Down Expand Up @@ -258,7 +263,7 @@ int File::GetFileSize(const char* file)
struct _finddata_t fileData;

// define the file size to allocate buffer
int findHandle = _findfirst(file, &fileData);
int findHandle = _findfirst(file, &fileData);

if(findHandle == -1)
{
Expand All @@ -272,7 +277,7 @@ int File::GetFileSize(const char* file)
#else

struct stat info;

if(stat(file, &info) != -1)
{
// Check that the file was found
Expand All @@ -295,9 +300,9 @@ int File::GetContent(const char *file, void *input, size_t len)

// open the file
#if defined(WIN32) || defined(WIN64)
fileHandle = _open(file, _O_RDONLY | _O_BINARY);
fileHandle = _open(file, _O_RDONLY | _O_BINARY);
#else
fileHandle = open(file, O_RDONLY);
fileHandle = open(file, O_RDONLY);
#endif

if(fileHandle == -1)
Expand Down Expand Up @@ -364,13 +369,13 @@ void File::CreateDirectories(const char *path)
{
std::string dir;

// Find the next directory separator
// Find the next directory separator
while(path[i] && path[i] != DIR_SEPARATOR_CHAR)
i++;

bool found = (path[i] == DIR_SEPARATOR_CHAR) ? true : false;
// path[i] points either to separator or end of string (in the last case, we also need to create

// path[i] points either to separator or end of string (in the last case, we also need to create
// last directory
dir.assign(path, i);

Expand All @@ -383,14 +388,14 @@ void File::CreateDirectories(const char *path)
int rc = mkdir(dir.c_str(), S_IROTH | S_IWOTH | S_IXOTH);
#endif

// Error
// Error
if(rc == -1)
{
// Do not log "directory already exists" errors as it is normal
if(errno != EEXIST)
{
printf("\n\nCannot create directory %s - %s", path, strerror(errno));
return;
return;
}
}
}
Expand All @@ -414,9 +419,9 @@ int File::Write(const char *file, const char* content, size_t size)
if(fileh == -1)
return -1;

// write the content to the file
// write the content to the file
int rc = _write(fileh, content, size);

_close(fileh);

return rc;
Expand All @@ -437,7 +442,7 @@ int File::Truncate(const char *file)

if(fileh == -1)
return -1;

return _close(fileh);
}

Expand All @@ -458,6 +463,6 @@ int File::Append(const char *file, const char *data, unsigned int len)

int rc = _write(fileh, data, len);
_close(fileh);

return rc;
}
34 changes: 19 additions & 15 deletions sqlines/file.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Copyright (c) 2016 SQLines
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -26,7 +26,11 @@
#include <direct.h>
#else
#include <sys/stat.h>
#include <sys/io.h>
#ifdef __APPLE__
#include <sys/uio.h>
#else
#include <sys/io.h>
#endif
#include <unistd.h>

#define _read read
Expand All @@ -51,7 +55,7 @@ bool File::IsDirectory(const char *path)
#ifdef WIN32

struct _finddata_t fileInfo;
int findHandle = _findfirst(path, &fileInfo);
int findHandle = _findfirst(path, &fileInfo);

// Check if a file or directory exists with this name
if(findHandle == -1)
Expand Down Expand Up @@ -88,7 +92,7 @@ bool File::IsFile(const char *path, size_t *size)
#ifdef WIN32

struct _finddata_t fileInfo;
int findHandle = _findfirst(path, &fileInfo);
int findHandle = _findfirst(path, &fileInfo);

// check if a file or directory exists with this name
if(findHandle == -1)
Expand Down Expand Up @@ -261,7 +265,7 @@ int File::GetFileSize(const char* file)
struct _finddata_t fileData;

// define the file size to allocate buffer
int findHandle = _findfirst(file, &fileData);
int findHandle = _findfirst(file, &fileData);

if(findHandle == -1)
{
Expand All @@ -275,7 +279,7 @@ int File::GetFileSize(const char* file)
#else

struct stat info;

if(stat(file, &info) != -1)
{
// Check that the file was found
Expand All @@ -298,7 +302,7 @@ int File::GetContent(const char *file, void *input, size_t len)

// open the file
#ifdef WIN32
fileHandle = _open(file, _O_RDONLY | _O_BINARY);
fileHandle = _open(file, _O_RDONLY | _O_BINARY);
#else
fileHandle = open(file, O_RDONLY);
#endif
Expand Down Expand Up @@ -330,7 +334,7 @@ std::string File::GetRelativeName(const char* base, const char *file)
if(base == NULL || file == NULL)
return relative;

int i = 0;
int i = 0;
int sep_pos = 0;

// Skip equal leading directories
Expand Down Expand Up @@ -370,9 +374,9 @@ int File::Write(const char *file, const char* content, size_t size)
if(fileh == -1)
return -1;

// write the content to the file
// write the content to the file
int rc = _write(fileh, content, size);

_close(fileh);

return rc;
Expand Down Expand Up @@ -403,13 +407,13 @@ void File::CreateDirectories(const char *path)
{
std::string dir;

// Find the next directory separator
// Find the next directory separator
while(path[i] && path[i] != DIR_SEPARATOR_CHAR)
i++;

bool found = (path[i] == DIR_SEPARATOR_CHAR) ? true : false;
// path[i] points either to separator or end of string (in the last case, we also need to create

// path[i] points either to separator or end of string (in the last case, we also need to create
// last directory
dir.assign(path, i);

Expand All @@ -422,14 +426,14 @@ void File::CreateDirectories(const char *path)
int rc = mkdir(dir.c_str(), S_IROTH | S_IWOTH | S_IXOTH);
#endif

// Error
// Error
if(rc == -1)
{
// Do not log "directory already exists" errors as it is normal
if(errno != EEXIST)
{
printf("\n\nCannot create directory %s - %s", path, strerror(errno));
return;
return;
}
}
}
Expand Down
22 changes: 13 additions & 9 deletions sqlines/filelist.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Copyright (c) 2016 SQLines
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -26,7 +26,11 @@
#include <direct.h>
#else
#include <sys/stat.h>
#include <sys/io.h>
#ifdef __APPLE__
#include <sys/uio.h>
#else
#include <sys/io.h>
#endif
#include <unistd.h>

#define _read read
Expand Down Expand Up @@ -57,7 +61,7 @@ FileList::FileList()
wildcards are allowed for directories and file names
d:\prj*\a*\*.sql

last wildcard is only for files, use \ at the end to specify last wildcard for directories
last wildcard is only for files, use \ at the end to specify last wildcard for directories

if path is not specified for an item, the path from the previous item is taken if it exists
d:\project\*.sql, *.ddl
Expand Down Expand Up @@ -111,7 +115,7 @@ int FileList::Load(const char *inputPath)
for(std::list<std::string>::iterator i = dirs.begin(); i != dirs.end(); i++)
{
// Find files in the specified directory matching the file wildcard
FindFiles(*i, file, dirs, _files);
FindFiles(*i, file, dirs, _files);
}

}
Expand Down Expand Up @@ -163,7 +167,7 @@ int FileList::FindFiles(std::string dir, std::string file, std::list<std::string

struct _finddata_t fileInfo;

int searchHandle = _findfirst(path.c_str(), &fileInfo);
int searchHandle = _findfirst(path.c_str(), &fileInfo);
if(searchHandle == -1)
return 0;

Expand All @@ -190,7 +194,7 @@ int FileList::FindFiles(std::string dir, std::string file, std::list<std::string
dirs.push_back(foundDir);
}

} while(_findnext(searchHandle, &fileInfo) == 0);
} while(_findnext(searchHandle, &fileInfo) == 0);

_findclose(searchHandle);

Expand All @@ -210,7 +214,7 @@ int FileList::FindFiles(std::string dir, std::string file, std::list<std::string
{
FILE *file = fopen(temp, "r");
char fileName[1024]; *fileName = '\x0';

if(file)
{
// Read file content line by line
Expand All @@ -219,7 +223,7 @@ int FileList::FindFiles(std::string dir, std::string file, std::list<std::string
// Remove new line from path, otherwise stat() will fail
int len = strlen(fileName);
if(len && fileName[len-1] == '\n')
fileName[len-1] = '\x0';
fileName[len-1] = '\x0';

size_t size = 0;
bool file = File::IsFile(fileName, &size);
Expand All @@ -231,7 +235,7 @@ int FileList::FindFiles(std::string dir, std::string file, std::list<std::string
}
}
}

fclose(file);
}

Expand Down