Exemple #1
0
int
	main
	(
	int argc,
	char ** argv
	)
{
	ifstream mf_file("mutual_funds.txt");
	ofstream results("results.txt");
	string mf;
	std::vector<double> rets(8);
	int num = 30;

	std::vector<mystruct0> rets;

	mf = "SPY";
	for (unsigned q = 0; q < 8; ++q) {
		rets[q] = get_return(
			mf,
			MARKET_DATA_WEEKLY,
			NUM_WEEKS_IN_A_QUARTER,
			q*NUM_WEEKS_IN_A_QUARTER);
	}

	mystruct a(mf, rets);

	rets.push_back(a);
	print_rets(mf, rets);

	try {
		while (mf_file.good()) {
			getline(mf_file, mf);
			if (mf.empty()) break;

			for (unsigned q = 0; q < 8; ++q) {
				rets[q] = get_return(
					mf,
					MARKET_DATA_WEEKLY,
					NUM_WEEKS_IN_A_QUARTER,
					q*NUM_WEEKS_IN_A_QUARTER);
			}

			print_rets(mf, rets);

			if (num-- == 0) break;
		}
	}
	catch (exception e) {

	}

	mf_file.close();

	system("pause");
	return 0;
}
Exemple #2
0
void count_cds()
{
	FILE *titles_fp = NULL;
	FILE *tracks_fp = NULL;
	char entry[MAX_ENTRY];
	int titles = 0;
	int tracks = 0;

	titles_fp = fopen(title_file, "r");
	if (titles_fp) {
		while (fgets(entry, MAX_ENTRY, titles_fp))
			titles++;
		fclose(titles_fp);
	}

	tracks_fp = fopen(tracks_file, "r");
	if (tracks_fp) {
		while (fgets(entry, MAX_ENTRY, tracks_fp))
			tracks++;
		fclose(tracks_fp);
	}

	mvprintw(ERROR_LINE, 0, 
		"Database contains %d titles, witch a total of %d tracks.", 
		titles, tracks);

	get_return();
}
void count_cds()
{
    FILE *titles_fp, *tracks_fp;
    char entry[MAX_ENTRY];
    int titles = 0;
    int tracks = 0;

    titles_fp = fopen(TITLE_FILE, "r");
    if (titles_fp) {
        while (fgets(entry, MAX_ENTRY, titles_fp)){
            titles++;
        }
        fclose(titles_fp);
    }

    tracks_fp = fopen(TRACKS_FILE, "r");
    if (tracks_fp) {
        while (fgets(entry, MAX_ENTRY, tracks_fp)){
            tracks++;
        }
        fclose(tracks_fp);
    }

    mvprintw(ERROR_LINE, 0,
             "Database contains %d titles, with a total of %d tracks.",
             titles, tracks);
    get_return();
}
/* Compute a count of the total number of records and tracks in the db */
void count_cds() {
    FILE *titles_fp, *tracks_fp;
    char entry[MAX_ENTRY];
    int ntitles = 0;
    int ntracks = 0;

    // count titles
    titles_fp = fopen(TITLE_FILE, "r");
    if (titles_fp) {   // note that this is expected to fail if no writes yet
        while (fgets(entry, MAX_ENTRY, titles_fp)) {
            ntitles++;
        }
        fclose(titles_fp);
    }

    // count tracks
    tracks_fp = fopen(TRACKS_FILE, "r");
    if (tracks_fp) {
        while (fgets(entry, MAX_ENTRY, tracks_fp)) {
            ntracks++;
        }
        fclose(tracks_fp);
    }

    // print message, and wait for user to press return so they have a chance
    // to read it.
    mvprintw(ERROR_LINE, 0,
             "Database containes %d titles, and a total of %d tracks",
             ntitles, ntracks);
    get_return();
}
/* Find a cd. If it's found, we store it's info in the globals
 * `current_cd` and `current_cat`. If we find 0 or more than 1 matches,
 * we inform the user and null-out `current_cd` 
 * 
 * NOTE: the authors hae typos here, everywhere where they have `found == ...`
 *       they intended to have `found = ...` */
void find_cd() {
    char match[MAX_STRING], entry[MAX_ENTRY];
    FILE *titles_fp;
    int count = 0;
    char *found, *title, *catalog;

    mvprintw(Q_LINE, 0, "Enter a string to search for in CD titles: ");
    get_string(match);

    titles_fp = fopen(TITLE_FILE, "r");
    if (titles_fp) {
        while (fgets(entry, MAX_ENTRY, titles_fp)) {

            // skip over the catalog... the strstr function returns
            //   a pointer to the first match against s2 found in s1, or NULL
            //   if not found.
            catalog = entry;
            if ((found = strstr(catalog, ","))) {
                *found = '\0';  // make `catalog` be '\0'-terminated
                title = found + 1;

                // do another search for a ',', so that we can isolate the
                // title portion of the entry before doing our actual search.
                if ((found = strstr(title, ","))) {
                    *found = '\0'; // make `title` be '\0'-terminated

                    // now that we've isolated the title, we can do our
                    // actual search
                    //   (NOTE: the above code should really be factored out
                    //    since (a) it's general, and (b) it obscures what's
                    //    going on with low-level implementation details)
                    if ((found = strstr(title, match))) {
                        count++;
                        strcpy(current_cd, title);
                        strcpy(current_cat, catalog);
                    }
                }
            }
        }
        fclose(titles_fp);
    }

    // if we did not find exactly one match, print a message and wait for user
    // to press enter.
    if (count != 1) {
        if (count == 0) {
            mvprintw(ERROR_LINE, 0, "Sorry, no matching CD found");
        }
        if (count > 1) {
            mvprintw(ERROR_LINE, 0, "Sorry, match is ambiguous; %d CDs found",
                     count);
        }
        current_cd[0] = '\0';
        get_return();
    }
}
void find_cd()
{
    char match[MAX_STRING], entry[MAX_ENTRY];
    FILE *titles_fp;
    int count = 0;
    char *found, *title, *catalog;

    mvprintw(Q_LINE, 0, "Enter a string to search for in CD titles: ");
    get_string(match);

    titles_fp = fopen(TITLE_FILE, "r");
    if (titles_fp) {
        while (fgets(entry, MAX_ENTRY, titles_fp)) {
            /* Skip past catalog number */
            catalog = entry;
            if (found = strstr(catalog, ",")) {
                *found = '\0';
                title = found + 1;

                /* Zap the next comma in the entry to reduce it to title only */
                if (found = strstr(title, ",")) {
                    *found = '\0';
                    
                    /* Now see if the match substring is present */
                    if (found = strstr(title, match)) {
                        count++;
                        strcpy(current_cd, title);
                        strcpy(current_cat, catalog);
                    }
                }
            }
        }
        fclose(titles_fp);
    }
    if (count != 1) {
        if (count == 0) {
            mvprintw(ERROR_LINE, 0, "Sorry, no matching CD found.");
        }
        if (count > 1) {
            mvprintw(ERROR_LINE, 0, "Sorry, match is ambiguous: %d CDs found.", count);
        }
        current_cd[0] = '\0';
        get_return();
    }
}
Exemple #7
0
void find_cd() {
  char match[MAX_STRING], entry[MAX_ENTRY];
  FILE *titles_fp;
  int count = 0;
  char *found, *title, *catalog;

  mvprintw(Q_LINE, 0, "Enter a string to search for in CD titles: ");
  get_string(match);

  titles_fp = fopen(title_file, "r");
  if (titles_fp) {
    while (fgets(entry, MAX_ENTRY, titles_fp)) {
      catalog = entry;
      if ((found = strstr(catalog, ","))) {
        *found = 0;
        title = found + 1;

        if ((found = strstr(title, ","))) {
          *found = '\0';
          if ((found = strstr(title, match))) {
            count++;
            strcpy(current_cd, title);
            strcpy(current_cat, catalog);
          }
        }
      }
    }
    fclose(titles_fp);
  }

  if (count != 1) {
    if (count == 0)
      mvprintw(ERROR_LINE, 0, "Sorry, no matching CD found.");
    if (count > 1)
      mvprintw(ERROR_LINE, 0, "Sorry, match is ambiguous: %d CDs found. ", count);
    current_cd[0] = '\0';
    get_return();
  }
}
Exemple #8
0
static int add_current_alias(void)
{
/*
 *	Alias the current message to the specified name and
 *	add it to the alias text file, for processing as
 *	the user leaves the program.
 *
 *	Returns non-zero iff alias actually added to file.
 */

	char aliasname[SLEN], firstname[SLEN], lastname[SLEN];
	char comment[SLEN], address1[LONG_STRING], buffer[SLEN];
	char comment_buff[LONG_STRING];
	char *chspace, *bufptr;
	struct header_rec *current_header;

	static char bad_punc[] = ",.:;";
	char *punc_ptr;
	int i, match;
	int replace, to_replace;

	if (curr_folder.curr_mssg == 0) {
	 dprint(4, (debugfile,
		"Add current alias called without any current message!\n"));
	 show_error(catgets(elm_msg_cat, AliasesSet, AliasesNoMessage,
		"No message to alias to!"));
	 return(0);
	}
	current_header = curr_folder.headers[curr_folder.curr_mssg-1];

	strcpy(buffer, catgets(elm_msg_cat, AliasesSet, AliasesCurrentMessage,
		"Current message address aliased to: "));
	PutLine(LINES-2,0, buffer);
	CleartoEOLN();
	*aliasname = '\0';
	if ((replace = get_aliasname(aliasname, buffer, &to_replace)) < 0) {
	    dprint(3, (debugfile,
	        "Aliasname [%s] was rejected in add_current_alias\n",
	        aliasname));
	    ClearLine(LINES-2);
	    return(0);
	}

	/* use full name in current message for default comment */
	tail_of(current_header->from, comment_buff, current_header->to);
	if(strchr(comment_buff, (int)'!') || strchr(comment_buff, (int)'@'))
	  /* never mind - it's an address not a full name */
	  *comment_buff = '\0';

/*
 *	Try to break up the From: comment into firstname, lastname, and
 *	any other text.  This is based on the fact that many address
 *	comments are pretty straightforward.  This will break on many
 *	situations.  Should handle:
 *		(Robert Howard)
 *		(Robert L. Howard)
 *		(Robert Howard, Georgia Tech)
 *	pretty well.  Will break on:
 *		(The Voice of Reason)
 *		and others....
 */

	*firstname = '\0';
	*lastname = '\0';
	*comment = '\0';
	if (strlen(comment_buff) != 0) {	/* There is something. */
	    bufptr = comment_buff;
	    while (*bufptr == ' ') bufptr++;	/* Always strip leading WS */
	    if ((chspace = strchr(bufptr, ' ')) != NULL) {
	   /*
	    *   A space means that there is at least (firstname lastname)
	    *   Get firstname and move bufptr.
	    */
	        *chspace = '\0';
	        strcpy(firstname, bufptr);
	        bufptr = chspace + 1;			/* Move the pointer */
	        while (*bufptr == ' ') bufptr++;
	    }

above:	    if ((chspace = strchr(bufptr, ' ')) != NULL) {
	   /*
	    *   Another space means a third+ word.  We either have:
	    *       1. Word 3+ is a comment, or
	    *       2. Word 2 is a middle initial (word 3 is lastname).
	    *   Check and see.
	    */
	        *chspace = '\0';
	        if ((strlen(bufptr) == 1) ||
	            (strlen(bufptr) == 2  && *(bufptr+1) == '.')) {
	       /*
	        *   If the second word is either a single
	        *   character or a character followed by '.' it was
	        *   probably a middle initial.  Add it to firstname
	        *   and shift.
	        */
	            strcat(firstname, " ");
	            strcat(firstname, bufptr);
	            bufptr = chspace + 1;		/* Move the pointer */
	            while (*bufptr == ' ') bufptr++;
	            goto above;
	        }
	        strcpy(lastname, bufptr);
	        bufptr = chspace + 1;			/* Move the pointer */
	        while (*bufptr == ' ') bufptr++;
	        strcpy(comment, bufptr);
	    }
	    else {
	   /*
	    *   Only a lastname left.
	    */
	        strcpy(lastname, bufptr);
	    }

	/*
	 *  Finally, get any puctuation characters off the end of
	 *  lastname.
	 */
	    match = TRUE;
	    for (i = strlen(lastname) - 1; match && i>0; i--) {
	        match = FALSE;
	        for (punc_ptr = bad_punc; *punc_ptr != '\0'; punc_ptr++) {
	            if (lastname[i] == *punc_ptr) {
	                lastname[i] = '\0';
	                match = TRUE;
	                break;
	            }
	        }
	    }
	}

	get_realnames(aliasname, firstname, lastname, comment, buffer);

	/* grab the return address of this message */
	get_return(address1, curr_folder.curr_mssg-1);
	strcpy(address1, strip_parens(address1));	/* remove parens! */

	return(ask_accept(aliasname, firstname, lastname, comment, address1,
	        buffer, replace, to_replace));

}
Exemple #9
0
void list_tracks() {
  FILE *tracks_fp;
  char entry[MAX_ENTRY];
  int cat_length;
  int lines_op = 0;
  WINDOW *track_pad_ptr;
  int tracks;
  int key;
  int first_line = 0;

  if (current_cd[0] == '\0') {
    mvprintw(ERROR_LINE, 0, "You must select a CD first. ", stdout);
    get_return();
    return;
  }
  clear_all_screen();
  cat_length = strlen(current_cat);

  tracks_fp = fopen(tracks_file, "r");
  if (!tracks_fp)
    return;
  while (fgets(entry, MAX_ENTRY, tracks_fp)) {
    if (strncmp(current_cat, entry, cat_length) == 0)
      tracks++;
  }
  fclose(tracks_fp);

  track_pad_ptr = newpad(tracks + 1 + BOXED_LINES, BOXED_ROWS + 1);
  if (!track_pad_ptr)
    return;

  tracks_fp = fopen(tracks_file, "r");
  if (!tracks_fp)
    return;

  mvprintw(4, 0, "CD Track Listing\n");

  while (fgets(entry, MAX_ENTRY, tracks_fp)) {
    if (strncmp(current_cat, entry, cat_length) == 0) {
      mvwprintw(track_pad_ptr, lines_op++, 0, "%s", entry + cat_length + 1);
    }
  }
  fclose(tracks_fp);

  if (lines_op > BOXED_LINES) {
    mvprintw(MESSAGE_LINE, 0, "Cursor keys to scroll, RETURN or q to exit");
  } else {
    mvprintw(MESSAGE_LINE, 0, "RETURN or q to exit");
  }
  wrefresh(stdscr);
  keypad(stdscr, TRUE);
  cbreak();
  noecho();

  key = 0;
  while (key != 'q' && key != KEY_ENTER && key != '\n') {
    if (key == KEY_UP) {
      if (first_line > 0)
        first_line--;
    }
    if (key == KEY_DOWN) {
      if (first_line + BOXED_LINES + 1 < tracks)
        first_line++;
    }
    prefresh(track_pad_ptr, first_line, 0, BOX_LINE_POS, BOX_ROW_POS,
        BOX_LINE_POS + BOXED_LINES, BOX_ROW_POS + BOXED_ROWS);
    key = getch();
  }

  delwin(track_pad_ptr);
  keypad(stdscr, FALSE);
  nocbreak();
  echo();
}
Exemple #10
0
/*
   list_tracks - list the tracks for the current CD
 */
void list_tracks()
{
    FILE *tracks_fp;
    char entry[MAX_ENTRY];
    int cat_length;
    int lines_op = 0;
    WINDOW *track_pad_ptr;
    int tracks = 0;
    int key;
    int first_line = 0;

    if (current_cd[0] == '\0') {
	mvprintw(ERROR_LINE, 0, "You must select a CD first. ", stdout);
	get_return();
	return;
    }
    clear_all_screen();
    cat_length = strlen(current_cat);

    /* First count the number of tracks for the current CD */
    tracks_fp = fopen(tracks_file, "r");
    if (!tracks_fp)
	return;
    while (fgets(entry, MAX_ENTRY, tracks_fp)) {
	if (strncmp(current_cat, entry, cat_length) == 0)
	    tracks++;
    }
    fclose(tracks_fp);


    /* Make a new pad, ensure that even if there is only a single
       track the PAD is large enough so the later prefresh() is always
       valid.
     */
    track_pad_ptr = newpad(tracks + 1 + BOXED_LINES, BOXED_ROWS + 1);
    if (!track_pad_ptr)
	return;

    tracks_fp = fopen(tracks_file, "r");
    if (!tracks_fp)
	return;

    mvprintw(4, 0, "CD Track Listing\n");

    /* write the track information into the pad */
    while (fgets(entry, MAX_ENTRY, tracks_fp)) {
	/* Compare catalog number and output rest of entry */
	if (strncmp(current_cat, entry, cat_length) == 0) {
	    mvwprintw(track_pad_ptr, lines_op++, 0, "%s", entry + cat_length + 1);
	}
    }
    fclose(tracks_fp);

    if (lines_op > BOXED_LINES) {
	mvprintw(MESSAGE_LINE, 0, "Cursor keys to scroll, RETURN or q to exit");
    } else {
	mvprintw(MESSAGE_LINE, 0, "RETURN or q to exit");
    }
    wrefresh(stdscr);
    keypad(stdscr, TRUE);
    cbreak();
    noecho();

    key = 0;
    while (key != 'q' && key != KEY_ENTER && key != '\n') {
	if (key == KEY_UP) {
	    if (first_line > 0)
		first_line--;
	}
	if (key == KEY_DOWN) {
	    if (first_line + BOXED_LINES + 1 < tracks)
		first_line++;
	}
	/* now draw the appropriate part of the pad on the screen */
	prefresh(track_pad_ptr, first_line, 0,
		 BOX_LINE_POS, BOX_ROW_POS,
		 BOX_LINE_POS + BOXED_LINES, BOX_ROW_POS + BOXED_ROWS);
/*	wrefresh(stdscr); */
	key = getch();
    }

    delwin(track_pad_ptr);
    keypad(stdscr, FALSE);
    nocbreak();
    echo();
}
Exemple #11
0
 //execute context
 l_thread::type_return l_thread::execute(l_call_context&  context)
 {
     //lock context
     context.lock();
     //save context
     register3(R_CONTEXT)      = l_variable( &context );
     //pc...
     unsigned int     pc       = 0;
     l_function&      function = m_vm->function(context.get_fun_id());
     l_list_command&  commands = function.m_commands;
     //macro
     #define raise(str)\
     {\
         push_error(str, pc, (unsigned int)commands[pc].m_line);\
         return T_RETURN_ERROR;\
     }
     #define vconst(c)  function.m_costants[c]
     #define top_size   (m_top+1)
     //for all commands
     for (pc = 0; pc < commands.size(); ++pc)
     {
         //current command
         l_command& cmp = commands[pc];
         //opcodes
         switch (cmp.m_op_code)
         {
             case L_JMP: pc = cmp.m_arg - 1; break;
             case L_RETURN:
                 //push return
                 if(cmp.m_arg)
                 {
                     //get value
                     register3(2) = pop();
                     //return...
                     return T_RETURN_VALUE;
                 }
                 else
                 {
                     //came back
                     return T_RETURN_VOID;
                 }
                 //..
             break;
                 
             case L_IF:
                 if(top().is_true())
                 {
                     pc = cmp.m_arg - 1;
                 }
                 pop();
             break;
                 
             case L_IF0:
                 if(top().is_false())
                 {
                     pc = cmp.m_arg - 1;
                 }
                 pop();
             break;
                 
             case L_IF_OR_POP:
                 if(top().is_true())
                 {
                     pc = cmp.m_arg - 1;
                 }
                 else
                 {
                     pop();
                 }
             break;
                 
             case L_IF0_OR_POP:
                 if(top().is_false())
                 {
                     pc = cmp.m_arg - 1;
                 }
                 else
                 {
                     pop();
                 }
             break;
                 
             case L_PUSH:          push( stack(cmp.m_arg) );  break;
             case L_PUSH_NULL:     push( l_variable() );      break;
             case L_PUSH_TRUE:     push( l_variable(true) );  break;
             case L_PUSH_FALSE:    push( l_variable(false) ); break;
             case L_PUSHK:         push( vconst(cmp.m_arg) ); break;
             case L_CLOSER:
             {
                 //get value
                 l_variable& call_fun = vconst(cmp.m_arg);
                 //...
                 if(call_fun.is_function())
                 {
                     
                     //new context
                     register3(0) = l_closer::gc_new(get_gc());
                     //init context
                     register3(0).to<l_closer>()->init(call_fun.m_value.m_fid, this, l_variable(&context));
                     //push context
                     push(register3(0));
                 }
             }
             break;
                 ////////////////////////////////////////////////////////////
             case L_ADD:
                 if(!stack(1).add(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
                 
             case L_MUL:
                 if(!stack(1).mul(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
                 
             case L_SUB:
                 if(!stack(1).sub(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
                 
             case L_DIV:
                 if(!stack(1).div(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
                 
             case L_MOD:
                 if(!stack(1).mod(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
                 
             case L_UNM:
                 if(!stack(0).unm(stack(0))) raise("not valid operation");
             break;
             ////////////////////////////////////////////////////////////
             case L_EQ:
                 if(!stack(1).equal(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
                 
             case L_NEQ:
                 if(!stack(1).not_equal(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
                 
             case L_RT:
                 if(!stack(1).rt(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
                 
             case L_RE:
                 if(!stack(1).re(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
                 
             case L_LT:
                 if(!stack(1).lt(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
                 
             case L_LE:
                 if(!stack(1).le(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
             ////////////////////////////////////////////////////////////
             case L_NOT:
                 if(!stack(0).not_value(stack(0))) raise("not valid operation");
             break;
                 
             case L_OR:
                 if(!stack(1).or_value(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
                 
             case L_AND:
                 if(!stack(1).and_value(stack(0),stack(1))) raise("not valid operation");
                 pop();
             break;
             ////////////////////////////////////////////////////////////
             case L_GET_GLOBAL:
                 push( global(context,cmp.m_arg) );
             break;
                 
             case L_SET_GLOBAL:
                 global(context,cmp.m_arg) = pop();
             break;
             ////////////////////////////////////////////////////////////
             case L_GET_LOCAL:
                 push( strick_local(context,cmp.m_arg) );
             break;
                 
             case L_SET_LOCAL:
                 strick_local(context,cmp.m_arg) = pop();
             break;
             ////////////////////////////////////////////////////////////
             case L_SET_UP_VALUE:
                 local(context,cmp.m_arg) = pop();
             break;
                 
             case L_GET_UP_VALUE:
                 push( local(context,cmp.m_arg) );
             break;
             ////////////////////////////////////////////////////////////
             case L_GET_THIS:
                 push(context.this_field());
             break;
             case L_SET_THIS:
                 context.this_field() = pop();
             break;
             case L_SET_THIS_NPOP:
                 context.this_field() = stack(cmp.m_arg);
             break;
             ////////////////////////////////////////////////////////////
             case L_NEW_ARRAY:
             {
                 register3(0) = l_array::gc_new(get_gc());
                 //init ?
                 if( cmp.m_arg > 0 )
                 {
                     //types
                     l_array* vector = register3(0).array();
                     //put stack into vector
                     for(int i = cmp.m_arg-1; i >= 0; --i)
                     {
                         vector->operator[](i) = pop();
                     }
                 }
                 //push array (n.b. gc run...)
                 push( register3(0) );
             }
             break;
             //alloc tablet
             case L_NEW_TABLE:
             {
                 register3(0) = l_table::gc_new(get_gc());
                 //init ?
                 if( cmp.m_arg > 0 )
                 {
                     //types
                     l_table* table = register3(0).table();
                     //assert
                     assert(!(cmp.m_arg % 2));
                     //put stack into vector
                     for(int i = (cmp.m_arg)-1; i >= 0; i-=2)
                     {
                         //push key and value
                         table->operator[](stack(1)) = stack(0);
                         //pop value
                         pop();
                         //pop key
                         pop();
                     }
                 }
                 //push table (n.b. gc run...)
                 push( register3(0) );
             }
             break;
             case L_GET_AT_VAL:
             {
                       l_variable& r_b = stack(1);
                 const l_variable& r_c = stack(0);
                 //try
                 if ( r_b.is_object() )
                 {
                     //is a vector
                     if(r_b.is_array())
                     {
                         //types
                         l_array* vector = r_b.array();
                         //to size int
                         size_t index = 0;
                         //cast
                         if( r_c.is_int() )  index= (size_t)r_c.m_value.m_i;
                         else if( r_c.is_float() )index= (size_t)r_c.m_value.m_f;
                         else raise( "value isn't a valid key" );
                         //save last
                         get_this() = stack(1);
                         //get
                         stack(1)   = vector->operator[](index) ;
                     }
                     else if(r_b.is_table())
                     {
                         //types
                         l_table* table =  r_b.table();
                         //is a string?
                         if(!r_c.is_string()) raise( "value isn't a valid key" );
                         //save last
                         get_this() = stack(1);
                         //get and pop value
                         stack(1) = table->operator[](r_c);
                     }
                     else
                     {
                         raise( "value isn't a vector/table, field not available" );
                     }
                 }
                 else
                 {
                     raise( "value isn't a vector/table/object, field not avaliable" );
                 }
                 //pop index
                 pop();
             }
             break;
             case L_SET_AT_VAL:
             {
                 //get table/array
                       l_variable& r_a = stack(2);
                 //get index
                 const l_variable& r_b = stack(1);
                 //try
                 if ( r_a.is_object() )
                 {
                     //is a vector
                     if(r_a.is_array())
                     {
                         //types
                         l_array* vector = r_a.array();
                         //to size int
                         size_t index = 0;
                         //cast
                              if( r_b.is_int()   ) index= (size_t)r_b.m_value.m_i;
                         else if( r_b.is_float() ) index= (size_t)r_b.m_value.m_f;
                         else raise( "value isn't a valid key" );
                         //get and pop value
                         vector->operator[](index) = pop();
                     }
                     else if(r_a.is_table())
                     {
                         //types
                         l_table* table =  r_a.table();
                         //is a string?
                         if(!r_b.is_string()) raise( "value isn't a valid key" );
                         //get and pop value
                         table->operator[](r_b) = pop();
                     }
                     else
                     {
                         raise( "value isn't a vector/table, field not available" );
                     }
                 }
                 else
                 {
                     //pop value
                     pop();
                 }
                 //pop index
                 pop();
                 //pop array/tablet
                 pop();
             }
             break;
             //for in
             case L_IT:
             {
                 //get index
                 l_variable& r_a = top();
                 //..
                 //try
                 if ( r_a.is_object() )
                 {
                     //get object
                     l_obj* this_obj = (l_obj*)r_a.m_value.m_pobj;
                     //is a vector
                     if(r_a.is_array())
                     {
                         //types
                         l_array* vector = r_a.array();
                         //pop value
                         pop();
                         //push it
                         push( vector->get_it() );
                     }
                     else if (r_a.is_table())
                     {
                         //types
                         l_table* table = r_a.table();
                         //pop value
                         pop();
                         //push it
                         push( table->get_it() );
                     }
                     else if (r_a.to<l_xrange>())
                     {
                         //types
                         l_xrange* xrange = static_cast< l_xrange* > ( this_obj );
                         //pop value
                         pop();
                         //push it
                         push( xrange->get_it() );
                     }
                     else
                     {
                         raise( "this value not have a iterator" );
                     }
                 }
                 else
                 {
                     //pop value
                     pop();
                     //..
                     raise( "value isn't a table/array/object, iterator not supported" );
                 }
             }
             break;
             //for of
             case L_FOR_IN:
             case L_FOR_OF:
             {
                 //get index
                 l_variable& r_it = top();
                 //try
                 if ( r_it.is_iterator() )
                 {
                     //types
                     l_iterator* l_it  = r_it.iterator();
                     //is array it
                     if(l_it)
                     {
                         //else assert
                         assert(l_it);
                         //push it
                         if(l_it->valid())
                         {
                             if(cmp.m_op_code == L_FOR_OF)
                             {
                                 //get next
                                 push( l_it->get() );
                             }
                             else //L_FOR_IN
                             {
                                 //get next
                                 push( l_it->get_id() );
                             }
                             //next
                             l_it->self_next();
                         }
                         else
                         {
                             //pop iterator
                             pop();
                             //and jump
                             pc = cmp.m_arg - 1;
                         }
                     }
                     else
                     {
                         raise( "value isn't a valid iterator" );
                     }
                 }
                 else
                 {
                     //pop it
                     pop();
                     //and jump
                     pc = cmp.m_arg - 1;
                     //...
                     raise( "value isn't an iterator" );
                 }
             }
             break;
             case L_THIS_CALL:
             case L_CALL:
             {
                 //get index
                 register3(0) = pop();
                 //get args
                 if( register3(0).is_cfunction() )
                 {
                     //return size
                     int n_return = register3(0).m_value.m_pcfun(this,cmp.m_arg);
                     //assert (1 return)
                     assert(n_return <= 1);
                     //error
                     if(n_return<0)
                     {
                         raise( "native call exception" );
                     }
                     else if(n_return>1)
                     {
                         raise( "native call can't return more than a value" );
                     }
                     //pop args
                     for(int i=0; i < cmp.m_arg; ++i)
                     {
                         pop();
                     }
                     //if return
                     if(n_return) push(get_return());
                 }
                 else if( register3(0).is_closer() )
                 {
                     //get context
                     l_closer* closer = register3(0).to<l_closer>();
                     //else assert
                     if(!closer){ assert(0); };
                     //new function
                     l_function& call_fun    = m_vm->function(closer->get_fun_id());
                     //save last context
                     l_variable last_ctx     = register3(R_CONTEXT);
                     //new context
                     register3(1)            = l_call_context::gc_new(get_gc());
                     l_call_context* new_ctx = register3(1).to<l_call_context>();
                     //this?
                     if(cmp.m_op_code == L_THIS_CALL)
                     {
                         new_ctx->this_field() = get_this();
                     }
                     //init
                     new_ctx->init(*closer);
                     //lock context
                     new_ctx->lock();
                     //n args
                     unsigned int n_fun_args = call_fun.m_args_size;
                     //alloc array args..?
                     if (call_fun.m_have_args_list)
                     {
                         //alloc array
                         register3(3) = l_array::gc_new(get_gc());
                     }
                     //put arguments
                     for(unsigned int
                         arg  = 0;
                         arg != cmp.m_arg;
                       ++arg)
                     {
                         if (arg < n_fun_args)
                         {
                             new_ctx->variable( call_fun.constant(arg) ) = pop();
                         }
                         else if (call_fun.m_have_args_list)
                         {
                             register3(3).array()->push(pop());
                         }
                         else
                         {
                             pop();
                         }
                     }
                     //add var list
                     if (call_fun.m_have_args_list)
                     {
                         //push array
                         new_ctx->variable( call_fun.constant(n_fun_args) ) = register3(3);
                         //to null
                         register3(3) = l_variable();
                     }
                     //save stack
                     long stack_top_bf_call = m_top;
                     //execute call
                     type_return n_return = execute(*new_ctx);
                     //error?
                     if(n_return==T_RETURN_ERROR)
                     {
                         raise( "call exception" );
                     }
                     //unlock context
                     new_ctx->unlock();
                     //reset last context
                     register3(R_CONTEXT) = last_ctx;
                     //restore stack
                     m_top = stack_top_bf_call;
                     //return?
                     if(n_return == T_RETURN_VALUE)
                     {
                         push(register3(2));
                     }
                 }
                 else
                 {
                     raise( "value isn't an function" );
                 }
                 //dealloc
                 if(cmp.m_op_code == L_THIS_CALL)
                 {
                     get_this() = l_variable();
                 }
             }
             break;
                 
             default:  break;
         }
     }
     
     return T_RETURN_VOID;
 }
/* This function can only be called if a cd has already been selected and
 * stored in the globals `current_cd` and `current_cat`. */
void list_tracks() {
    FILE *tracks_fp;
    char entry[MAX_ENTRY];
    int cat_length;
    int lines_op = 0;
    WINDOW *track_pad;
    int ntracks = 0;
    int key;
    int first_line = 0;

    // handle the case of no current cd
    if (current_cd[0] == '\0') {
        mvprintw(ERROR_LINE, 0, "You must select a CD before listing tracks");
        get_return();
        return;
    }

    clear_all_screen();

    // First, count the number of tracks for the current CD. close the
    // file when done so we can reopen before listing them.
    cat_length = strlen(current_cat);
    tracks_fp = fopen(TRACKS_FILE, "r");
    if (!tracks_fp) return;
    while (fgets(entry, MAX_ENTRY, tracks_fp)) {
        if (strncmp(current_cat, entry, cat_length) == 0) {
            ntracks++;
        }
    }
    fclose(tracks_fp);

    // make a new pad. Make sure it is big enough to fill up the boxed region
    // even if there are no listings.
    //   NOTE: we don't actually make a box for this operation... the box is
    //   only used in update_cd. But we are reusing the same region.
    track_pad = newpad(ntracks + 1 + BOXED_LINES, BOXED_COLS + 1);
    if (!track_pad) return;

    // reopen the tracks file
    tracks_fp = fopen(TRACKS_FILE, "r");
    if (!tracks_fp) return;

    // print out all the tracks onto the pad.
    mvprintw(4, 0, "CD Track Listing\n");
    while (fgets(entry, MAX_ENTRY, tracks_fp)) {
        if (strncmp(current_cat, entry, cat_length) == 0) {
            // print the track info. Use `cat_length+1` bc of the comma
            //    this actually is kind of strange because we are still
            //    printing a kind of ugly comma-separated track number and
            //    name, but it works. You could make it better as an exercise.
            mvwprintw(track_pad, lines_op++, 0, "%s", entry + cat_length + 1);
        }
    }
    fclose(tracks_fp);

    // tell the user what to do
    if (lines_op > BOXED_LINES) {
        mvprintw(MESSAGE_LINE, 0, "Arrows to scroll, RETURN or q to exit.");
    } else {
        mvprintw(MESSAGE_LINE, 0, "RETURN or q to exit.");
    }

    // set up the control keys and such, refresh the main screen
    wrefresh(stdscr);
    keypad(stdscr, TRUE);
    cbreak();
    noecho();

    // start up a loop over control keys; inside the loop refresh the pad
    key = 0;
    while (key != '\n' && key != KEY_ENTER && key != 'q') {
        if (key == KEY_UP) {
            if (first_line > 0) {
                first_line--;
            }
        } else if (key == KEY_DOWN) {
            if (first_line + BOXED_LINES < ntracks - 1) {
                first_line++;
            }
        }

        prefresh(track_pad, first_line, 0,
                 BOX_LINE_POS, BOX_COL_POS,
                 BOX_LINE_POS + BOXED_LINES, BOX_COL_POS + BOXED_COLS);
        key = getch();
    }
}
Exemple #13
0
void
main_loop(void)
{
        int c;
        int in_defun=0;
        char *p;

        lineno = 1;

        reset();
        put_lineno();
LOOP:
        c = jump_to_at();
        if (c == ')') {
                if (!in_defun)
                        error("unmatched @) found");
                in_defun = 0;
                putc('}',out);
                reset();
                goto LOOP;
        } else if (c == '\'') {
                char *p;
                poolp = pool;
                p = read_symbol(0);
                pushc('\0');
                fprintf(out,"%s",p);
                goto LOOP;
        }  else if (c == '[') {
                char *p;
                poolp = pool;
                p = read_symbol(1);
                pushc('\0');
                fprintf(out,"%s",p);
                goto LOOP;
        } else if (c != '(') {
                char *p;
                unreadc(c);
                poolp = pool;
                poolp = p = read_function();
                fprintf(out,"%s",translate_function(poolp));
                goto LOOP;
        }
        p = read_token();
        if (strcmp(p, "defun") == 0) {
                if (in_defun)
                        error("@) expected before new function definition");
                in_defun = 1;
                get_function();
                get_lambda_list();
                put_fhead();
                put_lineno();
                c = jump_to_at();
                put_declaration();
                put_lineno();
        } else if (strcmp(p, "return") == 0) {
                tab_save = tab;
                get_return();
                put_return();
        } else
                error_symbol(p);
        goto LOOP;
}