home *** CD-ROM | disk | FTP | other *** search
- #include "headers.h"
- #include "xfersend.h"
- #include "xferrecv.h"
- #include "win.h"
- #include "werr.h"
- #include "wimpt.h"
- #include "os.h"
- #include "bbc.h"
- #include "dboxquery.h"
- #include "kernel.h"
- #include "swis.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
-
- extern int voice_map[];
- extern char filename[];
- extern BOOL modified;
- extern BOOL loaded;
- extern char win_title[];
- extern wimp_w main_win_handle;
- extern pattern *this_patternptr;
- extern pattern *headptr;
- extern char tempo_buff[];
- extern char volume_buff[];
-
- static BOOL filename_ok(char *name);
- static void load_error(void);
- static void load_finish(BOOL, char *);
- static void save_finish(BOOL, char *);
-
- /****************************** SAVE TO FILE *********************************/
-
- BOOL save_file(char *fname, void *handle)
- {
- int i;
- pattern *patternptr = headptr;
- char *voice;
- os_filestr file;
- os_regset regs;
- FILE *fp;
-
- handle = handle;
-
- if (!filename_ok(fname)) return FALSE;
-
- if ((fp = fopen(fname,"w")) == NULL) {
- error();
- return FALSE;
- }
-
- fprintf(fp, "%d %d ", atoi(tempo_buff), atoi(volume_buff));
-
- regs.r[0] = 0;
- os_swi(Sound_Tuning, ®s);
- fprintf(fp, "%d ", regs.r[0]);
-
- for (i = 1; i <= Number_of_voices; i++)
- if (voice = voicename(i), voice != NULL) {
- fputs(voice, fp);
- fputc('\n', fp);
- } else
- fprintf(fp, "\n");
-
- while (patternptr != NULL) {
- fwrite(patternptr, sizeof(patternptr->position), 1, fp);
- patternptr = patternptr->next;
- }
-
- fclose(fp);
-
- file.action = 18;
- file.name = fname;
- file.loadaddr = Sequence_filetype;
- os_file(&file);
-
- save_finish(xfersend_file_is_safe(), fname);
- return TRUE;
- }
-
- static void save_finish(BOOL safe, char *name)
- {
- if (safe) {
- loaded = TRUE;
- strcpy(filename, name);
- set_unmodified();
- }
- }
-
- /******************************* LOAD FROM FILE *********************************/
-
- void load_file(void)
- {
- int i, j;
- int filetype;
- int bpm, volume, tuning;
- int voice_map[Number_of_voices];
- char *fname;
- FILE *fp;
- pattern *patternptr;
- char vname[256];
-
- filetype = xferrecv_checkinsert(&fname);
-
- if (filetype != Sequence_filetype) return;
- if (!filename_ok(fname)) return;
-
- if ((fp = fopen(fname, "r")) == NULL) {
- error();
- return;
- }
-
- fscanf(fp, "%d %d %d ", &bpm, &volume, &tuning);
-
- os_swi1(Sound_Tuning, tuning);
- os_swi1(Sound_Volume, volume);
- set_tempo(bpm);
-
- sprintf(tempo_buff, "%d\n", bpm);
- sprintf(volume_buff, "%d\n", volume);
-
- for (i = 1; i <= Number_of_voices; i++) {
- fgets(vname, 256, fp);
- if (vname[0] != '\n') {
- vname[strlen(vname)-1] = NULL;
- voice_map[i-1] = voice_slot(vname);
- }
- }
-
- patternptr = patterns_init();
-
- while (fread(&(patternptr->position), sizeof(patternptr->position), 1, fp)) {
-
- for (i = 0; i < Number_of_positions; i++)
- for (j = 0; j < Number_of_channels; j++)
- patternptr->position[i][j].voice = voice_map[patternptr->position[i][j].voice-1];
-
- if ((patternptr->next = create_pattern()) == NULL) {
- load_error();
- return;
- }
-
- patternptr = patternptr->next;
- }
-
- fclose(fp);
- remove_pattern(patternptr);
- load_finish(xferrecv_file_is_safe(), fname);
- }
-
- static void load_finish(BOOL safe, char *name)
- {
- if (safe) {
- strcpy(filename, name);
- loaded = TRUE;
- set_unmodified();
- } else {
- loaded = FALSE;
- set_modified();
- }
- goto_pattern(headptr);
- open_front(main_win_handle);
- input_focus(main_win_handle);
- xferrecv_insertfileok();
- }
-
- static void load_error()
- {
- wimp_close_wind(main_win_handle);
- patterns_init();
- }
-
- static BOOL filename_ok(char *name)
- {
- if (strlen(name) > Filename_bufflen) {
- werr(FALSE, "File name is too long");
- return FALSE;
- }
- return TRUE;
- }
-
-
- /*************************************************/
- /* MUST ASK SOMEBODY KNOWLEDGABLE ABOUT THIS */
- /* (scrap file not deleted, safe flag not setup) */
- /* IS THERE NOT ANOTHER WAY OF DOING THIS ??? */
- /*************************************************/
-
- void force_load_ram(wimp_msgstr *m)
- {
- wimp_msgstr reply;
-
- reply.hdr.size = sizeof(wimp_msgdatasaveok);
- reply.hdr.your_ref = m->hdr.my_ref;
- reply.hdr.action = wimp_MDATASAVEOK;
-
- reply.data.datasaveok.w = m->data.datasave.w;
- reply.data.datasaveok.i = m->data.datasave.i;
- reply.data.datasaveok.x = m->data.datasave.x;
- reply.data.datasaveok.y = m->data.datasave.y;
- reply.data.datasaveok.estsize = -1;
- reply.data.datasaveok.type = m->data.datasave.type;
- strcpy(reply.data.datasaveok.name, "<Wimp$Scrap>");
-
- wimpt_noerr(wimp_sendmessage(wimp_ESEND, &reply, m->hdr.task));
- }
-
- /**************************** PUT FILENAME IN WINDOW *********************/
-
- void main_win_title()
- {
- loaded ? strcpy(win_title, filename) : strcpy(win_title,"<untitled>");
- strcat(win_title, " (Pattern #");
- sprintf(&(win_title[strlen(win_title)]), "%d)", pattern_number(this_patternptr)+1);
- if (modified) strcat(win_title, " *");
- win_settitle(main_win_handle, win_title);
- }
-
- /*********************** SET WINDOW MODIFIED FLAG ************************/
-
- void set_unmodified()
- {
- modified = FALSE;
- main_win_title();
- }
-
- void set_modified()
- {
- modified = TRUE;
- main_win_title();
- }
-