1
0
mirror of https://github.com/cc65/cc65.git synced 2024-10-01 00:57:11 +00:00

Added the write routine.

git-svn-id: svn://svn.cc65.org/cc65/trunk@5584 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz 2012-03-09 11:46:16 +00:00
parent b6329757d7
commit 4e7488d9b9
4 changed files with 68 additions and 17 deletions

View File

@ -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;

View File

@ -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;

View File

@ -33,6 +33,8 @@
#include <stdlib.h>
/* 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.

View File

@ -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.