Created
April 5, 2017 23:11
-
-
Save jackyyf/1b60d55d4db20db82b67afcbdee93789 to your computer and use it in GitHub Desktop.
Gist by paste.py @ 2017-04-06 07:11:00.703243
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"os" | |
"strconv" | |
"strings" | |
"time" | |
) | |
const filename = "EnemyFleetRecord.csv" | |
const backuptemplate = "EnemyFleetRecord-%d.csv" | |
var stdin = bufio.NewReader(os.Stdin) | |
func Exit(code int) { | |
fmt.Println("Press enter to exit...") | |
stdin.ReadBytes('\n') | |
os.Exit(code) | |
} | |
func main() { | |
if _, err := os.Stat(filename); os.IsNotExist(err) { | |
fmt.Println("File", filename, "doesn't exist!") | |
Exit(1) | |
} else if err != nil { | |
fmt.Println("Error open while stat file", filename, ":", err.Error()) | |
Exit(254) | |
} | |
now := time.Now().UnixNano() | |
backupname := fmt.Sprintf(backuptemplate, now) | |
for { | |
if _, err := os.Stat(backupname); os.IsNotExist(err) { | |
break | |
} | |
now = time.Now().UnixNano() | |
backupname = fmt.Sprintf(backuptemplate, now) | |
} | |
original, err := os.Open(filename) | |
if err != nil { | |
fmt.Println("Can't open file for reading:", err.Error()) | |
Exit(1) | |
} | |
reader := bufio.NewReader(original) | |
if err := os.Rename(filename, backupname); err != nil { | |
fmt.Println("Can not rename file", filename, ":", err.Error()) | |
Exit(1) | |
} | |
fmt.Println("Old file is backuped at", backupname) | |
modified, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666) | |
defer modified.Close() | |
if err != nil { | |
fmt.Println("Can't open file for writing:", err.Error()) | |
Exit(1) | |
} | |
for i := 1; ; i++ { | |
line, err := reader.ReadString('\n') | |
if err != nil { | |
if err == io.EOF { | |
fmt.Printf("%d lines processed.\n", i-1) | |
break | |
} else { | |
fmt.Println("Error read file:", err.Error()) | |
Exit(1) | |
} | |
} else { | |
line = strings.TrimSpace(line) | |
} | |
csvparts := strings.Split(line, ",") | |
if len(csvparts) != 20 { | |
fmt.Printf("[Line #%d] CSV should contains 20 columns, leave this line untouched.\n", i) | |
fmt.Fprintln(modified, line) | |
continue | |
} | |
if _, err := strconv.ParseInt(csvparts[7], 10, 0); err != nil { | |
fmt.Printf("[Line #%d] Enemy ship ID is not a number, leave this line untouched.\n", i) | |
fmt.Fprintln(modified, line) | |
continue | |
} | |
e := false | |
d := false | |
for idx := 7; idx < 13; idx++ { | |
n, err := strconv.ParseInt(csvparts[idx], 10, 0) | |
if err != nil { | |
fmt.Printf("[Line #%d] UNEXPECTED ERROR when parse column %d: %s\n", i, idx, err.Error()) | |
fmt.Printf("[Line #%d] Leave this line untouched.\n", i) | |
e = true | |
break | |
} | |
// DO NOT TOUCH SHIP ID = -1 | |
if n > 500 && n < 1000 { | |
n += 1000 | |
} | |
// Duplicate line, remove it. | |
if n > 1500 && n < 2500 { | |
d = true | |
} | |
csvparts[idx] = strconv.FormatInt(n, 10) | |
} | |
if !d { | |
if !e { | |
fmt.Printf("[Line #%d] Line patched.\n", i) | |
line = strings.Join(csvparts, ",") | |
} | |
fmt.Fprintln(modified, line) | |
} else { | |
fmt.Printf("[Line #%d] Duplicate line. Removed from output.\n", i) | |
} | |
} | |
fmt.Println("Done!") | |
Exit(0) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"os" | |
"strconv" | |
"strings" | |
"time" | |
) | |
const filename = "ShipParameterRecord.csv" | |
const backuptemplate = "ShipParameterRecord-%d.csv" | |
var stdin = bufio.NewReader(os.Stdin) | |
func Exit(code int) { | |
fmt.Println("Press enter to exit...") | |
stdin.ReadBytes('\n') | |
os.Exit(code) | |
} | |
func main() { | |
if _, err := os.Stat(filename); os.IsNotExist(err) { | |
fmt.Println("File", filename, "doesn't exist!") | |
Exit(1) | |
} else if err != nil { | |
fmt.Println("Error open while stat file", filename, ":", err.Error()) | |
Exit(254) | |
} | |
now := time.Now().UnixNano() | |
backupname := fmt.Sprintf(backuptemplate, now) | |
for { | |
if _, err := os.Stat(backupname); os.IsNotExist(err) { | |
break | |
} | |
now = time.Now().UnixNano() | |
backupname = fmt.Sprintf(backuptemplate, now) | |
} | |
original, err := os.Open(filename) | |
if err != nil { | |
fmt.Println("Can't open file for reading:", err.Error()) | |
Exit(1) | |
} | |
reader := bufio.NewReader(original) | |
if err := os.Rename(filename, backupname); err != nil { | |
fmt.Println("Can not rename file", filename, ":", err.Error()) | |
Exit(1) | |
} | |
fmt.Println("Old file is backuped at", backupname) | |
modified, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666) | |
defer modified.Close() | |
if err != nil { | |
fmt.Println("Can't open file for writing:", err.Error()) | |
Exit(1) | |
} | |
for i := 1; ; i++ { | |
line, err := reader.ReadString('\n') | |
if err != nil { | |
if err == io.EOF { | |
fmt.Printf("%d lines processed.\n", i-1) | |
break | |
} else { | |
fmt.Println("Error read file:", err.Error()) | |
Exit(1) | |
} | |
} else { | |
line = strings.TrimSpace(line) | |
} | |
csvparts := strings.Split(line, ",") | |
if len(csvparts) != 36 { | |
fmt.Printf("[Line #%d] CSV should contains 36 columns, leave this line untouched.\n", i) | |
fmt.Fprintln(modified, line) | |
continue | |
} | |
n, err := strconv.ParseInt(csvparts[0], 10, 0) | |
if err != nil { | |
fmt.Printf("[Line #%d] Ship ID is not a number, leave this line untouched.\n", i) | |
fmt.Fprintln(modified, line) | |
continue | |
} | |
f := false | |
if n > 500 && n < 1000 && n != 503 && n != 508 { | |
n += 1000 | |
f = true | |
csvparts[0] = strconv.FormatInt(n, 10) | |
} | |
// Duplicate line, remove it. | |
if n < 1500 || f { | |
fmt.Printf("[Line #%d] Line patched.\n", i) | |
line = strings.Join(csvparts, ",") | |
fmt.Fprintln(modified, line) | |
} else { | |
fmt.Printf("[Line #%d] Duplicate line. Removed from output.\n", i) | |
} | |
} | |
fmt.Println("Done!") | |
Exit(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment