2 Copyright © 2000, The AROS Development Team. All rights reserved.
5 Desc: DataTypesDescriptorCreator
9 #include "createdtdesc.h"
12 int main(int argc
, char **argv
)
14 struct DTDesc
*TheDTDesc
;
16 if(Init(argc
, argv
, &TheDTDesc
))
26 int HandleName(struct DTDesc
*TheDTDesc
)
35 TheDTDesc
->ReadBuffer
[READBUFFERSIZE
-1]='\0';
37 DataPtr
=TheDTDesc
->ReadBuffer
+KeywordLength
[Name
];
40 * What, who yells buffer-overflow here?
43 strcpy(TheDTDesc
->Name
, DataPtr
);
45 TheDTDesc
->DTH
.dth_Name
=TheDTDesc
->Name
;
50 int HandleVersion(struct DTDesc
*TheDTDesc
)
59 TheDTDesc
->ReadBuffer
[READBUFFERSIZE
-1]='\0';
61 DataPtr
=TheDTDesc
->ReadBuffer
+KeywordLength
[Version
];
64 * What, who yells buffer-overflow here?
67 strcpy(TheDTDesc
->Version
, DataPtr
);
72 int HandleBaseName(struct DTDesc
*TheDTDesc
)
81 TheDTDesc
->ReadBuffer
[READBUFFERSIZE
-1]='\0';
83 DataPtr
=TheDTDesc
->ReadBuffer
+KeywordLength
[BaseName
];
86 * What, who yells buffer-overflow here?
89 strcpy(TheDTDesc
->BaseName
, DataPtr
);
91 TheDTDesc
->DTH
.dth_BaseName
=TheDTDesc
->BaseName
;
96 int HandlePattern(struct DTDesc
*TheDTDesc
)
105 TheDTDesc
->ReadBuffer
[READBUFFERSIZE
-1]='\0';
107 DataPtr
=TheDTDesc
->ReadBuffer
+KeywordLength
[Pattern
];
110 * What, who yells buffer-overflow here?
113 strcpy(TheDTDesc
->Pattern
, DataPtr
);
115 TheDTDesc
->DTH
.dth_Pattern
=TheDTDesc
->Pattern
;
120 int HandleMask(struct DTDesc
*TheDTDesc
)
130 TheDTDesc
->ReadBuffer
[READBUFFERSIZE
-1]='\0';
132 DataPtr
=TheDTDesc
->ReadBuffer
+KeywordLength
[Mask
];
139 TheDTDesc
->DTH
.dth_MaskLen
= 0;
143 uint8_t c
= *DataPtr
++;
153 TheDTDesc
->Mask
[TheDTDesc
->DTH
.dth_MaskLen
++] = c
;
154 if (c
) c
= *DataPtr
++;
165 TheDTDesc
->Mask
[TheDTDesc
->DTH
.dth_MaskLen
++] = 0xFFFF;
174 i
= strtol(DataPtr
, (char **)(&NewDataPtr
), 0);
175 if (DataPtr
!= NewDataPtr
)
177 DataPtr
= NewDataPtr
;
178 TheDTDesc
->Mask
[TheDTDesc
->DTH
.dth_MaskLen
++] = i
;
191 TheDTDesc
->DTH
.dth_MaskLen
=(uint16_t) strlen(DataPtr
);
192 if(!TheDTDesc
->DTH
.dth_MaskLen
)
197 for(i
=0; i
<TheDTDesc
->DTH
.dth_MaskLen
; i
++)
199 TheDTDesc
->Mask
[i
] = (DataPtr
[i
]==0xFF) ? 0xFFFF : (uint16_t) DataPtr
[i
];
203 TheDTDesc
->DTH
.dth_Mask
=TheDTDesc
->Mask
;
208 int HandleGroupID(struct DTDesc
*TheDTDesc
)
217 TheDTDesc
->ReadBuffer
[READBUFFERSIZE
-1]='\0';
219 DataPtr
=TheDTDesc
->ReadBuffer
+KeywordLength
[GroupID
];
221 if(strlen(DataPtr
)<4)
226 TheDTDesc
->DTH
.dth_GroupID
=MAKE_ID(DataPtr
[0], DataPtr
[1], DataPtr
[2], DataPtr
[3]);
231 int HandleID(struct DTDesc
*TheDTDesc
)
240 TheDTDesc
->ReadBuffer
[READBUFFERSIZE
-1]='\0';
242 DataPtr
=TheDTDesc
->ReadBuffer
+KeywordLength
[ID
];
244 if(strlen(DataPtr
)<4)
249 TheDTDesc
->DTH
.dth_ID
=MAKE_ID(DataPtr
[0], DataPtr
[1], DataPtr
[2], DataPtr
[3]);
254 int HandleFlags(struct DTDesc
*TheDTDesc
)
260 const char *TheFlags
[] =
270 const int FlagLength
[] =
280 const int NumFlags
=6;
282 const uint16_t FlagValues
[] =
297 TheDTDesc
->ReadBuffer
[READBUFFERSIZE
-1]='\0';
299 DataPtr
=TheDTDesc
->ReadBuffer
+KeywordLength
[Flags
];
301 TheDTDesc
->DTH
.dth_Flags
=0;
311 for(i
=0; i
<NumFlags
; i
++)
313 if(Len
<FlagLength
[i
])
318 if(strncmp(TheFlags
[i
], DataPtr
, FlagLength
[i
])==0)
320 TheDTDesc
->DTH
.dth_Flags
|= FlagValues
[i
];
322 Len
-=(FlagLength
[i
]-1);
323 DataPtr
+=(FlagLength
[i
]-1);
336 int HandlePriority(struct DTDesc
*TheDTDesc
)
346 TheDTDesc
->ReadBuffer
[READBUFFERSIZE
-1]='\0';
348 DataPtr
=TheDTDesc
->ReadBuffer
+KeywordLength
[Priority
];
350 Pri
=strtoul(DataPtr
, NULL
, 10);
352 TheDTDesc
->DTH
.dth_Priority
=(uint16_t) Pri
;
357 int HandleLine(struct DTDesc
*TheDTDesc
)
369 for(i
=0; i
<NumKeywords
; i
++)
371 if(strncmp(TheDTDesc
->ReadBuffer
, Keywords
[i
], KeywordLength
[i
])==0)
373 RetVal
=KeywordHandler
[i
](TheDTDesc
);
382 int RemoveNewLine(struct DTDesc
*TheDTDesc
)
391 Len
=strlen(TheDTDesc
->ReadBuffer
);
393 if(TheDTDesc
->ReadBuffer
[Len
-1]=='\n')
395 TheDTDesc
->ReadBuffer
[Len
-1]='\0';
401 void Work(struct DTDesc
*TheDTDesc
)
409 memset(TheDTDesc
->ReadBuffer
, '\0', READBUFFERSIZE
);
411 while(fgets(TheDTDesc
->ReadBuffer
, READBUFFERSIZE
, TheDTDesc
->Input
))
413 RemoveNewLine(TheDTDesc
);
415 if(!HandleLine(TheDTDesc
))
420 memset(TheDTDesc
->ReadBuffer
, '\0', READBUFFERSIZE
);
423 WriteOutDTD(TheDTDesc
);
427 int WriteOutDTD(struct DTDesc
*TheDTDesc
)
429 struct IFFHandle
*IH
;
430 struct FileDataTypeHeader FileDTH
;
438 if(strlen(TheDTDesc
->Name
)==0)
443 if(strlen(TheDTDesc
->BaseName
)==0)
449 if(TheDTDesc
->DTH
.dth_MaskLen
==0)
455 if(strlen(TheDTDesc
->Pattern
)==0)
457 TheDTDesc
->Pattern
[0]='#';
458 TheDTDesc
->Pattern
[1]='?';
459 TheDTDesc
->Pattern
[2]='\0';
462 IH
=NewIFF(TheDTDesc
->OutputName
, MAKE_ID('D','T','Y','P'));
468 if(!NewChunk(IH
, MAKE_ID('N','A','M','E')))
471 remove(TheDTDesc
->Name
);
475 if(WriteChunkData(IH
, TheDTDesc
->Name
, (strlen(TheDTDesc
->Name
)+1))<=0)
479 remove(TheDTDesc
->Name
);
485 if(strlen(TheDTDesc
->Version
) > 0)
487 if(!NewChunk(IH
, MAKE_ID('F','V','E','R')))
490 remove(TheDTDesc
->Name
);
494 if(WriteChunkData(IH
, TheDTDesc
->Version
, (strlen(TheDTDesc
->Version
)+1))<=0)
498 remove(TheDTDesc
->Name
);
505 if(!NewChunk(IH
, MAKE_ID('D','T','H','D')))
508 remove(TheDTDesc
->Name
);
512 FileDTH
.dth_Name
= (((unsigned int) sizeof(struct FileDataTypeHeader
)));
513 FileDTH
.dth_BaseName
= (((unsigned int) FileDTH
.dth_Name
) + strlen(TheDTDesc
->DTH
.dth_Name
) + 1);
514 FileDTH
.dth_Pattern
= (((unsigned int) FileDTH
.dth_BaseName
) + strlen(TheDTDesc
->DTH
.dth_BaseName
) + 1);
515 FileDTH
.dth_Mask
= (((unsigned int) FileDTH
.dth_Pattern
) + strlen(TheDTDesc
->DTH
.dth_Pattern
) + 1);
516 FileDTH
.dth_GroupID
= TheDTDesc
->DTH
.dth_GroupID
;
517 FileDTH
.dth_ID
= TheDTDesc
->DTH
.dth_ID
;
518 FileDTH
.dth_MaskLen
= TheDTDesc
->DTH
.dth_MaskLen
;
519 FileDTH
.dth_Pad
= TheDTDesc
->DTH
.dth_Pad
;
520 FileDTH
.dth_Flags
= TheDTDesc
->DTH
.dth_Flags
;
521 FileDTH
.dth_Priority
= TheDTDesc
->DTH
.dth_Priority
;
523 FileDTH
.dth_Name
= Swap32IfLE(((uint32_t) FileDTH
.dth_Name
));
524 FileDTH
.dth_BaseName
= Swap32IfLE(((uint32_t) FileDTH
.dth_BaseName
));
525 FileDTH
.dth_Pattern
= Swap32IfLE(((uint32_t) FileDTH
.dth_Pattern
));
526 FileDTH
.dth_Mask
= Swap32IfLE(((uint32_t) FileDTH
.dth_Mask
));
527 FileDTH
.dth_GroupID
= Swap32IfLE(FileDTH
.dth_GroupID
);
528 FileDTH
.dth_ID
= Swap32IfLE(FileDTH
.dth_ID
);
529 FileDTH
.dth_MaskLen
= Swap16IfLE(FileDTH
.dth_MaskLen
);
530 FileDTH
.dth_Pad
= Swap16IfLE(FileDTH
.dth_Pad
);
531 FileDTH
.dth_Flags
= Swap16IfLE(FileDTH
.dth_Flags
);
532 FileDTH
.dth_Priority
= Swap16IfLE(FileDTH
.dth_Priority
);
534 if(WriteChunkData(IH
, (char *) &FileDTH
, sizeof(struct FileDataTypeHeader
))<=0)
538 remove(TheDTDesc
->Name
);
542 if(WriteChunkData(IH
, TheDTDesc
->DTH
.dth_Name
, (strlen(TheDTDesc
->DTH
.dth_Name
)+1))<=0)
546 remove(TheDTDesc
->Name
);
550 if(WriteChunkData(IH
, TheDTDesc
->DTH
.dth_BaseName
, (strlen(TheDTDesc
->DTH
.dth_BaseName
)+1))<=0)
554 remove(TheDTDesc
->Name
);
558 if(WriteChunkData(IH
, TheDTDesc
->DTH
.dth_Pattern
, (strlen(TheDTDesc
->DTH
.dth_Pattern
)+1))<=0)
562 remove(TheDTDesc
->Name
);
566 for(i
=0; i
<TheDTDesc
->DTH
.dth_MaskLen
; i
++)
568 TheDTDesc
->DTH
.dth_Mask
[i
]=Swap16IfLE(TheDTDesc
->DTH
.dth_Mask
[i
]);
571 if (TheDTDesc
->DTH
.dth_MaskLen
)
573 if(WriteChunkData(IH
, (char *) TheDTDesc
->DTH
.dth_Mask
, TheDTDesc
->DTH
.dth_MaskLen
*sizeof(uint16_t))<=0)
577 remove(TheDTDesc
->Name
);
589 int Init(int argc
, char **argv
, struct DTDesc
**TheDTDesc
)
591 struct DTDesc
*NewDTDesc
;
593 if(!(argv
&& TheDTDesc
))
600 NewDTDesc
=(struct DTDesc
*) malloc(sizeof(struct DTDesc
));
606 *TheDTDesc
=NewDTDesc
;
608 memset(NewDTDesc
, '\0', sizeof(struct DTDesc
));
610 NewDTDesc
->ProgName
=argv
[0];
611 NewDTDesc
->Input
=stdin
;
612 NewDTDesc
->OutputName
=NewDTDesc
->Name
;
614 if(!ParseArgs(argc
, argv
, NewDTDesc
))
619 if(!OpenInput(NewDTDesc
))
627 int OpenInput(struct DTDesc
*TheDTDesc
)
635 if(TheDTDesc
->InputName
)
637 TheDTDesc
->Input
=fopen(TheDTDesc
->InputName
, "r");
638 if(!TheDTDesc
->Input
)
640 TheDTDesc
->Input
=stdin
;
649 int ParseArgs(int argc
, char **argv
, struct DTDesc
*TheDTDesc
)
653 if(!(argv
&& TheDTDesc
))
658 for(i
=1; i
<argc
; i
++)
660 if(strcmp(argv
[i
], "-o") == 0)
664 Usage(TheDTDesc
->ProgName
);
669 TheDTDesc
->OutputName
=argv
[i
];
673 if(strcmp(argv
[i
], "-h") == 0)
675 Usage(TheDTDesc
->ProgName
);
681 TheDTDesc
->InputName
=argv
[i
];
689 void Usage(char *ProgName
)
691 char DefaultName
[]="createdtdesc";
694 NamePtr
= ProgName
? ProgName
: DefaultName
;
697 "usage: %s [-o <Output-Name>] <Input-Name>\n"
702 void Cleanup(struct DTDesc
*TheDTDesc
)
706 if((TheDTDesc
->Input
!=NULL
) && (TheDTDesc
->Input
!=stdin
))
708 fclose(TheDTDesc
->Input
);
711 free((void *) TheDTDesc
);