Skip to content

Commit

Permalink
Start to work on diff saving capabilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbens committed Apr 26, 2024
1 parent 334fd8f commit 54d6756
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
2 changes: 2 additions & 0 deletions po/POTFILES.in
Original file line number Diff line number Diff line change
Expand Up @@ -3556,6 +3556,8 @@ src/debris.h
src/debug.c
src/debug.h
src/debug_fpu.c
src/dev_diff.c
src/dev_diff.h
src/dev_mapedit.c
src/dev_mapedit.h
src/dev_spob.c
Expand Down
55 changes: 55 additions & 0 deletions src/dev_diff.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* See Licensing and Copyright notice in naev.h
*/
/**
* @file dev_diff.c
*
* @brief Handles saving unidiffs.
*/

#include "dev_diff.h"

#include "array.h"
#include "nxml.h"

int ddiff_save( UniHunk_t *diffs, const char *filename )
{
xmlDocPtr doc;
xmlTextWriterPtr writer;
int ret = 0;

/* Create the writer. */
writer = xmlNewTextWriterDoc( &doc, 0 );
if ( writer == NULL ) {
WARN( _( "testXmlwriterDoc: Error creating the xml writer" ) );
return -1;
}

/* Set the writer parameters. */
xmlw_setParams( writer );

/* Start writer. */
xmlw_start( writer );
xmlw_startElem( writer, "unidiff" );

/* Write the bulk of the diff. */
for ( int i = 0; i < array_size( diffs ); i++ ) {
}

xmlw_endElem( writer ); /* "unidiff" */
xmlw_done( writer );

/* No need for writer anymore. */
xmlFreeTextWriter( writer );

/* Write data. */
if ( xmlSaveFileEnc( filename, doc, "UTF-8" ) < 0 ) {
WARN( "Failed to write '%s'!", filename );
ret = -1;
}

/* Clean up. */
xmlFreeDoc( doc );

return ret;
}
8 changes: 8 additions & 0 deletions src/dev_diff.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* See Licensing and Copyright notice in naev.h
*/
#pragma once

#include "unidiff.h"

int ddiff_save( UniHunk_t *diffs, const char *filename );
30 changes: 24 additions & 6 deletions src/dev_uniedit.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "array.h"
#include "conf.h"
#include "dev_diff.h"
#include "dev_spob.h"
#include "dev_sysedit.h"
#include "dev_system.h"
Expand Down Expand Up @@ -77,9 +78,10 @@ typedef enum UniEditViewMode_ {

extern StarSystem *systems_stack;

int uniedit_diffMode = 0;
static UniHunk_t *uniedit_diff = NULL;
static UniEditMode uniedit_mode = UNIEDIT_DEFAULT; /**< Editor mode. */
int uniedit_diffMode = 0;
int uniedit_diffSaved = 0;
static UniHunk_t *uniedit_diff = NULL;
static UniEditMode uniedit_mode = UNIEDIT_DEFAULT; /**< Editor mode. */
static UniEditViewMode uniedit_viewmode =
UNIEDIT_VIEW_DEFAULT; /**< Editor view mode. */
static int uniedit_view_faction = -1; /**< Faction currently being viewed. */
Expand Down Expand Up @@ -201,7 +203,8 @@ void uniedit_open( unsigned int wid_unused, const char *unused )

/* Must have no diffs applied. */
diff_clear();
uniedit_diff = array_create( UniHunk_t );
uniedit_diff = array_create( UniHunk_t );
uniedit_diffSaved = 1; /* Start OK with empty diffs. */

/* Reset some variables. */
uniedit_mode = UNIEDIT_DEFAULT;
Expand Down Expand Up @@ -368,6 +371,15 @@ static int uniedit_keys( unsigned int wid, SDL_Keycode key, SDL_Keymod mod,
*/
static void uniedit_close( unsigned int wid, const char *wgt )
{
if ( uniedit_diffMode && !uniedit_diffSaved ) {
if ( !dialogue_YesNoRaw(
_( "#rUnsaved Progress" ),
_( "You have #runsaved changes#0 to the universe diff. Are you "
"sure you wish to close the editor and #rlose all your "
"changes#0?" ) ) )
return;
}

uniedit_diffClear();

/* Frees some memory. */
Expand All @@ -391,8 +403,13 @@ static void uniedit_save( unsigned int wid_unused, const char *unused )
(void)wid_unused;
(void)unused;

dsys_saveAll();
dpl_saveAll();
if ( uniedit_diffMode ) {
ddiff_save( uniedit_diff, "test.xml" );
uniedit_diffSaved = 1;
} else {
dsys_saveAll();
dpl_saveAll();
}
}

/*
Expand Down Expand Up @@ -2684,6 +2701,7 @@ static void uniedit_diffClear( void )

void uniedit_diffAdd( UniHunk_t *hunk )
{
uniedit_diffSaved = 0; /* Unsaved progress. */
diff_start();

/* Replace if already same type exists. */
Expand Down
2 changes: 2 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ source = files(
'debris.c',
'debug.c',
'debug_fpu.c',
'dev_diff.c',
'dev_mapedit.c',
'dev_spob.c',
'dev_sysedit.c',
Expand Down Expand Up @@ -222,6 +223,7 @@ headers = files(
'damagetype.h',
'debris.h',
'debug.h',
'dev_diff.h',
'dev_mapedit.h',
'dev_spob.h',
'dev_sysedit.h',
Expand Down

0 comments on commit 54d6756

Please sign in to comment.