Exemplo n.º 1
0
Arquivo: main.c Projeto: nareto/ADS
void read_file(FILE * inputfile){
  char * line;
  int block_ended = 1, line_count = 0, total_lines = 0, i = 1, j;
  float next_perc;
  unsigned int next_article_id = 0, next_author_id = 0;
  article * temp_article = NULL;
  author * temp_author;
  list_node * temp_author_node;
  graph_node * temp_gnode = NULL;

  line = (char *) malloc(MAX_LINE_LENGTH*sizeof(char));
  while(fgets(line, MAX_LINE_LENGTH, inputfile) != NULL)
    ++total_lines;

  rewind(inputfile);
  line = fgets(line, MAX_LINE_LENGTH, inputfile);
  ++line_count;
  while(line_is_blank(line)){ /*remove leading white lines*/
    line = fgets(line, MAX_LINE_LENGTH, inputfile);
    ++line_count;
  }
  printf("Creating article graph: ");
  fflush(stdout);
  while(line != NULL){
    next_perc = (float) line_count / (float) total_lines;
    if(next_perc  > ((float) i /10.0)){
      printf("%d%%...",10*i);
      fflush(stdout);
      ++i;
    }
    if(block_ended && !line_is_blank(line)){ /*a new article/authors block begins*/
      remove_ending_newline(line);
      block_ended = 0;
      temp_article = new_article(line, next_article_id);
      temp_gnode = new_graph_node(temp_article, article_node);
      ++next_article_id;
    }
    else {
      if(line_is_blank(line)){ /*we have just terminated processing an article/authors block */
	if(!block_ended){
	  block_ended = 1;
	  add_node_to_graph(temp_gnode, artcl_graph);
	}
      }
      else{ /*there's a new author for the current article/authors block*/
	remove_ending_newline(line);
	if((temp_author_node = search_in_hash(line, authors_dict)) == NULL){
	  temp_author = new_author(line, next_author_id);
	  ++next_author_id;
	  insert_in_hash(temp_author, author_node, authors_dict);
	}
	else{
	  temp_author = (author *) temp_author_node->key;
	/*properly update the article's edges in the graph*/
	  for(j=0;j<temp_author->n_articles; ++j){
	    if(temp_author->articles_id[j] < artcl_graph->n_nodes){ /*if there is the same author repeated twice we would be asking for an article id not yet in the graph*/
	      add_edge(temp_gnode, artcl_graph->nodes[temp_author->articles_id[j]]); /*add edge or increase its weight*/
	    }
	  }
	}
	add_article_to_author(temp_article, temp_author);
	add_author_to_article(temp_author, temp_article);
      }
    }
    line = fgets(line, MAX_LINE_LENGTH, inputfile);  
    ++line_count;
  }
  free(line);
}
Exemplo n.º 2
0
/**
 * @brief Adds an article.
 * @usage news.add(faction,title,body,[date_to_rm, [date]])
 *
 * @usage s = news.add( "Empire", "Hello world!", "The Empire wishes to say hello!", 0 ) -- Adds an Empire specific article, with date 0.
 *
 *    @luaparam faction faction of the article, "Generic" for non-factional
 *    @luaparam title Title of the article
 *    @luaparam content What's in the article
 *    @luaparam date_to_rm date to remove the article
 *    @luaparam date What time to put, defaults to current date, use 0 to not use a date
 *    @luareturn The article matching name or nil if error.
 * @luafunc add( s )
 */
int newsL_add( lua_State *L )
{
   news_t *n_article;
   char *title, *content, *faction;
   ntime_t date, date_to_rm;

   title   = NULL;
   content = NULL;
   faction = NULL;

   date = ntime_get();
   date_to_rm = 50000000000000;

   /* If a table is passed in. ugly hack */
   if (lua_istable(L, 1)) {
      lua_pushnil(L);

      /* traverse table */
      while (lua_next(L, -2)) {
         /* traverse sub table */
         if (lua_istable(L, -1)) {
            lua_pushnil(L);
            while (lua_next(L, -2)) {
               if (lua_isnumber(L, -1)) {
                  if (date_to_rm)
                     date_to_rm = lua_tonumber(L, -1);
                  else
                     date = lua_tonumber(L, -1);
               }
               else if (lua_istime(L, -1)) {
                  if (date_to_rm)
                     date_to_rm = luaL_validtime(L, -1);
                  else
                     date = luaL_validtime(L, -1);
               }
               else if (lua_isstring(L, -1)) {
                  if (!faction)
                     faction = strdup(lua_tostring(L, -1));
                  else if (!title)
                     title = strdup(lua_tostring(L, -1));
                  else if (!content)
                     content = strdup(lua_tostring(L, -1));
               }

               lua_pop(L, 1);
            }

            if (title && content && faction)
               new_article(title, content, faction, date, date_to_rm);
            else
               WARN("Bad arguments");

            free(faction);
            free(title);
            free(content);
            faction = NULL;
            title = NULL;
            content = NULL;

            date = ntime_get();
            date_to_rm = 50000000000000;
         }

         lua_pop(L, 1);
      }

      lua_pop(L, 1);

      /* If we're landed, we should regenerate the news buffer. */
      if (landed) {
         generate_news(faction_name(land_planet->faction));
         if (land_loaded)
            bar_regen();
      }

      return 0;
   }

   if (!(lua_isstring(L, 1) && lua_isstring(L, 2) && lua_isstring(L, 3))) {
      WARN("\nBad arguments, use "
           "addArticle(\"Faction\",\"Title\",\"Content\",[date,[date_to_rm]])");
      return 0;
   }

   faction = strdup(lua_tostring(L, 1));
   title = strdup(lua_tostring(L, 2));
   content = strdup(lua_tostring(L, 3));

   /* get date and date to remove, or leave at defaults*/
   if (lua_isnumber(L, 4) || lua_istime(L, 4)) {
      if (lua_istime(L, 4))
         date_to_rm = luaL_validtime(L, 4);
      else
         date_to_rm = lua_tonumber(L, 4);
   }

   if (lua_isnumber(L, 5) || lua_istime(L, 5)) {
      if (lua_istime(L, 5))
         date = luaL_validtime(L, 5);
      else
         date = lua_tonumber(L, 5);
   }

   if (title && content && faction)
      n_article = new_article(title, content, faction, date, date_to_rm);
   else
      WARN("Bad arguments");

   lua_pusharticle(L, n_article->id);

   free(title);
   free(content);
   free(faction);

   /* If we're landed, we should regenerate the news buffer. */
   if (landed) {
      generate_news(faction_name(land_planet->faction));
      if (land_loaded)
         bar_regen();
   }

   return 1;
}