- fixed mingw compilation problem of file-tool

v1.6_develop_471
Michael Zillgith 10 months ago
parent 500a108962
commit 1fd2ad9724

@ -12,10 +12,10 @@
#include "iec61850_client.h" #include "iec61850_client.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32 #ifdef _WIN32
#include <Windows.h> #include <windows.h>
#else #else
#include <libgen.h> #include <libgen.h>
#endif #endif
@ -31,14 +31,16 @@ dirname(char* path)
int len = strlen(path); int len = strlen(path);
int i = 0; int i = 0;
while (i < len) { while (i < len)
{
if (path[i] == '/' || path[i] == ':' || path[i] == '\\') if (path[i] == '/' || path[i] == ':' || path[i] == '\\')
lastSep = path + i; lastSep = path + i;
i++; i++;
} }
if (lastSep) { if (lastSep)
{
strcpy(_dirname, path); strcpy(_dirname, path);
_dirname[lastSep - path] = 0; _dirname[lastSep - path] = 0;
} }
@ -48,7 +50,6 @@ dirname(char* path)
return _dirname; return _dirname;
} }
static char _basename[1000]; static char _basename[1000];
static char* static char*
@ -59,7 +60,8 @@ basename(char* path)
int len = strlen(path); int len = strlen(path);
int i = 0; int i = 0;
while (i < len) { while (i < len)
{
if (path[i] == '/' || path[i] == ':' || path[i] == '\\') if (path[i] == '/' || path[i] == ':' || path[i] == '\\')
lastSep = path + i; lastSep = path + i;
@ -81,7 +83,8 @@ static int tcpPort = 102;
static char* filename = NULL; static char* filename = NULL;
static bool singleRequest = false; static bool singleRequest = false;
typedef enum { typedef enum
{
FileOperationType_None = 0, FileOperationType_None = 0,
FileOperationType_Dir, FileOperationType_Dir,
FileOperationType_Info, FileOperationType_Info,
@ -92,17 +95,17 @@ typedef enum {
static FileOperationType operation = FileOperationType_None; static FileOperationType operation = FileOperationType_None;
static bool static bool
downloadHandler(void* parameter, uint8_t* buffer, uint32_t bytesRead) downloadHandler(void* parameter, uint8_t* buffer, uint32_t bytesRead)
{ {
FILE* fp = (FILE*) parameter; FILE* fp = (FILE*)parameter;
printf("received %i bytes\n", bytesRead); printf("received %i bytes\n", bytesRead);
if (bytesRead > 0) { if (bytesRead > 0)
if (fwrite(buffer, bytesRead, 1, fp) != 1) { {
if (fwrite(buffer, bytesRead, 1, fp) != 1)
{
printf("Failed to write local file!\n"); printf("Failed to write local file!\n");
return false; return false;
} }
@ -128,7 +131,6 @@ printHelp()
printf(" set <filename> - set file\n"); printf(" set <filename> - set file\n");
} }
static int static int
parseOptions(int argc, char** argv) parseOptions(int argc, char** argv)
{ {
@ -136,40 +138,51 @@ parseOptions(int argc, char** argv)
int retVal = 0; int retVal = 0;
while (currentArgc < argc) { while (currentArgc < argc)
if (strcmp(argv[currentArgc], "-h") == 0) { {
if (strcmp(argv[currentArgc], "-h") == 0)
{
hostname = argv[++currentArgc]; hostname = argv[++currentArgc];
} }
else if (strcmp(argv[currentArgc], "-p") == 0) { else if (strcmp(argv[currentArgc], "-p") == 0)
{
tcpPort = atoi(argv[++currentArgc]); tcpPort = atoi(argv[++currentArgc]);
} }
else if (strcmp(argv[currentArgc], "-s") == 0) { else if (strcmp(argv[currentArgc], "-s") == 0)
{
singleRequest = true; singleRequest = true;
} }
else if (strcmp(argv[currentArgc], "del") == 0) { else if (strcmp(argv[currentArgc], "del") == 0)
{
operation = FileOperationType_Del; operation = FileOperationType_Del;
filename = argv[++currentArgc]; filename = argv[++currentArgc];
} }
else if (strcmp(argv[currentArgc], "dir") == 0) { else if (strcmp(argv[currentArgc], "dir") == 0)
{
operation = FileOperationType_Dir; operation = FileOperationType_Dir;
} }
else if (strcmp(argv[currentArgc], "subdir") == 0) { else if (strcmp(argv[currentArgc], "subdir") == 0)
{
operation = FileOperationType_Dir; operation = FileOperationType_Dir;
filename = argv[++currentArgc]; filename = argv[++currentArgc];
} }
else if (strcmp(argv[currentArgc], "info") == 0) { else if (strcmp(argv[currentArgc], "info") == 0)
{
operation = FileOperationType_Info; operation = FileOperationType_Info;
filename = argv[++currentArgc]; filename = argv[++currentArgc];
} }
else if (strcmp(argv[currentArgc], "get") == 0) { else if (strcmp(argv[currentArgc], "get") == 0)
{
operation = FileOperationType_Get; operation = FileOperationType_Get;
filename = argv[++currentArgc]; filename = argv[++currentArgc];
} }
else if (strcmp(argv[currentArgc], "set") == 0) { else if (strcmp(argv[currentArgc], "set") == 0)
{
operation = FileOperationType_Set; operation = FileOperationType_Set;
filename = argv[++currentArgc]; filename = argv[++currentArgc];
} }
else { else
{
printf("Unknown operation!\n"); printf("Unknown operation!\n");
return 1; return 1;
} }
@ -195,22 +208,25 @@ showDirectory(IedConnection con)
else else
rootDirectory = IedConnection_getFileDirectory(con, &error, filename); rootDirectory = IedConnection_getFileDirectory(con, &error, filename);
if (error != IED_ERROR_OK) { if (error != IED_ERROR_OK)
{
printf("Error retrieving file directory\n"); printf("Error retrieving file directory\n");
} }
else { else
{
LinkedList directoryEntry = LinkedList_getNext(rootDirectory); LinkedList directoryEntry = LinkedList_getNext(rootDirectory);
while (directoryEntry != NULL) { while (directoryEntry != NULL)
{
FileDirectoryEntry entry = (FileDirectoryEntry) directoryEntry->data; FileDirectoryEntry entry = (FileDirectoryEntry)directoryEntry->data;
printf("%s %i\n", FileDirectoryEntry_getFileName(entry), FileDirectoryEntry_getFileSize(entry)); printf("%s %i\n", FileDirectoryEntry_getFileName(entry), FileDirectoryEntry_getFileSize(entry));
directoryEntry = LinkedList_getNext(directoryEntry); directoryEntry = LinkedList_getNext(directoryEntry);
} }
LinkedList_destroyDeep(rootDirectory, (LinkedListValueDeleteFunction) FileDirectoryEntry_destroy); LinkedList_destroyDeep(rootDirectory, (LinkedListValueDeleteFunction)FileDirectoryEntry_destroy);
} }
if (moreFollows) if (moreFollows)
@ -228,10 +244,11 @@ getFile(IedConnection con)
FILE* fp = fopen(localFilename, "wb"); FILE* fp = fopen(localFilename, "wb");
if (fp != NULL) { if (fp != NULL)
{
/* Download a file from the server */ /* Download a file from the server */
IedConnection_getFile(con, &error, filename, downloadHandler, (void*) fp); IedConnection_getFile(con, &error, filename, downloadHandler, (void*)fp);
if (error != IED_ERROR_OK) if (error != IED_ERROR_OK)
printf("Failed to get file!\n"); printf("Failed to get file!\n");
@ -290,14 +307,16 @@ deleteFile(IedConnection con)
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
if (argc < 2) { if (argc < 2)
{
printHelp(); printHelp();
return 0; return 0;
} }
parseOptions(argc, argv); parseOptions(argc, argv);
if (operation == FileOperationType_None) { if (operation == FileOperationType_None)
{
printHelp(); printHelp();
return 0; return 0;
} }
@ -308,10 +327,10 @@ main(int argc, char** argv)
IedConnection_connect(con, &error, hostname, tcpPort); IedConnection_connect(con, &error, hostname, tcpPort);
if (error == IED_ERROR_OK) { if (error == IED_ERROR_OK)
{
switch (operation)
switch (operation) { {
case FileOperationType_Dir: case FileOperationType_Dir:
showDirectory(con); showDirectory(con);
break; break;
@ -330,15 +349,13 @@ main(int argc, char** argv)
break; break;
} }
IedConnection_abort(con, &error); IedConnection_abort(con, &error);
} }
else { else
{
printf("Failed to connect to %s:%i\n", hostname, tcpPort); printf("Failed to connect to %s:%i\n", hostname, tcpPort);
} }
IedConnection_destroy(con); IedConnection_destroy(con);
return 0; return 0;
} }

Loading…
Cancel
Save