void MXPImporter::loadInstructions( QTextStream &stream, Recipe &recipe )
{
	//==========================instructions ( along with other optional fields... mxp format doesn't define end of ingredients and start of other fields )==============//
	stream.skipWhiteSpace();
	QString current = stream.readLine().trimmed();
	while ( !current.contains( "- - - -" ) && !stream.atEnd() ) {
		if ( current.trimmed() == "Source:" ) {
			Element new_author( getNextQuotedString( stream ) );
			recipe.authorList.append( new_author );
			//kDebug()<<"Found source: "<<new_author.name<<" (adding as author)";
		}
		else if ( current.trimmed() == "Description:" ) {
			QString description = getNextQuotedString( stream );
			//kDebug()<<"Found description: "<<m_description<<" (adding to end of instructions)";
			recipe.instructions += "\n\nDescription: " + description;
		}
		else if ( current.trimmed() == "S(Internet Address):" ) {
			QString internet = getNextQuotedString( stream );
			//kDebug()<<"Found internet address: "<<m_internet<<" (adding to end of instructions)";
			recipe.instructions += "\n\nInternet address: " + internet;
		}
		else if ( current.trimmed() == "Yield:" ) {
			recipe.yield.setAmount(getNextQuotedString( stream ).trimmed().toInt());
			recipe.yield.setType(i18n("servings"));
			//kDebug()<<"Found yield: "<<recipe.yield.amount<<" (adding as servings)";
		}
		else if ( current.trimmed() == "T(Cook Time):" ) {
			( void ) getNextQuotedString( stream ); //this would be prep time, but we don't use prep time at the moment
			//kDebug()<<"Found cook time: "<<m_prep_time<<" (adding as prep time)";
		}
		else if ( current.trimmed() == "Cuisine:" ) {
			Element new_cat( getNextQuotedString( stream ) );
			recipe.categoryList.append( new_cat );
			//kDebug()<<"Found cuisine (adding as category): "<<new_cat.name;
		}
		else
			recipe.instructions += current + '\n';

		current = stream.readLine().trimmed();
	}
	recipe.instructions = recipe.instructions.trimmed();
	//kDebug()<<"Found instructions: "<<m_instructions;
}
void MXPImporter::importMXP( QTextStream &stream )
{
	Recipe recipe;

	kapp->processEvents(); //don't want the user to think its frozen... especially for files with thousands of recipes

	//kDebug()<<"Found recipe MXP format: * Exported from MasterCook *";
	QString current;

	// title
	stream.skipWhiteSpace();
	recipe.title = stream.readLine().trimmed();
	//kDebug()<<"Found title: "<<m_title;

	//author
	stream.skipWhiteSpace();
	current = stream.readLine().trimmed();
	if ( current.mid( 0, current.indexOf( ":" ) ).simplified().toLower() == "recipe by" ) {
		Element new_author( current.mid( current.indexOf( ":" ) + 1, current.length() ).trimmed() );
		recipe.authorList.append( new_author );
		//kDebug()<<"Found author: "<<new_author.name;
	}
	else {
		addWarningMsg( i18n( "While loading recipe \"%1\" "
		                              "the field \"Recipe By:\" is either missing or could not be detected.", recipe.title ) );
	}

	//servings
	stream.skipWhiteSpace();
	current = stream.readLine().trimmed();
	if ( current.mid( 0, current.indexOf( ":" ) ).simplified().toLower() == "serving size" ) {
		//allows serving size to be loaded even if preparation time is missing
		int end_index;
		if ( current.contains( "preparation time", Qt::CaseInsensitive ) )
			end_index = current.indexOf( "preparation time", 0, Qt::CaseInsensitive ) - 15;
		else
			end_index = current.length();

		recipe.yield.setAmount(current.mid( current.indexOf( ":" ) + 1, end_index ).trimmed().toInt());
		recipe.yield.setType(i18n("servings"));
		//kDebug()<<"Found serving size: "<<recipe.yield.amount;
	}
	else {
		addWarningMsg( i18n( "While loading recipe \"%1\" "
		                              "the field \"Serving Size:\" is either missing or could not be detected." , recipe.title ) );
	}

	if ( current.contains( "preparation time", Qt::CaseInsensitive ) ) {
		QString prep_time = current.mid( current.indexOf( ":", current.indexOf( "preparation time", 0, Qt::CaseInsensitive ) ) + 1,
		                                 current.length() ).trimmed();
		recipe.prepTime = QTime( prep_time.section( ':', 0, 0 ).toInt(), prep_time.section( ':', 1, 1 ).toInt() );
		kDebug() << "Found preparation time: " << prep_time ;
	}
	else {
		addWarningMsg( i18n( "While loading recipe \"%1\" "
			"the field \"Preparation Time:\" is either missing or could not be detected." , recipe.title ) );
	}

	loadCategories( stream, recipe );
	loadIngredients( stream, recipe );
	loadInstructions( stream, recipe );
	loadOptionalFields( stream, recipe );

	add
		( recipe );

	if ( !stream.atEnd() ) {
		importMXP( stream );
		return ;
	}
}
Beispiel #3
0
Datei: main.c Projekt: 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);
}
Beispiel #4
0
void NYCGenericImporter::importNYCGeneric( QTextStream &stream )
{
	kapp->processEvents(); //don't want the user to think its frozen... especially for files with thousands of recipes

	QString current;

	stream.skipWhiteSpace();

	//title
	while ( !( current = stream.readLine() ).isEmpty() && !stream.atEnd() )
		m_recipe.title = current;

	//categories
	while ( !( current = stream.readLine() ).isEmpty() && !stream.atEnd() ) {
		if ( current[ 0 ].isNumber() ) {
			loadIngredientLine( current );
			break;
		} //oops, this is really an ingredient line (there was no category line)

		QStringList categories = current.split( ',', QString::SkipEmptyParts );

		if ( categories.count() > 0 && categories[ 0 ].toUpper() == "none" )  //there are no categories
			break;

		for ( QStringList::const_iterator it = categories.constBegin(); it != categories.constEnd(); ++it ) {
			Element new_cat( QString( *it ).trimmed() );
			kDebug() << "Found category: " << new_cat.name ;
			m_recipe.categoryList.append( new_cat );
		}
	}

	//ingredients
	while ( !( current = stream.readLine() ).isEmpty() && !stream.atEnd() )
		loadIngredientLine( current );

	//everything else is the instructions with optional "contributor", "prep time" and "yield"
	bool found_next;
	while ( !( found_next = ( current = stream.readLine() ).startsWith( "@@@@@" ) ) && !stream.atEnd() ) {
		if ( current.startsWith( "Contributor:" ) ) {
			Element new_author( current.mid( current.indexOf( ':' ) + 1, current.length() ).trimmed() );
			kDebug() << "Found author: " << new_author.name ;
			m_recipe.authorList.append( new_author );
		}
		else if ( current.startsWith( "Preparation Time:" ) ) {
			m_recipe.prepTime = QTime::fromString( current.mid( current.indexOf( ':' ), current.length() ) );
		}
		else if ( current.startsWith( "Yield:" ) ) {
			int colon_index = current.indexOf( ':' );
			int amount_type_sep_index = current.indexOf(" ",colon_index+1);

			m_recipe.yield.setAmount(current.mid( colon_index+2, amount_type_sep_index-colon_index ).toDouble());
			m_recipe.yield.setType(current.mid( amount_type_sep_index+3, current.length() ));
		}
		else if ( current.startsWith( "NYC Nutrition Analysis (per serving or yield unit):" ) ) {
			//m_recipe.instructions += current + '\n';
		}
		else if ( current.startsWith( "NYC Nutrilink:" ) ) {
			//m_recipe.instructions += current + '\n';
		}
		else if ( !current.trimmed().isEmpty() && !current.startsWith("** Exported from Now You're Cooking!") ) {
			m_recipe.instructions += current + '\n';
		}
	}

	m_recipe.instructions = m_recipe.instructions.trimmed();
	putDataInRecipe();

	if ( found_next )
		importNYCGeneric( stream );
}