1 /*
2     scsort.c:
3 
4     Copyright (C) 1991 Barry Vercoe, John ffitch
5 
6     This file is part of Csound.
7 
8     The Csound Library is free software; you can redistribute it
9     and/or modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation; either
11     version 2.1 of the License, or (at your option) any later version.
12 
13     Csound is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public
19     License along with Csound; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21     02110-1301 USA
22 */
23 
24 #include "csoundCore.h"                                  /*   SCSORT.C  */
25 #include "corfile.h"
26 #include <ctype.h>
27 
28 extern void sort(CSOUND*);
29 extern void twarp(CSOUND*);
30 extern void swritestr(CSOUND*, CORFIL *sco, int first);
31 extern void sfree(CSOUND *csound);
32 //extern void sread_init(CSOUND *csound);
33 extern int  sread(CSOUND *csound);
34 
35 /* called from smain.c or some other main */
36 /* reads,sorts,timewarps each score sect in turn */
37 
38 extern void sread_initstr(CSOUND *, CORFIL *sco);
scsortstr(CSOUND * csound,CORFIL * scin)39 char *scsortstr(CSOUND *csound, CORFIL *scin)
40 {
41     int     n;
42     int     first = 0;
43     CORFIL *sco;
44 
45     csound->scoreout = NULL;
46     if (csound->scstr == NULL && (csound->engineStatus & CS_STATE_COMP) == 0) {
47       first = 1;
48       sco = csound->scstr = corfile_create_w(csound);
49     }
50     else sco = corfile_create_w(csound);
51     csound->sectcnt = 0;
52     sread_initstr(csound, scin);
53 
54     while ((n = sread(csound)) > 0) {
55       if (csound->frstbp->text[0] == 's') { // ignore empty segment
56         // should this free memory?
57         //printf("repeated 's'\n");
58         continue;
59       }
60       sort(csound);
61       twarp(csound);
62       swritestr(csound, sco, first);
63       //printf("sorted: >>>%s<<<\n", sco->body);
64     }
65     //printf("**** first = %d body = >>%s<<\n", first, sco->body);
66     if (first) {
67       int i = 0;
68       while (isspace(sco->body[i])) i++;
69       if (sco->body[i] == 'e' && sco->body[i+1] == '\n' && sco->body[i+2] != 'e') {
70         corfile_rewind(sco);
71         corfile_puts(csound, "f0 800000000000.0\ne\n", sco); /* ~25367 years */
72       }
73       else corfile_puts(csound, "e\n", sco);
74       //printf("body >>%s<<\n", sco->body);
75     }
76     corfile_flush(csound, sco);
77     sfree(csound);
78     if (first) {
79       return sco->body;
80     }
81     else {
82       char *str = cs_strdup(csound,sco->body);
83       corfile_rm(csound, &(sco));
84       return str;
85     }
86 }
87 
88