mirror of
https://github.com/lionsoul2014/ip2region.git
synced 2025-12-08 19:25:22 +00:00
apply the need save and put segment
This commit is contained in:
parent
8e3c440ada
commit
e085caf148
@ -322,9 +322,16 @@ func edit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
help()
|
help()
|
||||||
|
var sTip = ""
|
||||||
var reader = bufio.NewReader(os.Stdin)
|
var reader = bufio.NewReader(os.Stdin)
|
||||||
for {
|
for {
|
||||||
fmt.Printf(">> ")
|
if editor.NeedSave() {
|
||||||
|
sTip = "*"
|
||||||
|
} else {
|
||||||
|
sTip = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%seditor>> ", sTip)
|
||||||
line, err := reader.ReadString('\n')
|
line, err := reader.ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to read line from cli: %s\n", err)
|
fmt.Printf("failed to read line from cli: %s\n", err)
|
||||||
@ -335,11 +342,18 @@ func edit() {
|
|||||||
if cmd == "help" {
|
if cmd == "help" {
|
||||||
help()
|
help()
|
||||||
} else if cmd == "exit" {
|
} else if cmd == "exit" {
|
||||||
|
if editor.NeedSave() {
|
||||||
|
fmt.Printf("there are changes that need to save, type 'quit!' to force quit\n")
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else if cmd == "quit!" {
|
||||||
|
// quit directly
|
||||||
break
|
break
|
||||||
} else if cmd == "save" {
|
} else if cmd == "save" {
|
||||||
err = editor.Save()
|
err = editor.Save()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to save the changes: %s", err)
|
fmt.Printf("failed to save the changes: %s\n", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("all segments saved to %s\n", srcFile)
|
fmt.Printf("all segments saved to %s\n", srcFile)
|
||||||
|
|||||||
@ -17,6 +17,7 @@ type Editor struct {
|
|||||||
// source ip file
|
// source ip file
|
||||||
srcPath string
|
srcPath string
|
||||||
srcHandle *os.File
|
srcHandle *os.File
|
||||||
|
toSave bool
|
||||||
|
|
||||||
// segments list
|
// segments list
|
||||||
segments *list.List
|
segments *list.List
|
||||||
@ -37,6 +38,7 @@ func NewEditor(srcFile string) (*Editor, error) {
|
|||||||
e := &Editor{
|
e := &Editor{
|
||||||
srcPath: srcPath,
|
srcPath: srcPath,
|
||||||
srcHandle: srcHandle,
|
srcHandle: srcHandle,
|
||||||
|
toSave: false,
|
||||||
segments: list.New(),
|
segments: list.New(),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,6 +73,10 @@ func (e *Editor) loadSegments() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Editor) NeedSave() bool {
|
||||||
|
return e.toSave
|
||||||
|
}
|
||||||
|
|
||||||
func (e *Editor) SegLen() int {
|
func (e *Editor) SegLen() int {
|
||||||
return e.segments.Len()
|
return e.segments.Len()
|
||||||
}
|
}
|
||||||
@ -84,7 +90,50 @@ func (e *Editor) Put(ip string) error {
|
|||||||
return e.PutSegment(seg)
|
return e.PutSegment(seg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PutSegment put the specified segment into the current segment list with
|
||||||
|
// the following position relationships.
|
||||||
|
// 1, fully contained like:
|
||||||
|
// StartIP------seg.StartIP--------seg.EndIP----EndIP
|
||||||
|
// |------------------|
|
||||||
|
// 2, intersect like:
|
||||||
|
// StartIP------seg.StartIP------EndIP------|
|
||||||
|
// |---------------------seg.EndIP
|
||||||
|
//
|
||||||
func (e *Editor) PutSegment(seg *Segment) error {
|
func (e *Editor) PutSegment(seg *Segment) error {
|
||||||
|
var tOne *list.Element
|
||||||
|
var next *list.Element
|
||||||
|
for ele := e.segments.Front(); ele != nil; ele = next {
|
||||||
|
next = ele.Next()
|
||||||
|
s, ok := ele.Value.(*Segment)
|
||||||
|
if !ok {
|
||||||
|
// could this even be a case ?
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the related segment
|
||||||
|
if seg.StartIP >= s.StartIP && seg.StartIP <= s.EndIP {
|
||||||
|
tOne = ele
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if tOne == nil {
|
||||||
|
// could this even be a case ?
|
||||||
|
// if the loaded segments contains all the segments we have
|
||||||
|
// from 0 to 0xffffffff
|
||||||
|
return fmt.Errorf("failed to find the related segment")
|
||||||
|
}
|
||||||
|
|
||||||
|
s, ok := tOne.Value.(*Segment)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("internal error: invalid segment type")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("tOne: %s\n", s)
|
||||||
|
|
||||||
|
// open the to save flag
|
||||||
|
e.toSave = true
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,6 +157,11 @@ func (e *Editor) PutFile(src string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) Save() error {
|
func (e *Editor) Save() error {
|
||||||
|
// check the to-save flag
|
||||||
|
if e.toSave == false {
|
||||||
|
return fmt.Errorf("nothing changed")
|
||||||
|
}
|
||||||
|
|
||||||
dstHandle, err := os.OpenFile(e.srcPath, os.O_WRONLY|os.O_TRUNC, 0644)
|
dstHandle, err := os.OpenFile(e.srcPath, os.O_WRONLY|os.O_TRUNC, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -115,11 +169,11 @@ func (e *Editor) Save() error {
|
|||||||
|
|
||||||
// loop and flush all the segments to the dstHandle
|
// loop and flush all the segments to the dstHandle
|
||||||
var next *list.Element
|
var next *list.Element
|
||||||
for e := e.segments.Front(); e != nil; e = next {
|
for ele := e.segments.Front(); ele != nil; ele = next {
|
||||||
next = e.Next()
|
next = ele.Next()
|
||||||
s, ok := e.Value.(*Segment)
|
s, ok := ele.Value.(*Segment)
|
||||||
if !ok {
|
if !ok {
|
||||||
// could this even a case ?
|
// could this even be a case ?
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +185,9 @@ func (e *Editor) Save() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// close the handle
|
// close the handle
|
||||||
|
// and close the to-save flag
|
||||||
_ = dstHandle.Close()
|
_ = dstHandle.Close()
|
||||||
|
e.toSave = false
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user