--- loncom/homework/CAPA-converter/capaCommon.c 2000/05/03 15:35:11 1.2
+++ loncom/homework/CAPA-converter/capaCommon.c 2001/07/11 23:00:57 1.7
@@ -111,7 +111,7 @@ capa_parse(set,problem,filename,num_ques
int set;Problem_t **problem;char *filename;int *num_questions;
void (*func_ptr)();
{
- int errcode;
+ int errcode,temp;
extern FILE *Input_stream[MAX_OPENED_FILE];
extern char Opened_filename[MAX_OPENED_FILE][QUARTER_K];
extern int Lexi_line;
@@ -152,7 +152,10 @@ extern void (*Status_Func)();
/*yyrestart(yyin);*/
begin_text();
/*if ( !yyparse() ) { errcode = Lexi_qnum; } else { errcode = 0; }*/
- if (!yylex()) { errcode = Lexi_qnum; } else { errcode = 0; }
+ if (!(temp=yylex())) { errcode = Lexi_qnum; } else { errcode = 0; }
+ printf("\nExited on: %d\n",temp);
+ printf("Flushing:\n");
+ flush_delayed();
/* fclose(Input_stream[0]);*/ /*The Lexer handles closing this*/
/* print_symb_stat(); */
/*
@@ -164,25 +167,122 @@ extern void (*Status_Func)();
return (errcode);
}
-char* delayed;
+int dyn_maxlen=1000000;
+int delay;
+void dyn_init()
+{
+ dyn_delayed.len=0;
+ dyn_delayed.max=0;
+ dyn_delayed.str=NULL;
+}
+
+/* this can be used to free up the str components if the output
+ compenants are used internally, as in the reinit code
+*/
+void dyn_free()
+{
+ if (dyn_delayed.str) {free(dyn_delayed.str);dyn_delayed.str=NULL;}
+}
+
+int append_message(struct dyn_string *dyn_msg,char *format,va_list ap) {
+ char *new;
+ int len,result;
+
+ result=vasprintf(&new,format,ap);
+ len=strlen(new);
+
+#ifdef DYN_DEBUG
+ fprintf(stderr,"before: len %d; gcount %d; max %d\n",
+ len,dyn_msg->len,dyn_msg->max);
+#endif /* DYN_DEBUG */
+
+ if (dyn_msg->len+len < dyn_maxlen) {
+ if (dyn_msg->len+len>dyn_msg->max-2) {
+ dyn_msg->max=(dyn_msg->len+len)*2;
+ if (dyn_msg->max>dyn_maxlen) { dyn_msg->max=dyn_maxlen; }
+ if (dyn_msg->max != 0) {
+ dyn_msg->str=realloc(dyn_msg->str,dyn_msg->max);
+ } else {
+ return 1;
+ }
+ dyn_msg->str[dyn_msg->len]='\0';
+ }
+ strcat(dyn_msg->str,new);
+ dyn_msg->len+=len;
+ } else {
+ if (dyn_msg->max != dyn_maxlen-1) { /*already maxed out or can
+ we fit this one in?*/
+ dyn_msg->max=dyn_maxlen;
+ dyn_msg->str=realloc(dyn_msg->str,dyn_msg->max);
+ dyn_msg->str[dyn_msg->len]='\0';
+ strncat(dyn_msg->str,new,dyn_msg->max-dyn_msg->len-1);
+ dyn_msg->len=strlen(dyn_msg->str);
+ }
+ }
+ free(new);
+
+#ifdef DYN_DEBUG
+ fprintf(stderr,"after: len %d; gcount %d; max %d; strlen(dyn_msg): %d\n",
+ len,dyn_msg->len,dyn_msg->max,strlen(dyn_msg->str));
+#endif /* DYN_DEBUG */
+
+ return 1;
+}
+
+void start_delayed(){ delay=1; }
+void end_delayed(){ delay=0; }
+
+void add_delayed(char *format, ...) {
+ va_list ap;
+
+ va_start(ap,format);
+ append_message(&dyn_delayed,format,ap);
+}
+
+void flush_delayed()
+{
+ if (dyn_delayed.str) fputs(dyn_delayed.str,stdout);
+ dyn_free();dyn_init();
+ delay=0;
+}
+
void send(char *text,...)
{
va_list ap;
- if (delayed) {printf(delayed);free(delayed);delayed=NULL;}
va_start(ap,text);
- vprintf(text,ap);
+ if (delay) {
+ append_message(&dyn_delayed,text,ap);
+ } else {
+ vprintf(text,ap);
+ }
}
-void send_delayed(char *text,...)
+void end_mode()
{
- va_list ap;
- va_start(ap,text);
- vasprintf(&delayed,text,ap);
+ switch (mode) {
+ case MODE_COMMENT: send("\n"); break;
+ case MODE_BLOCK: send("\n"); break;
+ case MODE_SCRIPT: send("\n"); break;
+ case MODE_OUTTEXT: send("\n"); break;
+ case MODE_ANSWER: send("\n"); break;
+ case MODE_NONE: break;
+ }
+ mode=MODE_NONE;
}
-void remove_delayed()
+void start_mode(int newmode,char* args)
{
- if (delayed) {free(delayed);delayed=NULL;}
+ if (newmode == mode) return;
+ end_mode();
+ switch (newmode) {
+ case MODE_COMMENT: send("\n"); break;
+ case MODE_BLOCK: send("\n",args); break;
+ case MODE_SCRIPT: send("