1
0
Fork 0
forked from len0rd/rockbox

Save the scratchpad state in Sudoku plugin (FS#5737).

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@26622 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Dominik Riebeling 2010-06-06 10:59:29 +00:00
parent b2302e4e9b
commit 3ee3b28f8a
2 changed files with 38 additions and 9 deletions

View file

@ -302,6 +302,10 @@ static void save_state(struct sudoku_state_t *state)
{ {
rb->memcpy(state->savedboard, state->currentboard, rb->memcpy(state->savedboard, state->currentboard,
sizeof(state->savedboard)); sizeof(state->savedboard));
#ifdef SUDOKU_BUTTON_POSSIBLE
rb->memcpy(state->savedpossible, state->possiblevals,
sizeof(state->savedpossible));
#endif
} }
/* Copies the saved to the current board */ /* Copies the saved to the current board */
@ -309,6 +313,10 @@ static void restore_state(struct sudoku_state_t *state)
{ {
rb->memcpy(state->currentboard, state->savedboard, rb->memcpy(state->currentboard, state->savedboard,
sizeof(state->savedboard)); sizeof(state->savedboard));
#ifdef SUDOKU_BUTTON_POSSIBLE
rb->memcpy(state->possiblevals, state->savedpossible,
sizeof(state->possiblevals));
#endif
} }
void default_state(struct sudoku_state_t* state) void default_state(struct sudoku_state_t* state)
@ -420,10 +428,10 @@ bool load_sudoku(struct sudoku_state_t* state, char* filename)
{ {
int fd; int fd;
size_t n; size_t n;
int r = 0, c = 0; int r = 0, c = 0, d = 0;
unsigned int i; unsigned int i;
int valid=0; int valid=0;
char buf[300]; /* A buffer to read a sudoku board from */ char buf[500]; /* A buffer to read a sudoku board from */
fd=rb->open(filename, O_RDONLY); fd=rb->open(filename, O_RDONLY);
if (fd < 0) { if (fd < 0) {
@ -432,15 +440,15 @@ bool load_sudoku(struct sudoku_state_t* state, char* filename)
} }
rb->strlcpy(state->filename,filename,MAX_PATH); rb->strlcpy(state->filename,filename,MAX_PATH);
n=rb->read(fd,buf,300); n=rb->read(fd,buf,500);
if (n <= 0) { if (n <= 0) {
return(false); return(false);
} }
rb->close(fd); rb->close(fd);
r=0; r=0;
c=0; c=0;
i=0; i=0;
d=0;
while ((i < n) && (r < 9)) { while ((i < n) && (r < 9)) {
switch (buf[i]){ switch (buf[i]){
case ' ': case '\t': case ' ': case '\t':
@ -458,6 +466,7 @@ bool load_sudoku(struct sudoku_state_t* state, char* filename)
valid=0; valid=0;
} }
c = 0; c = 0;
d = 0;
break; break;
case '_': case '.': case '_': case '.':
valid=1; valid=1;
@ -485,6 +494,13 @@ bool load_sudoku(struct sudoku_state_t* state, char* filename)
} }
c++; c++;
} }
if((buf[i]>='a' && buf[i] <= 'z') && i < (n-1)
&& (buf[i+1] >= 'a' && buf[i+1] <= 'z')) {
state->possiblevals[r][d]
= (((buf[i]-'a') * 26 + buf[i+1]-'a')<<1);
i++;
d++;
}
/* Ignore any other characters */ /* Ignore any other characters */
break; break;
} }
@ -511,12 +527,15 @@ bool save_sudoku(struct sudoku_state_t* state)
int fd; int fd;
int r,c; int r,c;
int i; int i;
char line[13]; #ifdef SUDOKU_BUTTON_POSSIBLE
char sep[13]; int x;
char line[41]="...|...|... ; \r\n";
#else
char line[13]="...|...|...\r\n";
#endif
char sep[13]="-----------\r\n";
rb->splash(0, "Saving..."); rb->splash(0, "Saving...");
rb->memcpy(line,"...|...|...\r\n",13);
rb->memcpy(sep,"-----------\r\n",13);
if (state->filename[0]==0) { if (state->filename[0]==0) {
return false; return false;
@ -539,6 +558,15 @@ bool save_sudoku(struct sudoku_state_t* state)
i++; i++;
} }
} }
#ifdef SUDOKU_BUTTON_POSSIBLE
i+=2;
for(c=0; c<9; c++) {
x = ((state->possiblevals[r][c]>>1)/26);
line[i++] = x + 'a';
x = ((state->possiblevals[r][c]>>1)%26);
line[i++] = x + 'a';
}
#endif
rb->write(fd,line,sizeof(line)); rb->write(fd,line,sizeof(line));
if ((r==2) || (r==5)) { if ((r==2) || (r==5)) {
rb->write(fd,sep,sizeof(sep)); rb->write(fd,sep,sizeof(sep));

View file

@ -330,6 +330,7 @@ struct sudoku_state_t {
int editmode; /* We are editing the start board */ int editmode; /* We are editing the start board */
#ifdef SUDOKU_BUTTON_POSSIBLE #ifdef SUDOKU_BUTTON_POSSIBLE
short possiblevals[9][9]; /* possible values a cell could be, user sets them */ short possiblevals[9][9]; /* possible values a cell could be, user sets them */
short savedpossible[9][9]; /* cached copy of possible cell values */
#endif #endif
}; };