@@ -12,7 +12,6 @@ import (
1212 "os"
1313 "path"
1414 "path/filepath"
15- "strconv"
1615 "strings"
1716
1817 "github.com/jeromelesaux/dsk/amsdos"
5857 quiet = flag .Bool ("quiet" , false , "Suppress unnecessary output (useful for scripting)." )
5958 stdoutOpt = flag .Bool ("stdout" , false , "To redirect to stdout when using get file" )
6059 hidden = flag .Bool ("hide" , false , "hide the imported file" )
60+ removeHeader = flag .Bool ("removeheader" , false , "remove amsdos header from exported file" )
6161 appVersion = "0.34"
6262 version = flag .Bool ("version" , false , "Display the application version and exit." )
6363)
@@ -127,7 +127,7 @@ func main() {
127127 if err {
128128 fmt .Fprintf (os .Stderr , "Error while opening file %s error :%s\n " , dskFilepath , msg )
129129 }
130- err , msg , _ = getFileDsk (d , "*" , dskFilepath , dskfolderPath )
130+ err , msg , _ = getFileDsk (d , "*" , dskFilepath , dskfolderPath , * removeHeader )
131131 if err {
132132 fmt .Fprintf (os .Stderr , "Error while writing file %s in folder %s error :%s\n " , dskFilepath , dskfolderPath , msg )
133133 }
@@ -138,10 +138,18 @@ func main() {
138138 }
139139
140140 if * executeAddress != "" {
141- execAddress = parseHexadecimal16bits (* executeAddress )
141+ var err error
142+ execAddress , err = utils .ParseHex16 (* executeAddress )
143+ if err != nil {
144+ exitOnError (err .Error (), "Invalid execute address format" )
145+ }
142146 }
143147 if * loadingAddress != "" {
144- loadAddress = parseHexadecimal16bits (* loadingAddress )
148+ var err error
149+ loadAddress , err = utils .ParseHex16 (* loadingAddress )
150+ if err != nil {
151+ exitOnError (err .Error (), "Invalid loading address format" )
152+ }
145153 }
146154 if ! * quiet {
147155 fmt .Fprintf (os .Stderr , "DSK cli version [%s]\n Made by Sid (ImpAct)\n " , appVersion )
@@ -362,7 +370,7 @@ func main() {
362370 }
363371 }
364372 } else {
365- isError , msg , hint := getFileDsk (d , * get , * dskPath , directory )
373+ isError , msg , hint := getFileDsk (d , * get , * dskPath , directory , * removeHeader )
366374 if isError {
367375 exitOnError (msg , hint )
368376 }
@@ -723,35 +731,6 @@ func fileinfoDsk(d dsk.DSK, fileInDsk string) (onError bool, message, hint strin
723731 return false , "" , ""
724732}
725733
726- func parseHexadecimal16bits (address string ) (value16 uint16 ) {
727- switch address [0 ] {
728- case '#' :
729- value := strings .Replace (address , "#" , "" , - 1 )
730- v , err := strconv .ParseUint (value , 16 , 16 )
731- if err != nil {
732- fmt .Fprintf (os .Stderr , "cannot get the hexadecimal value fom %s, error : %v\n " , * executeAddress , err )
733- } else {
734- value16 = uint16 (v )
735- }
736- case '0' :
737- value := strings .Replace (address , "0x" , "" , - 1 )
738- v , err := strconv .ParseUint (value , 16 , 16 )
739- if err != nil {
740- fmt .Fprintf (os .Stderr , "cannot get the hexadecimal value fom %s, error : %v\n " , * executeAddress , err )
741- } else {
742- value16 = uint16 (v )
743- }
744- default :
745- v , err := strconv .ParseUint (address , 10 , 16 )
746- if err != nil {
747- fmt .Fprintf (os .Stderr , "cannot get the hexadecimal value fom %s, error : %v\n " , * executeAddress , err )
748- } else {
749- value16 = uint16 (v )
750- }
751- }
752- return
753- }
754-
755734func analyseDsk (d dsk.DSK , dskPath string ) (onError bool , message , hint string ) {
756735 if err := d .CheckDsk (); err != nil {
757736 return true , fmt .Sprintf ("Error while read dsk file (%s) error %v\n " , dskPath , err ), "Check your dsk file path or Check your dsk file with option -dsk yourdsk.dsk -analyze"
@@ -807,7 +786,7 @@ func putFileDsk(d dsk.DSK, fileInDsk, dskPath string, fileType string, loadAddre
807786 return false , "" , ""
808787}
809788
810- func getFileDsk (d dsk.DSK , fileInDsk , dskPath , directory string ) (onError bool , message , hint string ) {
789+ func getFileDsk (d dsk.DSK , fileInDsk , dskPath , directory string , removeHeader bool ) (onError bool , message , hint string ) {
811790 if fileInDsk == "" {
812791 return true , "amsdosfile option is empty, set it." , "dsk -dsk output.dsk -get -amsdosfile hello.bin"
813792 }
@@ -846,6 +825,12 @@ func getFileDsk(d dsk.DSK, fileInDsk, dskPath, directory string) (onError bool,
846825 if err != nil {
847826 return true , fmt .Sprintf ("Error while creating file (%s) error %v\n " , filename , err ), "Check your dsk with option -dsk yourdsk.dsk -analyze"
848827 }
828+ if removeHeader {
829+ isAmsdos , _ := amsdos .CheckAmsdos (content )
830+ if isAmsdos {
831+ content = content [256 :] // Remove the first 256 bytes (AMS/DOS header)
832+ }
833+ }
849834 _ , err = af .Write (content )
850835 if err != nil {
851836 return true , fmt .Sprintf ("Error while copying content in file (%s) error %v\n " , filename , err ), "Check your dsk with option -dsk yourdsk.dsk -analyze"
@@ -871,6 +856,12 @@ func getFileDsk(d dsk.DSK, fileInDsk, dskPath, directory string) (onError bool,
871856 return true , fmt .Sprintf ("Error while creating file (%s) error %v\n " , filename , err ), "Check your file path"
872857 }
873858 defer af .Close ()
859+ if removeHeader {
860+ isAmsdos , _ := amsdos .CheckAmsdos (content )
861+ if isAmsdos {
862+ content = content [256 :] // Remove the first 256 bytes (AMS/DOS header)
863+ }
864+ }
874865 _ , err = af .Write (content )
875866 if err != nil {
876867 return true , fmt .Sprintf ("Error while copying content in file (%s) error %v\n " , filename , err ), " Check your dsk with option -dsk yourdsk.dsk -analyze"
@@ -1480,25 +1471,37 @@ func analyseDataTest(dskFilepath string) bool {
14801471
14811472func parsing16bitsRasmAnnotation () bool {
14821473 fmt .Printf ("Parsing value rasm annotation #C000 " )
1483- v := parseHexadecimal16bits ("#C000" )
1474+ v , err := utils .ParseHex16 ("#C000" )
1475+ if err != nil {
1476+ return false
1477+ }
14841478 return v == 0xC000
14851479}
14861480
14871481func parsing16bitsCAnnotation () bool {
14881482 fmt .Printf ("Parsing value c annotation 0xC000 " )
1489- v := parseHexadecimal16bits ("0xC000" )
1483+ v , err := utils .ParseHex16 ("0xC000" )
1484+ if err != nil {
1485+ return false
1486+ }
14901487 return v == 0xC000
14911488}
14921489
14931490func parsing16bitsIntegerAnnotation () bool {
14941491 fmt .Printf ("Parsing value integer annotation 49152 " )
1495- v := parseHexadecimal16bits ("49152" )
1492+ v , err := utils .ParseHex16 ("49152" )
1493+ if err != nil {
1494+ return false
1495+ }
14961496 return v == 0xC000
14971497}
14981498
14991499func parsing8bitsRasmAnnotation () bool {
15001500 fmt .Printf ("Parsing value c annotation #D0 " )
1501- v := parseHexadecimal16bits ("#D0" )
1501+ v , err := utils .ParseHex16 ("#D0" )
1502+ if err != nil {
1503+ return false
1504+ }
15021505 return v == 0xD0
15031506}
15041507
@@ -1520,7 +1523,7 @@ func getFileBinaryDataTest(filePath, dskFilepath string) bool {
15201523 if onError {
15211524 return onError
15221525 }
1523- isError , _ , _ := getFileDsk (d , filePath , dskFilepath , "" )
1526+ isError , _ , _ := getFileDsk (d , filePath , dskFilepath , "" , false )
15241527 return isError
15251528}
15261529
0 commit comments