version 1.8, 2001/11/15 21:42:42
|
version 1.11, 2001/12/05 18:58:21
|
Line 1
|
Line 1
|
|
/* The LearningOnline Network with CAPA |
|
* Helaper functions for capa convertor. |
|
* $Id$ |
|
* |
|
* Copyright Michigan State University Board of Trustees |
|
* |
|
* This file is part of the LearningOnline Network with CAPA (LON-CAPA). |
|
* |
|
* LON-CAPA is free software; you can redistribute it and/or modify |
|
* it under the terms of the GNU General Public License as published by |
|
* the Free Software Foundation; either version 2 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* LON-CAPA is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU General Public License |
|
* along with LON-CAPA; if not, write to the Free Software |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
* |
|
* /home/httpd/html/adm/gpl.txt |
|
* |
|
* http://www.lon-capa.org/ |
|
*/ |
|
|
/* =||>|===================== capaCommon.c =====================|<||= */ |
/* =||>|===================== capaCommon.c =====================|<||= */ |
/* created 1994 by Isaac Tsai */ |
/* created 1994 by Isaac Tsai */ |
/* 1994, 1995, 1996, 1997, 1998, 1999 copyrighted by Isaac Tsai */ |
/* 1994, 1995, 1996, 1997, 1998, 1999 copyrighted by Isaac Tsai */ |
Line 153 extern void (*Status_Func)();
|
Line 180 extern void (*Status_Func)();
|
begin_text(); |
begin_text(); |
/*if ( !yyparse() ) { errcode = Lexi_qnum; } else { errcode = 0; }*/ |
/*if ( !yyparse() ) { errcode = Lexi_qnum; } else { errcode = 0; }*/ |
if (!(temp=yylex())) { errcode = Lexi_qnum; } else { errcode = 0; } |
if (!(temp=yylex())) { errcode = Lexi_qnum; } else { errcode = 0; } |
/* printf("\nExited on: %d\n",temp); */ |
fprintf(stderr,"\nExited on: %d\n",temp); |
/* printf("Flushing:\n"); */ |
fprintf(stderr,"Current cache: %d\n",current_cache); |
|
fprintf(stderr,"Flushing:\n"); |
flush_delayed(); |
flush_delayed(); |
/* fclose(Input_stream[0]);*/ /*The Lexer handles closing this*/ |
/* fclose(Input_stream[0]);*/ /*The Lexer handles closing this*/ |
/* print_symb_stat(); */ |
/* print_symb_stat(); */ |
Line 169 extern void (*Status_Func)();
|
Line 197 extern void (*Status_Func)();
|
|
|
int dyn_maxlen=1000000; |
int dyn_maxlen=1000000; |
int delay; |
int delay; |
void dyn_init() |
void dyn_init(struct dyn_string *dyn) |
{ |
{ |
dyn_delayed.len=0; |
dyn->len=0; |
dyn_delayed.max=0; |
dyn->max=0; |
dyn_delayed.str=NULL; |
dyn->str=NULL; |
} |
} |
|
|
/* this can be used to free up the str components if the output |
void dyn_free(struct dyn_string* dyn) |
compenants are used internally, as in the reinit code |
|
*/ |
|
void dyn_free() |
|
{ |
{ |
if (dyn_delayed.str) {free(dyn_delayed.str);dyn_delayed.str=NULL;} |
if (dyn->str) { |
|
free(dyn->str);dyn->str=NULL; |
|
dyn->len=0; |
|
dyn->max=0; |
|
} |
} |
} |
|
|
|
|
int append_message(struct dyn_string *dyn_msg,char *format,va_list ap) { |
int append_message(struct dyn_string *dyn_msg,char *format,va_list ap) { |
char *new; |
char *new; |
int len,result; |
int len,result; |
|
|
result=vasprintf(&new,format,ap); |
if ((result=vasprintf(&new,format,ap))==-1) { |
|
fprintf(stderr,"vaspintf didn't like :%s:",format); |
|
exit(1); |
|
} |
len=strlen(new); |
len=strlen(new); |
|
|
#ifdef DYN_DEBUG |
#ifdef DYN_DEBUG |
Line 237 void add_delayed(char *format, ...) {
|
Line 270 void add_delayed(char *format, ...) {
|
|
|
va_start(ap,format); |
va_start(ap,format); |
append_message(&dyn_delayed,format,ap); |
append_message(&dyn_delayed,format,ap); |
} |
if (do_cache[current_cache]) { |
|
append_message(&cached_data[current_cache],format,ap); |
|
} |
|
} |
|
|
void flush_delayed() |
void flush_delayed() |
{ |
{ |
if (dyn_delayed.str) fputs(dyn_delayed.str,stdout); |
|
dyn_free();dyn_init(); |
|
delay=0; |
delay=0; |
|
if (dyn_delayed.str) { send(dyn_delayed.str); } |
|
dyn_free(&dyn_delayed);dyn_init(&dyn_delayed); |
|
} |
|
|
|
|
|
int current_dest=DEFAULT_DEST; |
|
void change_destination(int which_dest) |
|
{ |
|
if (which_dest < MAX_DEST) { |
|
current_dest = which_dest; |
|
} else { |
|
fprintf(stderr,"Tried to set destination above MAX_DEST: %d", which_dest); |
|
exit(1); |
|
} |
|
} |
|
|
|
void send_to(int which_dest, int which, char *text, va_list ap) |
|
{ |
|
if (delay) { |
|
append_message(&dyn_delayed,text,ap); |
|
} else { |
|
if (num_streams[which_dest]) { |
|
if (which == ALL_STREAMS) { |
|
int i; |
|
for (i=0;i<num_streams[which_dest];i++) { append_message(&streams[which_dest][i],text,ap); } |
|
} else { |
|
append_message(&streams[which_dest][which],text,ap); |
|
} |
|
} else { |
|
vprintf(text,ap); |
|
} |
|
} |
|
if (do_cache[current_cache]) { |
|
append_message(&cached_data[current_cache],text,ap); |
|
} |
} |
} |
|
|
void send(char *text,...) |
void send(char *text,...) |
{ |
{ |
va_list ap; |
va_list ap; |
va_start(ap,text); |
va_start(ap,text); |
if (delay) { |
send_to(current_dest,ALL_STREAMS,text,ap); |
append_message(&dyn_delayed,text,ap); |
} |
|
|
|
void send_stream(int which, char *text,...) |
|
{ |
|
va_list ap; |
|
va_start(ap,text); |
|
send_to(current_dest,which,text,ap); |
|
} |
|
|
|
int num_streams[MAX_DEST]; |
|
struct dyn_string streams[MAX_DEST][MAX_STREAMS]; |
|
void start_streams(int which_dest, int num) { |
|
int i; |
|
for(i=0; i<num; i++) { dyn_init(&streams[which_dest][i]); } |
|
for(i=1; i<num; i++) { beg_mode[which_dest][i]=mode[current_dest][0]; |
|
mode[which_dest][i]=mode[current_dest][0]; } |
|
num_streams[which_dest]=num; |
|
current_dest = which_dest; |
|
} |
|
|
|
void end_streams(int which_dest, int which) { |
|
int i; |
|
start_mode(beg_mode[which_dest][which],NULL); |
|
fputs(streams[which_dest][which].str,stdout); |
|
for(i=0; i<num_streams[which_dest]; i++) { dyn_free(&streams[which_dest][which]); } |
|
num_streams[which_dest]=0; |
|
mode[DEFAULT_DEST][0]=mode[which_dest][which]; |
|
start_mode(mode[DEFAULT_DEST][0],NULL); |
|
} |
|
|
|
int is_dest_empty(int which_dest) { |
|
int i,empty=1; |
|
|
|
if ( num_streams[which_dest] ) { |
|
for(i=0; i<num_streams[which_dest]; i++) { |
|
if (streams[which_dest][i].str != NULL) { empty=0; break; } |
|
} |
} else { |
} else { |
vprintf(text,ap); |
if (streams[which_dest][0].str != NULL) { empty=0; } |
} |
} |
|
return empty; |
} |
} |
|
|
|
int mode[MAX_DEST][MAX_STREAMS]; |
|
int watch_mode[MAX_DEST][MAX_STREAMS]; |
|
int beg_mode[MAX_DEST][MAX_STREAMS]; |
void end_mode() |
void end_mode() |
{ |
{ |
switch (mode) { |
end_mode_stream(current_dest, ALL_STREAMS); |
case MODE_COMMENT: send("</comment>\n"); break; |
} |
case MODE_BLOCK: send("</block>\n"); break; |
|
case MODE_SCRIPT: send("</script>\n"); break; |
void end_mode_stream(int which_dest, int which) |
case MODE_OUTTEXT: send("<endouttext />\n"); break; |
{ |
case MODE_ANSWER: send("\n"); break; |
if (num_streams[which_dest]) { |
case MODE_HINT: send("<endouttext />\n</hintpart>\n</hintgroup>\n"); break; |
if (which == ALL_STREAMS) { |
|
int i; |
|
for (i=0;i<num_streams[which_dest];i++) { end_mode_stream(which_dest,i); } |
|
return; |
|
} |
|
} else { |
|
which=0;/* if streams aren't active make sure which is correct */ |
|
} |
|
switch (mode[which_dest][which]) { |
|
case MODE_COMMENT: send_stream(which,"</comment>\n"); break; |
|
case MODE_BLOCK: send_stream(which,"</block>\n"); break; |
|
case MODE_SCRIPT: send_stream(which,"</script>\n"); break; |
|
case MODE_OUTTEXT: send_stream(which,"<endouttext />\n"); break; |
|
case MODE_ANSWER: send_stream(which,"\n"); break; |
|
case MODE_IMPORT: send_stream(which,"</import>\n"); break; |
case MODE_NONE: break; |
case MODE_NONE: break; |
} |
} |
mode=MODE_NONE; |
mode[which_dest][which]=MODE_NONE; |
|
watch_mode[which_dest][which]=0; |
} |
} |
|
|
void start_mode(int newmode,char* args) |
void start_mode(int newmode,char* args) |
{ |
{ |
if (newmode == mode) return; |
start_mode_stream(current_dest,ALL_STREAMS,newmode,args); |
end_mode(); |
} |
|
|
|
void start_mode_stream(int which_dest,int which,int newmode,char* args) |
|
{ |
|
if (num_streams[which_dest]) { |
|
if (which == ALL_STREAMS) { |
|
int i; |
|
for (i=0;i<num_streams[which_dest];i++) { start_mode_stream(which_dest,i,newmode,args); } |
|
return; |
|
} else { |
|
if (newmode == mode[which_dest][which]) return; |
|
} |
|
} else { |
|
if (newmode == mode[which_dest][0]) return; |
|
which=0;/* if streams aren't active make sure which is correct */ |
|
} |
|
end_mode_stream(which_dest,which); |
switch (newmode) { |
switch (newmode) { |
case MODE_COMMENT: send("<comment>\n"); break; |
case MODE_COMMENT: send_stream(which,"<comment>\n"); break; |
case MODE_BLOCK: send("<block %s>\n",args); break; |
case MODE_BLOCK: send_stream(which,"<block %s>\n",args); break; |
case MODE_SCRIPT: send("<script type=\"loncapa/perl\">\n"); break; |
case MODE_SCRIPT: send_stream(which,"<script type=\"loncapa/perl\">\n"); break; |
case MODE_OUTTEXT: send("<startouttext />\n"); break; |
case MODE_OUTTEXT: send_stream(which,"<startouttext />\n"); break; |
case MODE_ANSWER: send("\n"); break; |
case MODE_ANSWER: send_stream(which,"\n"); break; |
case MODE_HINT: send("<hintgroup>\n<hintpart on=\"default\">\n<startouttext />\n"); break; |
case MODE_IMPORT: send_stream(which,"<import>"); break; |
case MODE_NONE: break; |
case MODE_NONE: break; |
} |
} |
mode=newmode; |
mode[which_dest][which]=newmode; |
|
} |
|
|
|
int current_cache=-1; |
|
int do_cache[MAX_CACHE]; |
|
struct dyn_string cached_data[MAX_CACHE]; |
|
|
|
void new_cache() |
|
{ |
|
current_cache++; |
|
do_cache[current_cache]=1; |
|
if (current_cache>MAX_CACHE) { exit(CACHE_ERROR); } |
|
dyn_init(&cached_data[current_cache]); |
|
} |
|
|
|
void start_cache() |
|
{ |
|
do_cache[current_cache]=1; |
|
} |
|
|
|
void stop_cache() |
|
{ |
|
do_cache[current_cache]=0; |
|
} |
|
|
|
void delete_cache() |
|
{ |
|
if (current_cache > -1) { |
|
dyn_free(&cached_data[current_cache]); |
|
current_cache--; |
|
} |
} |
} |
/* =||>|===================== End of capaCommon.c =====================|<||= */ |
/* =||>|===================== End of capaCommon.c =====================|<||= */ |
|
|