From 4e7488d9b9c9daf00a271891efcff931e8385670 Mon Sep 17 00:00:00 2001 From: uz Date: Fri, 9 Mar 2012 11:46:16 +0000 Subject: [PATCH] Added the write routine. git-svn-id: svn://svn.cc65.org/cc65/trunk@5584 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/sp65/input.c | 10 +--------- src/sp65/main.c | 50 +++++++++++++++++++++++++++++++++++++++-------- src/sp65/output.c | 20 +++++++++++++++++++ src/sp65/output.h | 5 +++++ 4 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/sp65/input.c b/src/sp65/input.c index a34e527e7..6e3073f80 100644 --- a/src/sp65/input.c +++ b/src/sp65/input.c @@ -80,14 +80,6 @@ static const FileId FormatTable[] = { -static int Compare (const void* Key, const void* Id) -/* Compare function for bsearch */ -{ - return strcmp (Key, ((const FileId*) Id)->Ext); -} - - - int FindInputFormat (const char* Name) /* Find an input format by name. The function returns a value less than zero * if Name is not a known input format. @@ -98,7 +90,7 @@ int FindInputFormat (const char* Name) FormatTable, sizeof (FormatTable) / sizeof (FormatTable[0]), sizeof (FormatTable[0]), - Compare); + CompareFileId); /* Return the id or an error code */ return (F == 0)? -1 : F->Id; diff --git a/src/sp65/main.c b/src/sp65/main.c index 7a72f5a5e..db77b7109 100644 --- a/src/sp65/main.c +++ b/src/sp65/main.c @@ -48,6 +48,7 @@ #include "attr.h" #include "error.h" #include "input.h" +#include "output.h" @@ -77,16 +78,18 @@ static void Usage (void) printf ( "Usage: %s [options] file [options] [file]\n" "Short options:\n" - " -V\t\t\tPrint the version number and exit\n" - " -h\t\t\tHelp (this text)\n" - " -v\t\t\tIncrease verbosity\n" + " -V\t\t\t\tPrint the version number and exit\n" + " -h\t\t\t\tHelp (this text)\n" + " -v\t\t\t\tIncrease verbosity\n" "\n" "Long options:\n" - " --help\t\tHelp (this text)\n" - " --pop\t\t\tRestore the original loaded image\n" - " --slice x,y,w,h\tGenerate a slice from the loaded bitmap\n" - " --verbose\t\tIncrease verbosity\n" - " --version\t\tPrint the version number and exit\n", + " --help\t\t\tHelp (this text)\n" + " --pop\t\t\t\tRestore the original loaded image\n" + " --read file[,attrlist]\tRead an input file\n" + " --slice x,y,w,h\t\tGenerate a slice from the loaded bitmap\n" + " --verbose\t\t\tIncrease verbosity\n" + " --version\t\t\tPrint the version number and exit\n" + " --write file[,attrlist]\tWrite an output file\n", ProgName); } @@ -221,6 +224,36 @@ static void OptVersion (const char* Opt attribute ((unused)), +static void OptWrite (const char* Opt, const char* Arg) +/* Write an output file */ +{ + static const char* NameList[] = { + "name", "format" + }; + + + /* Parse the argument */ + Collection* A = ParseAttrList (Arg, NameList, 2); + + /* Must have a file name given */ + const char* FileName = NeedAttrVal (A, "name", Opt); + + /* Determine the format of the input file */ + int OF = ofAuto; + const char* Format = GetAttrVal (A, "format"); + if (Format != 0) { + OF = FindOutputFormat (Format); + if (OF < 0) { + Error ("Unknown output format `%s'", Format); + } + } + + /* Write the file */ + WriteOutputFile (FileName, 0, OF); +} + + + int main (int argc, char* argv []) /* sp65 main program */ { @@ -232,6 +265,7 @@ int main (int argc, char* argv []) { "--slice", 1, OptSlice }, { "--verbose", 0, OptVerbose }, { "--version", 0, OptVersion }, + { "--write", 1, OptWrite }, }; unsigned I; diff --git a/src/sp65/output.c b/src/sp65/output.c index 102e4c355..0dbe52825 100644 --- a/src/sp65/output.c +++ b/src/sp65/output.c @@ -33,6 +33,8 @@ +#include + /* common */ #include "fileid.h" @@ -88,6 +90,24 @@ static const FileId FormatTable[] = { +int FindOutputFormat (const char* Name) +/* Find an output format by name. The function returns a value less than zero + * if Name is not a known output format. + */ +{ + /* Search for the entry in the table. */ + const FileId* F = bsearch (Name, + FormatTable, + sizeof (FormatTable) / sizeof (FormatTable[0]), + sizeof (FormatTable[0]), + CompareFileId); + + /* Return the id or an error code */ + return (F == 0)? -1 : F->Id; +} + + + void WriteOutputFile (const char* Name, const StrBuf* Data, OutputFormat Format) /* Write the contents of Data to the given file in the format specified. If * the format is ofAuto, it is determined by the file extension. diff --git a/src/sp65/output.h b/src/sp65/output.h index 2f83993d8..9b564b59d 100644 --- a/src/sp65/output.h +++ b/src/sp65/output.h @@ -67,6 +67,11 @@ typedef enum OutputFormat OutputFormat; +int FindOutputFormat (const char* Name); +/* Find an output format by name. The function returns a value less than zero + * if Name is not a known output format. + */ + void WriteOutputFile (const char* Name, const StrBuf* Data, OutputFormat Format); /* Write the contents of Data to the given file in the format specified. If * the format is ofAuto, it is determined by the file extension. -- 2.39.5