Skip to content

Commit

Permalink
save buffers with no file attached
Browse files Browse the repository at this point in the history
- file: extend save to ignore missing src file
- cmd: buffer_save: use buffer_mgr to save current instead of direct
- cmd: buffer_load: if filename is empty create new buffer (no file attached)
- buffer_mgr: add rubouts to 'add' methods to decrease counters, also add return
- buffer_mgr: add internal save method wich converts TECO-Main to normal buffer
- buffer: init: if no name given "new buffer" is used
  • Loading branch information
Yomin committed Nov 10, 2012
1 parent cca8a96 commit 498ee44
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 73 deletions.
22 changes: 16 additions & 6 deletions buffer.c
Expand Up @@ -31,6 +31,7 @@

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

// DEFINES

Expand Down Expand Up @@ -89,6 +90,9 @@ char buf[BUFFER_STATUS_SIZE];

void buffer_init(const char* name, int number, struct buffer_state* buffer)
{
if(!name)
name = "new buffer";

strncpy(buffer->name, name, BUFFER_NAME_SIZE-1);
buffer->name[MIN(strlen(name), BUFFER_NAME_SIZE-1)] = 0;
buffer->number = number;
Expand Down Expand Up @@ -183,9 +187,14 @@ void buffer_flush(struct buffer_state* buffer)
int buffer_save(const char* filename, struct buffer_state* buffer)
{
buffer_flush(buffer);
int unsaved = !buffer->file.file;

int ret = buffer_map_error(file_save(filename, &buffer->file));
if(ret)
return ret;

if(unsaved)
strcpy(buffer->name, basename(buffer->file.name));
buffer->status &= ~BUFFER_STATUS_MODIFIED;
screen_set_status(buffer_status(buffer));
return 0;
Expand Down Expand Up @@ -824,12 +833,13 @@ int buffer_map_error(int fileerror)
{
switch(fileerror)
{
case 0: return 0;
case FILE_ERROR_NOT_FOUND: return BUFFER_ERROR_FILE_NOT_FOUND;
case FILE_ERROR_NAME_SIZE: return BUFFER_ERROR_FILE_NAME_SIZE;
case FILE_ERROR_NO_SPACE: return BUFFER_ERROR_FILE_NO_SPACE;
case FILE_ERROR_CANT_WRITE: return BUFFER_ERROR_FILE_CANT_WRITE;
case FILE_ERROR_SRC_LOST: return BUFFER_ERROR_FILE_SRC_LOST;
case 0: return 0;
case FILE_ERROR_NOT_FOUND: return BUFFER_ERROR_FILE_NOT_FOUND;
case FILE_ERROR_NAME_SIZE: return BUFFER_ERROR_FILE_NAME_SIZE;
case FILE_ERROR_NO_SPACE: return BUFFER_ERROR_FILE_NO_SPACE;
case FILE_ERROR_CANT_WRITE: return BUFFER_ERROR_FILE_CANT_WRITE;
case FILE_ERROR_SRC_LOST: return BUFFER_ERROR_FILE_SRC_LOST;
case FILE_ERROR_NAME_NEEDED: return BUFFER_ERROR_FILE_NAME_NEEDED;
}
THROW(EXCEPTION_UNKNOWN_RETURN);
return 0;
Expand Down
15 changes: 8 additions & 7 deletions buffer.h
Expand Up @@ -46,13 +46,14 @@

#define BUFFER_NAME_SIZE 80

#define BUFFER_ERROR_FILE_NOT_FOUND -1
#define BUFFER_ERROR_FILE_NAME_SIZE -2
#define BUFFER_ERROR_FILE_NO_SPACE -3
#define BUFFER_ERROR_FILE_CANT_WRITE -4
#define BUFFER_ERROR_FILE_SRC_LOST -5
#define BUFFER_ERROR_BEGIN -6
#define BUFFER_ERROR_END -7
#define BUFFER_ERROR_BEGIN -1
#define BUFFER_ERROR_END -2
#define BUFFER_ERROR_FILE_NOT_FOUND -3
#define BUFFER_ERROR_FILE_NAME_SIZE -4
#define BUFFER_ERROR_FILE_NO_SPACE -5
#define BUFFER_ERROR_FILE_CANT_WRITE -6
#define BUFFER_ERROR_FILE_NAME_NEEDED -7
#define BUFFER_ERROR_FILE_SRC_LOST -8

struct buffer_state
{
Expand Down
118 changes: 107 additions & 11 deletions buffer_mgr.c
Expand Up @@ -92,6 +92,58 @@ int add(const char* name, int number, const char* file)
return 0;
}

void mgr_save_rubout()
{
struct buffer_state* buf;
rubout_load(&buf);
rubout_load(&buf->status);
int num = -1;
list_remove(match_by_number, &num, &buffer_list);
--count_buffer;
buf->number = -1;
strcpy(buf->name, "TECO-Main");
buf->file.name[0] = 0;
if(buf->file.file)
{
fclose(buf->file.file);
buf->file.file = 0;
}
screen_set_status(buffer_status(buf));
}

int mgr_save(struct buffer_state* buf, const char* name)
{
int tmain = buf->number == -1;
char status = buf->status;

if(tmain)
{
struct buffer_state* intern = list_add(0, &buffer_list);
buffer_init("TECO-Main", -1, intern);
buf->number = ++count_buffer;
}

int ret = buffer_mgr_map_error(buffer_save(name, buf));

if(tmain)
{
if(ret)
{
int num = -1;
list_remove(match_by_number, &num, &buffer_list);
buf->number = -1;
--count_buffer;
}
else
{
rubout_save(&status, sizeof(char));
rubout_register(mgr_save_rubout, &buf, sizeof(struct buffer_state*));
}
}

return ret;
}

void mgr_add_rubout()
{
int previous, current = buffer_mgr_current()->number;
Expand Down Expand Up @@ -136,19 +188,44 @@ void buffer_mgr_flush()
list_map((mapFunc*)buffer_flush, &buffer_list);
}

void buffer_mgr_add(const char* name)
void buffer_mgr_add_rubout()
{
--count_buffer;
}

int buffer_mgr_add(const char* name)
{
int ret = mgr_add(name, ++count_buffer, 0);
if(!ret)
rubout_register_s(buffer_mgr_add_rubout);
return ret;
}

void buffer_mgr_add_intern_rubout()
{
mgr_add(name, ++count_buffer, 0);
++count_intern;
}

void buffer_mgr_add_intern(const char* name)
int buffer_mgr_add_intern(const char* name)
{
mgr_add(name, --count_intern, 0);
int ret = mgr_add(name, --count_intern, 0);
if(!ret)
rubout_register_s(buffer_mgr_add_intern_rubout);
return ret;
}

int buffer_mgr_add_file(const char* name, const char* file)
{
return mgr_add(name, ++count_buffer, file);
int ret = mgr_add(name, ++count_buffer, file);
if(!ret)
rubout_register_s(buffer_mgr_add_rubout);
return ret;
}

void buffer_mgr_add_new()
{
mgr_add(0, ++count_buffer, 0);
rubout_register_s(buffer_mgr_add_rubout);
}

void buffer_mgr_delete_rubout()
Expand All @@ -165,7 +242,7 @@ int buffer_mgr_delete(int number)
{
if(number == -1)
return BUFFER_MGR_ERROR_ACCESS_FORBIDDEN;
struct buffer_state* buf = (struct buffer_state*) list_get(number, &buffer_list);
struct buffer_state* buf = list_get(number, &buffer_list);
if(!buf)
return BUFFER_MGR_ERROR_NOT_FOUND;
char* name = buf->file.name;
Expand All @@ -175,6 +252,20 @@ int buffer_mgr_delete(int number)
return 0;
}

int buffer_mgr_save(int number, const char* name)
{
struct buffer_state* buf = list_get(number, &buffer_list);
if(!buf)
return BUFFER_MGR_ERROR_NOT_FOUND;
return mgr_save(buf, name);
}

int buffer_mgr_save_current(const char* name)
{
struct buffer_state* buf = list_current(&buffer_list);
return mgr_save(buf, name);
}

struct buffer_state* buffer_mgr_current()
{
return list_current(&buffer_list);
Expand All @@ -200,6 +291,9 @@ void buffer_mgr_register_rubouts()
{
buffer_register_rubouts();
rubout_ptr_register(mgr_add_rubout);
rubout_ptr_register(mgr_save_rubout);
rubout_ptr_register(buffer_mgr_add_rubout);
rubout_ptr_register(buffer_mgr_add_intern_rubout);
rubout_ptr_register(buffer_mgr_delete_rubout);
rubout_ptr_register(buffer_mgr_switch_rubout);
}
Expand All @@ -208,11 +302,13 @@ int buffer_mgr_map_error(int buffererror)
{
switch(buffererror)
{
case 0: return 0;
case BUFFER_ERROR_FILE_NOT_FOUND: return BUFFER_MGR_ERROR_FILE_NOT_FOUND;
case BUFFER_ERROR_FILE_NAME_SIZE: return BUFFER_MGR_ERROR_FILE_NAME_SIZE;
case BUFFER_ERROR_FILE_NO_SPACE: return BUFFER_MGR_ERROR_FILE_NO_SPACE;
case BUFFER_ERROR_FILE_CANT_WRITE: return BUFFER_MGR_ERROR_FILE_CANT_WRITE;
case 0: return 0;
case BUFFER_ERROR_FILE_NOT_FOUND: return BUFFER_MGR_ERROR_FILE_NOT_FOUND;
case BUFFER_ERROR_FILE_NAME_SIZE: return BUFFER_MGR_ERROR_FILE_NAME_SIZE;
case BUFFER_ERROR_FILE_NO_SPACE: return BUFFER_MGR_ERROR_FILE_NO_SPACE;
case BUFFER_ERROR_FILE_CANT_WRITE: return BUFFER_MGR_ERROR_FILE_CANT_WRITE;
case BUFFER_ERROR_FILE_SRC_LOST: return BUFFER_MGR_ERROR_FILE_SRC_LOST;
case BUFFER_ERROR_FILE_NAME_NEEDED: return BUFFER_MGR_ERROR_FILE_NAME_NEEDED;
}
THROW(EXCEPTION_UNKNOWN_RETURN);
return 0;
Expand Down
22 changes: 14 additions & 8 deletions buffer_mgr.h
Expand Up @@ -30,21 +30,27 @@
#include "buffer.h"

#define BUFFER_MGR_ERROR_ACCESS_FORBIDDEN -1
#define BUFFER_MGR_ERROR_FILE_NOT_FOUND -2
#define BUFFER_MGR_ERROR_FILE_NAME_SIZE -3
#define BUFFER_MGR_ERROR_FILE_NO_SPACE -4
#define BUFFER_MGR_ERROR_FILE_CANT_WRITE -5
#define BUFFER_MGR_ERROR_NOT_FOUND -6
#define BUFFER_MGR_ERROR_EXISTING -7
#define BUFFER_MGR_ERROR_NOT_FOUND -2
#define BUFFER_MGR_ERROR_EXISTING -3
#define BUFFER_MGR_ERROR_FILE_NOT_FOUND -4
#define BUFFER_MGR_ERROR_FILE_NAME_SIZE -5
#define BUFFER_MGR_ERROR_FILE_NO_SPACE -6
#define BUFFER_MGR_ERROR_FILE_CANT_WRITE -7
#define BUFFER_MGR_ERROR_FILE_SRC_LOST -8
#define BUFFER_MGR_ERROR_FILE_NAME_NEEDED -9


void buffer_mgr_init();
void buffer_mgr_finish();
void buffer_mgr_flush();

void buffer_mgr_add(const char* name);
void buffer_mgr_add_intern(const char* name);
int buffer_mgr_add(const char* name);
int buffer_mgr_add_intern(const char* name);
int buffer_mgr_add_file(const char* name, const char* file);
void buffer_mgr_add_new();
int buffer_mgr_delete(int number);
int buffer_mgr_save(int number, const char* name);
int buffer_mgr_save_current(const char* name);

struct buffer_state* buffer_mgr_current();
struct buffer_state* buffer_mgr_switch(int number);
Expand Down
20 changes: 10 additions & 10 deletions cmd.c
Expand Up @@ -243,10 +243,7 @@ struct cmd_ret extra_exit(int given, int param)
struct cmd_ret extra_buffer_load_func(char* str)
{
if(strlen(str) == 0)
{
screen_set_msg("filename required");
return ret(CMD_RET_FAILURE|CMD_MASK_MSG, 0);
}
buffer_mgr_add_new();
else
{
switch(buffer_mgr_add_file(basename(str), str))
Expand All @@ -258,8 +255,8 @@ struct cmd_ret extra_buffer_load_func(char* str)
screen_set_msg("filename to long");
return ret(CMD_RET_FAILURE|CMD_MASK_MSG, 0);
}
return ret(CMD_RET_SUCCESS, 0);
}
return ret(CMD_RET_SUCCESS, 0);
}

struct cmd_ret extra_buffer_load(int given, int param)
Expand Down Expand Up @@ -297,20 +294,23 @@ struct cmd_ret extra_buffer_save_func(char* str)
if(strlen(str) == 0)
str = 0;

switch(buffer_save(str, buffer_mgr_current()))
switch(buffer_mgr_save_current(str))
{
case BUFFER_ERROR_FILE_NO_SPACE:
case BUFFER_MGR_ERROR_FILE_NO_SPACE:
screen_set_msg("no space left");
return ret(CMD_RET_FAILURE|CMD_MASK_MSG, 0);
case BUFFER_ERROR_FILE_CANT_WRITE:
case BUFFER_MGR_ERROR_FILE_CANT_WRITE:
screen_set_msg("write failed");
return ret(CMD_RET_FAILURE|CMD_MASK_MSG, 0);
case BUFFER_ERROR_FILE_NAME_SIZE:
case BUFFER_MGR_ERROR_FILE_NAME_SIZE:
screen_set_msg("filename to long");
return ret(CMD_RET_FAILURE|CMD_MASK_MSG, 0);
case BUFFER_ERROR_FILE_SRC_LOST:
case BUFFER_MGR_ERROR_FILE_SRC_LOST:
screen_set_msg("source file not available anymore");
return ret(CMD_RET_FAILURE|CMD_MASK_MSG, 0);
case BUFFER_MGR_ERROR_FILE_NAME_NEEDED:
screen_set_msg("filename needed");
return ret(CMD_RET_FAILURE|CMD_MASK_MSG, 0);
}

return ret(CMD_RET_SUCCESS, 0);
Expand Down

0 comments on commit 498ee44

Please sign in to comment.