2016-05-22 18 views
1

私はXMLとして保存しようとしている構造体には、しかし、それは巣後でタグには思われる大きなファイルの場合には、小さなXMLSの作成に問題がないように思われていますCのlibxml2のてXmlTextWriterタグの巣自体がランダムに

<argument><kind>5</kind><string/><argument><kind>2</kind><string>sprite.gif</string></argument></argument> 

間違ってされており、引数はいけない:しばらくの物事がフォーマットに行くを開始した後、しかし

<argument><kind>2</kind><string>video.avi</string></argument><argument><kind>4</kind><string>0</string></argument> 

:最初の30かそこらのために、私は形式で正しい結果を取得しています入れ子にする。ここで

//Save an obiect as in xml/gm format 
int save_obj(struct gm_object obj, char *file_str) 
{ 
    xmlTextWriterPtr writer; 
    xmlChar *tmp; 
    // Create a new XmlWriter for uri, with no compression. 
    writer = xmlNewTextWriterFilename(file_str, 0); 
    if (writer == NULL) 
    { 
    return 1; //1 = error 
    } 
    xmlTextWriterStartDocument(writer, NULL, "UTF-8", "no"); 
    //Root 
    xmlTextWriterStartElement(writer, BAD_CAST "object"); 
    xmlTextWriterWriteElement(writer, BAD_CAST "spriteName", BAD_CAST obj.spriteName); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "solid","%i",obj.solid); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "visible","%i",obj.visible); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "depth","%i",obj.depth); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "persistent","%i",obj.persistent); 
    xmlTextWriterWriteElement(writer, BAD_CAST "maskName", BAD_CAST obj.maskName); 
    xmlTextWriterWriteElement(writer, BAD_CAST "parentName", BAD_CAST obj.parentName); 
    xmlTextWriterStartElement(writer, BAD_CAST "events"); 
    //This is where we store any ints that need to be converted to strings 
    char str_int_converted[(CHAR_BIT * sizeof(int) - 1)/3 + 2]; 
    int i; 
    for (i = 0; i<obj.event_count-1;i++) 
    { 
    xmlTextWriterStartElement(writer, BAD_CAST "event"); 
    sprintf(str_int_converted, "%d", obj.events[i].enumb); 
    xmlTextWriterWriteAttribute(writer, BAD_CAST "enumb", BAD_CAST str_int_converted); 
    sprintf(str_int_converted, "%d", obj.events[i].eventtype); 
    xmlTextWriterWriteAttribute(writer, BAD_CAST "eventtype", BAD_CAST str_int_converted); 
    int ii; 
    for (ii = 0; ii<obj.events[i].action_count-1;ii++) 
    { 
     xmlTextWriterStartElement(writer, BAD_CAST "action"); 
     xmlTextWriterWriteFormatElement(writer, BAD_CAST "libid", "%i", obj.events[i].actions[ii].libid); 
     xmlTextWriterWriteFormatElement(writer, BAD_CAST "id","%i", obj.events[i].actions[ii].id); 
     xmlTextWriterWriteFormatElement(writer, BAD_CAST "kind","%i", obj.events[i].actions[ii].kind); 
     xmlTextWriterWriteFormatElement(writer, BAD_CAST "userelative","%i", obj.events[i].actions[ii].userelative); 
     xmlTextWriterWriteFormatElement(writer, BAD_CAST "useapplyto", "%i", obj.events[i].actions[ii].useapplyto); 
     xmlTextWriterWriteFormatElement(writer, BAD_CAST "isquestion", "%i", obj.events[i].actions[ii].isquestion); 
     xmlTextWriterWriteFormatElement(writer, BAD_CAST "exetype","%i", obj.events[i].actions[ii].exetype); 
     xmlTextWriterWriteElement(writer, BAD_CAST "functionname", BAD_CAST obj.events[i].actions[ii].functionname); 
     xmlTextWriterWriteElement(writer, BAD_CAST "codestring", BAD_CAST obj.events[i].actions[ii].codestring); 
     xmlTextWriterWriteElement(writer, BAD_CAST "whoName", BAD_CAST obj.events[i].actions[ii].whoName); 
     xmlTextWriterWriteFormatElement(writer, BAD_CAST "relative","%i", obj.events[i].actions[ii].relative); 
     xmlTextWriterWriteFormatElement(writer, BAD_CAST "isnot","%i", obj.events[i].actions[ii].isnot); 
     xmlTextWriterStartElement(writer, BAD_CAST "arguments"); 
     int iii; 
     for (iii = 0; iii<obj.events[i].actions[ii].arg_count-1;iii++) 
     { 
     //causing some of these to nest rather than properly close 
     xmlTextWriterStartElement(writer, BAD_CAST "argument"); 
     xmlTextWriterWriteFormatElement(writer, BAD_CAST "kind", "%i", obj.events[i].actions[ii].arguments[iii].kind); 
     xmlTextWriterWriteElement(writer, BAD_CAST "string", obj.events[i].actions[ii].arguments[iii].string); 
     xmlTextWriterEndElement(writer); //Close <argument> 
     } 
     xmlTextWriterEndElement(writer); //Close <arguments> 
     xmlTextWriterEndElement(writer); //Close <action> 
    } 
    xmlTextWriterEndElement(writer); //Close <event> 
    } 
    xmlTextWriterEndElement(writer); //Close <events> 
    //Put physics stuff here 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObject","%i", obj.PhysicsObject); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectSensor","%i", obj.PhysicsObjectSensor); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectShape","%i", obj.PhysicsObjectShape); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectDensity","%f", obj.PhysicsObjectDensity); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectRestitution","%f", obj.PhysicsObjectRestitution); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectGroup","%i", obj.PhysicsObjectGroup); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectLinearDamping","%f", obj.PhysicsObjectLinearDamping); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectAngularDamping","%f", obj.PhysicsObjectAngularDamping); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectFriction","%f", obj.PhysicsObjectFriction); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectAwake","%i", obj.PhysicsObjectAwake); 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectKinematic","%i", obj.PhysicsObjectKinematic); 
    xmlTextWriterStartElement(writer, BAD_CAST "PhysicsShapePoints"); 
    //This is where we store any floats that need to be converted to strings 
    char str_float_converted[(CHAR_BIT * sizeof(float) - 1)/3 + 2]; 
    for (i=0;i<obj.point_count;i++) 
    { 
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "point","%i,%i", obj.PhysicsShapePoints[i].x, obj.PhysicsShapePoints[i].y); 
    } 
    xmlTextWriterEndElement(writer); //Close <PhysicsShapePoints> 
    xmlTextWriterEndDocument(writer); 
    xmlFreeTextWriter(writer); 
} 

それが生産のフルファイルです。それは、文字の制限を超えて行ってきましたので、私はこの記事のボディにそれを置くことができませんでしたhttp://pastebin.com/raw/iJHYWkWq は申し訳ありません。 私は今、4時間にわたって、このバグで働いてきたと私はこの問題は、それを固定おろか持ったという他の誰を見つけることができないよう、任意の助けのために非常に感謝されます。あなたはそれにNULLを渡すと

+0

何デバッガはあなたを教えてくれありません。また、Valgrind(http://valgrind.org)のようなメモリマネージャを使ってコードを実行することを検討したいと思います。 – alk

+0

私は正直に言うと(私はGDBを使用)私は、デバッガで探しすべきかの情報にもわかりません。 – faissaloo

+0

または、これをより小さな例にカットしてください。 – alk

答えて

2

xmlTextWriterWriteElement()がうまく動作しません。形式として、あなたの文字列がAPIを呼び出す前に、NULLでないことを確認したりxmlTextWriterWriteFormatElementを(使用)との%s。