Browse Source

Implement saveConnection

It actually works
master
Dusk 3 months ago
parent
commit
ae82aebdeb
  1. 4
      .gitignore
  2. 5
      src/logic/login.c
  3. 2
      src/logic/login.h
  4. 30
      src/persistence/connection.c
  5. 19
      src/persistence/connection.h
  6. 3
      src/persistence/save.c
  7. 8
      src/persistence/save.h

4
.gitignore

@ -86,3 +86,7 @@ flycheck_*.el
kenocli
build/
#.conn
*.conn

5
src/logic/login.c

@ -18,6 +18,11 @@ kma_conn *initMenu()
switch(selection) {
case 'n':
conn = createConnection();
puts("Input connection name:");
char filename[32];
scanf("%s", filename);
puts("Saving to disk... ");
saveConnection(conn, filename);
break;
case 'q':
exit(EXIT_SUCCESS);

2
src/logic/login.h

@ -4,7 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "../persistence/save.h"
#include "../persistence/connection.h"
#include "kma_auth.h"

30
src/persistence/connection.c

@ -0,0 +1,30 @@
#include "connection.h"
void saveConnection(kma_conn *conn, char *filename) {
FILE *saved_conn;
// open file for writing
char *ext = ".conn";
char *actualfilename = malloc(sizeof(char) * (strlen(filename) + strlen(ext) + 1));
strcpy(actualfilename, filename);
strcat(actualfilename, ext);
saved_conn = fopen(actualfilename, "w");
if (!saved_conn) {
fprintf(stderr, "\nError opening file\n");
exit (1);
}
// write struct to file
int test = fwrite(conn, sizeof(kma_conn), 1, saved_conn);
if(test != 0)
printf("Contents to file written successfully !\n");
else
printf("Error writing file !\n");
// close file
fclose(saved_conn);
free(actualfilename);
}

19
src/persistence/connection.h

@ -0,0 +1,19 @@
// Saving to disk functions
#ifndef PERS_CONNECTION_H_
#define PERS_CONNECTION_H_
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "kma_auth.h"
// Saves connection given a filename
void saveConnection(kma_conn *conn, char *filename);
// Gets a saved connection from secondary memory
kma_conn *loadConnection(char* filename);
// Returns array of all saved connection names
char **listConnections();
#endif // PERS_CONNECTION_H_

3
src/persistence/save.c

@ -1,3 +0,0 @@
#include "save.h"

8
src/persistence/save.h

@ -1,8 +0,0 @@
// Saving to disk functions
#ifndef SAVE_H_
#define SAVE_H_
#endif // SAVE_H_
Loading…
Cancel
Save