Fixed set_lines_from_string, added generate_filename and write_lines

This commit is contained in:
Jesse van den Kieboom 2005-11-19 13:11:50 +00:00
parent 9d3d3d0e64
commit 4db386cc48
4 changed files with 127 additions and 25 deletions

View File

@ -1,9 +1,13 @@
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "gm-editor.h"
#include "gm-world.h"
#include "gm-support.h"
#include "gm-string.h"
#include "gm-debug.h"
#define GM_EDITOR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), GM_TYPE_EDITOR, GmEditorPrivate))
@ -137,7 +141,7 @@ gm_editor_set_lines_from_string(GmEditor *editor, gchar const *text) {
gm_editor_lines_free(editor);
while ((fptr = g_utf8_strchr(text, '\n', -1))) {
while ((fptr = g_utf8_strchr(text, -1, '\n'))) {
line = g_strndup(text, fptr - text);
gm_string_remove_char(line, '\r');
@ -211,3 +215,76 @@ gm_editor_close(GmEditor *editor) {
g_signal_emit(editor, editor_signals[CLOSE], 0);
}
gchar *
gm_editor_generate_filename(GmEditor *editor) {
gchar *filename, *ptr, *fptr;
gunichar c;
static guint unique_file_counter = 0;
filename = g_strdup(editor->priv->name);
ptr = filename;
fptr = filename;
// Remove any characters that are not digits or chars
// Replace spaces with underscores
while (*ptr != '\0') {
c = g_utf8_get_char(ptr);
if (c == ' ') {
*fptr++ = '_';
} else if (c == '.' || c == ':' || c == '_' || g_unichar_isalnum(c)) {
fptr += g_unichar_to_utf8(c, fptr);
}
ptr = g_utf8_next_char(ptr);
}
*fptr = '\0';
if (editor->priv->is_code) {
ptr = g_strdup_printf("%s/gnoemoe-edit.%s.%d.%s", g_get_tmp_dir(), filename,
unique_file_counter, "moo");
} else {
ptr = g_strdup_printf("%s/gnoemoe-edit.%s.%d.%s",g_get_tmp_dir(), filename,
unique_file_counter, "txt");
}
unique_file_counter++;
g_free(filename);
return ptr;
}
gchar *
gm_editor_write_lines(GmEditor *editor) {
gchar *tmp = gm_editor_generate_filename(editor);
gint fd = open(tmp, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP);
GList *line;
if (fd == -1) {
GM_DEBUG("Couldn't open file");
g_free(tmp);
return NULL;
}
// Write lines
for (line = editor->priv->lines; line; line = line->next) {
if (write(fd, line->data, strlen(line->data)) == -1) {
GM_DEBUG("Writing failed");
break;
} else {
GM_DEBUG("Written: %s", line->data);
}
if (line->next) {
if (write(fd, "\n", 1) == -1) {
GM_DEBUG("Writing failed");
break;
}
}
}
close(fd);
return tmp;
}

View File

@ -59,6 +59,7 @@ GmEditor *gm_editor_new_mcp(gchar *name, gchar *reference, gchar *type,
void gm_editor_set_lines_from_string(GmEditor *editor, gchar const *text);
void gm_editor_set_lines_from_file(GmEditor *editor, gchar const *filename);
gchar *gm_editor_write_lines(GmEditor *editor);
void gm_editor_save(GmEditor *editor);
void gm_editor_saved(GmEditor *editor);

View File

@ -1,6 +1,9 @@
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "gm-world.h"
#include "gm-app.h"
@ -45,6 +48,9 @@ struct _GmWorldPrivate {
GList *history;
GList *editors;
GmEditingInfo editing_info;
gint last_day;
gint fd_log;
};
/* Signals */
@ -84,6 +90,10 @@ gm_world_finalize(GObject *object) {
g_free(tmp_dir);
}
if (world->priv->fd_log > 0) {
close(world->priv->fd_log);
}
gm_g_list_free_simple(world->priv->history);
g_free(world->priv->path);
@ -510,22 +520,35 @@ gm_world_disconnect(GmWorld *world) {
void
gm_world_log(GmWorld *world, GmLogType type, gchar *text) {
FILE *f;
GString *s;
gchar *start, *log, *no_ansi;
struct tm *timet;
time_t timer;
gint len;
if (type == LOG_NONE) {
return;
}
timer = time(0);
timet = localtime(&timer);
log = g_strdup_printf("%s/logs/%04d-%02d-%02d.log", world->priv->path,
timet->tm_year + 1900, timet->tm_mon + 1, timet->tm_mday);
if (world->priv->fd_log <= 0 || world->priv->last_day != timet->tm_mday) {
if (world->priv->fd_log > 0) {
close(world->priv->fd_log);
}
log = g_strdup_printf("%s/logs/%04d-%02d-%02d.log", world->priv->path,
timet->tm_year + 1900, timet->tm_mon + 1, timet->tm_mday);
world->priv->fd_log = open(log, O_APPEND | O_CREAT | O_RDWR,
S_IRUSR | S_IWUSR);
g_free(log);
world->priv->last_day = timet->tm_mday;
}
f = fopen(log, "a");
g_free(log);
if (!f) {
if (world->priv->fd_log == -1) {
return;
}
@ -551,23 +574,22 @@ gm_world_log(GmWorld *world, GmLogType type, gchar *text) {
case LOG_STATUS:
s = g_string_append_c(s, '#');
break;
default:
break;
}
s = g_string_append(s, " ");
s = g_string_append(s, text);
//no_ansi = gm_ansi_strip(g_strdup(s->str));
no_ansi = g_strdup(s->str);
no_ansi = gm_ansi_strip(g_strdup(s->str));
len = strlen(no_ansi);
if (no_ansi[strlen(no_ansi) - 1] != '\n') {
fputs(no_ansi, f);
fputc('\n', f);
} else {
fputs(no_ansi, f);
write(world->priv->fd_log, no_ansi, strlen(no_ansi));
if (no_ansi[len - 1] != '\n') {
write(world->priv->fd_log, "\n", 1);
}
fclose(f);
g_free(no_ansi);
g_string_free(s, TRUE);
}
@ -637,7 +659,7 @@ gm_world_process_line(GmWorld *world, gchar *line) {
gm_world_parse_legacy_editing_start(world, non_text_start + 9);
} else {
gm_mcp_session_handle_oob(world->priv->mcp, non_text_start + 3);
//gm_world_log(world, LOG_MCP_IN, non_text_start);
gm_world_log(world, LOG_MCP_IN, non_text_start);
}
} else {
if (!gm_world_active(world)) {
@ -728,7 +750,7 @@ gm_world_add_editor(GmWorld *world, GmEditor *editor) {
}
void
gm_world_sendln_real(GmWorld *world, gchar *text, gboolean dolog) {
gm_world_sendln_log(GmWorld *world, gchar *text, GmLogType logtype) {
gchar *normal;
// Convert text from utf-8 to the correct locale
@ -736,12 +758,12 @@ gm_world_sendln_real(GmWorld *world, gchar *text, gboolean dolog) {
gm_options_get(world->priv->options, "charset"), "?");
if (!normal) {
gm_debug_msg(DEBUG_DEFAULT, "GmWorld.SendLnReal: conversion failed!");
gm_debug_msg(DEBUG_DEFAULT, "GmWorld.SendlnLog: conversion failed!");
normal = g_strdup(text);
}
if (dolog) {
gm_world_log(world, LOG_OUT, text);
if (logtype != LOG_NONE) {
gm_world_log(world, logtype, text);
}
gm_net_send_line(world->priv->net, normal);
@ -750,7 +772,7 @@ gm_world_sendln_real(GmWorld *world, gchar *text, gboolean dolog) {
void
gm_world_sendln(GmWorld *world, gchar *text) {
gm_world_sendln_real(world, text, TRUE);
gm_world_sendln_log(world, text, LOG_OUT);
}
void
@ -834,7 +856,7 @@ gm_world_auto_login(GmWorld *world) {
if (player_name && *player_name != '\0') {
sendln = g_strconcat("connect ", player_name, " ", passwd, NULL);
gm_world_sendln_real(world, sendln, FALSE);
gm_world_sendln_log(world, sendln, LOG_NONE);
g_free(sendln);
}
}

View File

@ -41,6 +41,7 @@ typedef struct _GmEditingInfo {
*/
typedef enum _GmLogType GmLogType;
enum _GmLogType {
LOG_NONE, /**< don't log */
LOG_IN, /**< incoming lines */
LOG_OUT, /**< outgoing lines */
LOG_MCP_IN, /**< mcp incoming lines */
@ -119,6 +120,7 @@ void gm_world_connect_to(GmWorld *world, gchar *host, gchar *port);
void gm_world_disconnect(GmWorld *world);
void gm_world_add_editor(GmWorld *world, GmEditor *editor);
void gm_world_remove_editor(GmWorld *world, GmEditor *editor);
void gm_world_sendln_log(GmWorld *world, gchar *text, GmLogType logtype);
void gm_world_sendln(GmWorld *world, gchar *text);
void gm_world_writeln(GmWorld *world, gchar *text);
void gm_world_process_input(GmWorld *world, gchar *text);