Diff for /loncom/homework/CAPA-converter/capaCommon.c between versions 1.1 and 1.10

version 1.1, 2000/04/12 20:06:16 version 1.10, 2001/12/04 15:17:57
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 111  capa_parse(set,problem,filename,num_ques Line 138  capa_parse(set,problem,filename,num_ques
 int  set;Problem_t **problem;char *filename;int  *num_questions;  int  set;Problem_t **problem;char *filename;int  *num_questions;
 void (*func_ptr)();  void (*func_ptr)();
 {  {
   int   errcode;    int   errcode,temp;
 extern  FILE      *Input_stream[MAX_OPENED_FILE];  extern  FILE      *Input_stream[MAX_OPENED_FILE];
 extern  char       Opened_filename[MAX_OPENED_FILE][QUARTER_K];  extern  char       Opened_filename[MAX_OPENED_FILE][QUARTER_K];
 extern  int        Lexi_line;  extern  int        Lexi_line;
Line 152  extern  void       (*Status_Func)(); Line 179  extern  void       (*Status_Func)();
   /*yyrestart(yyin);*/    /*yyrestart(yyin);*/
   begin_text();    begin_text();
   /*if ( !yyparse() )  { errcode = Lexi_qnum; } else { errcode = 0; }*/    /*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; }
     fprintf(stderr,"\nExited on: %d\n",temp);
     fprintf(stderr,"Current cache: %d\n",current_cache);
     fprintf(stderr,"Flushing:\n"); 
     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 164  extern  void       (*Status_Func)(); Line 195  extern  void       (*Status_Func)();
   return (errcode);    return (errcode);
 }  }
   
 void send(char *text)  int dyn_maxlen=1000000;
   int delay;
   void dyn_init(struct dyn_string *dyn)
   {
       dyn->len=0;
       dyn->max=0;
       dyn->str=NULL;
   }
   
   void dyn_free(struct dyn_string* dyn)
   {
       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) {
       char *new;
       int len,result;
   
       if ((result=vasprintf(&new,format,ap))==-1) {
         fprintf(stderr,"vaspintf didn't like :%s:",format);
         exit(1);
       }
       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);
       if (do_cache[current_cache]) {
         append_message(&cached_data[current_cache],format,ap);
       }
   }
   
   void flush_delayed()
   {
     delay=0;
     if (dyn_delayed.str) { send(dyn_delayed.str); }
     dyn_free(&dyn_delayed);dyn_init(&dyn_delayed);
   }
   
   
   void send_to(int which, char *text, va_list ap)
   {
     if (delay) {
       append_message(&dyn_delayed,text,ap);
     } else {
       if (num_streams) {
         if (which == ALL_STREAMS) {
    int i;
    for (i=0;i<num_streams;i++) { append_message(&streams[i],text,ap); }
         } else {
    append_message(&streams[which],text,ap);
         }
       } else {
         vprintf(text,ap);
       }
     }
     if (do_cache[current_cache]) {
       append_message(&cached_data[current_cache],text,ap);
     }
   }
   
   void send(char *text,...)
   {
     va_list ap;
     va_start(ap,text);
     send_to(ALL_STREAMS,text,ap);
   }
   
   void send_stream(int which, char *text,...)
   {
     va_list ap;
     va_start(ap,text);
     send_to(which,text,ap);
   }
   
   int num_streams=0;
   struct dyn_string streams[MAX_STREAMS];
   void start_streams(int num) {
     int i;
     for(i=0; i<num; i++) { dyn_init(&streams[i]); }
     for(i=1; i<num; i++) { mode[i]=mode[0]; }
     num_streams=num;
   }
   
   void end_streams(int which) {
     int i;
     fputs(streams[which].str,stdout);
     for(i=0; i<num_streams; i++) { dyn_free(&streams[which]); }
     num_streams=0;
     mode[0]=mode[which];
   }
   
   int watch_mode[MAX_STREAMS];
   void end_mode()
   {
     end_mode_stream(ALL_STREAMS);
   }
   
   void end_mode_stream(int which)
   {
     if (num_streams) {
       if (which == ALL_STREAMS) {
         int i;
         for (i=0;i<num_streams;i++) { end_mode_stream(i); }
         return;
       }
     } else {
       which=0;/* if streams aren't active make sure which is correct */
     }
     switch (mode[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_HINT: send_stream(which,"<endouttext />\n</hintpart>\n</hintgroup>\n"); break;
     case MODE_IMPORT: send_stream(which,"</import>\n"); break;
     case MODE_NONE: break;
     }
     mode[which]=MODE_NONE;
     watch_mode[which]=0;
   }
   
   void start_mode(int newmode,char* args)
   {
     start_mode_stream(ALL_STREAMS,newmode,args);
   }
   
   void start_mode_stream(int which,int newmode,char* args)
   {
     if (num_streams) {
       if (which == ALL_STREAMS) {
         int i;
         for (i=0;i<num_streams;i++) { start_mode_stream(i,newmode,args); }
         return;
       } else {
         if (newmode == mode[which]) return;
       }
     } else {
       if (newmode == mode[0]) return;
       which=0;/* if streams aren't active make sure which is correct */
     }
     end_mode_stream(which);
     switch (newmode) {
     case MODE_COMMENT: send_stream(which,"<comment>\n"); break;
     case MODE_BLOCK: send_stream(which,"<block %s>\n",args); break;
     case MODE_SCRIPT: send_stream(which,"<script type=\"loncapa/perl\">\n"); break;
     case MODE_OUTTEXT: send_stream(which,"<startouttext />\n"); break;
     case MODE_ANSWER: send_stream(which,"\n"); break;
     case MODE_HINT: send_stream(which,"<hintgroup>\n<hintpart on=\"default\">\n<startouttext />\n"); break;
     case MODE_IMPORT: send_stream(which,"<import>"); break;
     case MODE_NONE: break;
     }
     mode[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() 
 {  {
     printf(text);    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 =====================|<||= */
   

Removed from v.1.1  
changed lines
  Added in v.1.10


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>