|
4 | 4 | "encoding/json"
|
5 | 5 | "fmt"
|
6 | 6 | "io/ioutil"
|
| 7 | + "os" |
7 | 8 | "strconv"
|
8 | 9 | "strings"
|
9 | 10 |
|
@@ -376,11 +377,53 @@ func (this *Ini) DelSection(section string) *Ini {
|
376 | 377 |
|
377 | 378 | // TODO: implement Save
|
378 | 379 | func (this *Ini) Save(filename string) *Ini {
|
379 |
| - return this |
380 |
| -} |
381 | 380 |
|
382 |
| -// TODO: implement SaveFile |
383 |
| -func (this *Ini) SaveFile(filename string) *Ini { |
| 381 | + if filename == "" { |
| 382 | + return this |
| 383 | + } |
| 384 | + |
| 385 | + if this.doc == nil { |
| 386 | + return this |
| 387 | + } |
| 388 | + |
| 389 | + var result string |
| 390 | + |
| 391 | + is_last_type_comment := false |
| 392 | + for c := this.doc.FirstChild(); c != nil; c = c.NextSibling() { |
| 393 | + if kv_node, ok := c.(*ast.KVNode); ok { |
| 394 | + is_last_type_comment = false |
| 395 | + result = fmt.Sprintf("%s%s = %v\n", result, kv_node.Key.Literal, kv_node.Value.Literal) |
| 396 | + continue |
| 397 | + } |
| 398 | + |
| 399 | + if sect_node, ok := c.(*ast.SetcionNode); ok { |
| 400 | + is_last_type_comment = false |
| 401 | + result = fmt.Sprintf("%s[%s]\n", result, sect_node.Name.Literal) |
| 402 | + |
| 403 | + for c := sect_node.FirstChild(); c != nil; c = c.NextSibling() { |
| 404 | + if kv_node, ok := c.(*ast.KVNode); ok { |
| 405 | + result = fmt.Sprintf("%s%s = %v\n", result, kv_node.Key.Literal, kv_node.Value.Literal) |
| 406 | + continue |
| 407 | + } |
| 408 | + } |
| 409 | + |
| 410 | + continue |
| 411 | + } |
| 412 | + |
| 413 | + if comm_node, ok := c.(*ast.CommentNode); ok { |
| 414 | + if !is_last_type_comment { |
| 415 | + result = fmt.Sprintf("%s\n", result) |
| 416 | + } |
| 417 | + |
| 418 | + is_last_type_comment = true |
| 419 | + result = fmt.Sprintf("%s%s\n", result, comm_node.Comment.Literal) |
| 420 | + } |
| 421 | + } |
| 422 | + |
| 423 | + os.Remove(filename) |
| 424 | + var data = []byte(result) |
| 425 | + this.err = ioutil.WriteFile(filename, data, 0666) |
| 426 | + |
384 | 427 | return this
|
385 | 428 | }
|
386 | 429 |
|
|
0 commit comments