Updated FileNode Struct

Added New Field `absolutePath` to FileNode struct. This helps in adding
the song to the playlist through the Add Button.
This commit is contained in:
aditya-K2 2021-10-17 21:51:01 +05:30
parent eecb86ed8f
commit 680b41cb6e

View File

@ -1,18 +1,23 @@
package main package main
import ( import (
"strings"
"fmt" "fmt"
"strings"
) )
type FileNode struct { type FileNode struct {
children []FileNode children []FileNode
path string path string
parent *FileNode parent *FileNode
absolutePath string
} }
func (f *FileNode) addChildren(path string) { func (f *FileNode) addChildren(path string) {
f.children = append(f.children, FileNode{children: make([]FileNode, 0), path: path, parent: f}) if f.path != "" {
f.children = append(f.children, FileNode{children: make([]FileNode, 0), path: path, parent: f, absolutePath: f.absolutePath + "/" + path})
} else {
f.children = append(f.children, FileNode{children: make([]FileNode, 0), path: path, parent: f, absolutePath: f.absolutePath + path})
}
} }
func (f *FileNode) addChildNode(m FileNode) { func (f *FileNode) addChildNode(m FileNode) {
@ -23,22 +28,22 @@ func (f *FileNode) addChildNode(m FileNode){
func generateDirectoryTree(path []string) *FileNode { func generateDirectoryTree(path []string) *FileNode {
var head *FileNode = new(FileNode) var head *FileNode = new(FileNode)
var head1 *FileNode = head var head1 *FileNode = head
for i := range(path){ for i := range path {
sepPaths := strings.Split(path[i], "/") sepPaths := strings.Split(path[i], "/")
for j := range(sepPaths){ for j := range sepPaths {
if(len(head.children) == 0){ if len(head.children) == 0 {
head.addChildren(sepPaths[j]) head.addChildren(sepPaths[j])
head = &(head.children[len(head.children)-1]) head = &(head.children[len(head.children)-1])
} else { } else {
var headIsChanged = false var headIsChanged = false
for k := range(head.children){ for k := range head.children {
if head.children[k].path == sepPaths[j] { if head.children[k].path == sepPaths[j] {
head = &(head.children[k]) head = &(head.children[k])
headIsChanged = true headIsChanged = true
break break
} }
} }
if(!headIsChanged){ if !headIsChanged {
head.addChildren(sepPaths[j]) head.addChildren(sepPaths[j])
head = &(head.children[len(head.children)-1]) head = &(head.children[len(head.children)-1])
} }
@ -50,13 +55,14 @@ func generateDirectoryTree(path [] string) *FileNode{
} }
func (f FileNode) Print(count int) { func (f FileNode) Print(count int) {
if (len(f.children) == 0){ if len(f.children) == 0 {
return return
} else { } else {
for i := range f.children { for i := range f.children {
for j := 0; j < count; j++ { for j := 0; j < count; j++ {
fmt.Print("---") fmt.Print("---")
}; fmt.Println(f.children[i].path) }
fmt.Println(f.children[i].absolutePath)
f.children[i].Print(count + 1) f.children[i].Print(count + 1)
} }
} }