2 Copyright © 2000-2019, 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 //ASSERT((sizeof(struct FileDataTypeHeader) & 1) == 0);
513 if (TheDTDesc
->DTH
.dth_MaskLen
> 0)
515 // Write the mask directly after the header to preserve its word alignment.
516 FileDTH
.dth_Mask
= (((unsigned int) sizeof(struct FileDataTypeHeader
)));
517 FileDTH
.dth_Name
= (((unsigned int) FileDTH
.dth_Mask
) + (TheDTDesc
->DTH
.dth_MaskLen
<< 1));
518 FileDTH
.dth_Mask
= Swap32IfLE(((uint32_t) FileDTH
.dth_Mask
));
522 FileDTH
.dth_Mask
= 0;
523 FileDTH
.dth_Name
= (((unsigned int) sizeof(struct FileDataTypeHeader
)));
525 FileDTH
.dth_BaseName
= (((unsigned int) FileDTH
.dth_Name
) + strlen(TheDTDesc
->DTH
.dth_Name
) + 1);
526 FileDTH
.dth_Pattern
= (((unsigned int) FileDTH
.dth_BaseName
) + strlen(TheDTDesc
->DTH
.dth_BaseName
) + 1);
527 FileDTH
.dth_GroupID
= TheDTDesc
->DTH
.dth_GroupID
;
528 FileDTH
.dth_ID
= TheDTDesc
->DTH
.dth_ID
;
529 FileDTH
.dth_MaskLen
= TheDTDesc
->DTH
.dth_MaskLen
;
530 FileDTH
.dth_Pad
= TheDTDesc
->DTH
.dth_Pad
;
531 FileDTH
.dth_Flags
= TheDTDesc
->DTH
.dth_Flags
;
532 FileDTH
.dth_Priority
= TheDTDesc
->DTH
.dth_Priority
;
534 FileDTH
.dth_Name
= Swap32IfLE(((uint32_t) FileDTH
.dth_Name
));
535 FileDTH
.dth_BaseName
= Swap32IfLE(((uint32_t) FileDTH
.dth_BaseName
));
536 FileDTH
.dth_Pattern
= Swap32IfLE(((uint32_t) FileDTH
.dth_Pattern
));
537 FileDTH
.dth_GroupID
= Swap32IfLE(FileDTH
.dth_GroupID
);
538 FileDTH
.dth_ID
= Swap32IfLE(FileDTH
.dth_ID
);
539 FileDTH
.dth_MaskLen
= Swap16IfLE(FileDTH
.dth_MaskLen
);
540 FileDTH
.dth_Pad
= Swap16IfLE(FileDTH
.dth_Pad
);
541 FileDTH
.dth_Flags
= Swap16IfLE(FileDTH
.dth_Flags
);
542 FileDTH
.dth_Priority
= Swap16IfLE(FileDTH
.dth_Priority
);
544 if(WriteChunkData(IH
, (char *) &FileDTH
, sizeof(struct FileDataTypeHeader
))<=0)
548 remove(TheDTDesc
->Name
);
552 if (TheDTDesc
->DTH
.dth_MaskLen
)
554 for(i
=0; i
<TheDTDesc
->DTH
.dth_MaskLen
; i
++)
556 TheDTDesc
->DTH
.dth_Mask
[i
]=Swap16IfLE(TheDTDesc
->DTH
.dth_Mask
[i
]);
559 if(WriteChunkData(IH
, (char *) TheDTDesc
->DTH
.dth_Mask
, TheDTDesc
->DTH
.dth_MaskLen
<< 1)<=0)
563 remove(TheDTDesc
->Name
);
568 if(WriteChunkData(IH
, TheDTDesc
->DTH
.dth_Name
, (strlen(TheDTDesc
->DTH
.dth_Name
) + 1))<=0)
572 remove(TheDTDesc
->Name
);
576 if(WriteChunkData(IH
, TheDTDesc
->DTH
.dth_BaseName
, (strlen(TheDTDesc
->DTH
.dth_BaseName
) + 1))<=0)
580 remove(TheDTDesc
->Name
);
584 if(WriteChunkData(IH
, TheDTDesc
->DTH
.dth_Pattern
, (strlen(TheDTDesc
->DTH
.dth_Pattern
) + 1))<=0)
588 remove(TheDTDesc
->Name
);
599 int Init(int argc
, char **argv
, struct DTDesc
**TheDTDesc
)
601 struct DTDesc
*NewDTDesc
;
603 if(!(argv
&& TheDTDesc
))
610 NewDTDesc
=(struct DTDesc
*) malloc(sizeof(struct DTDesc
));
616 *TheDTDesc
=NewDTDesc
;
618 memset(NewDTDesc
, '\0', sizeof(struct DTDesc
));
620 NewDTDesc
->ProgName
=argv
[0];
621 NewDTDesc
->Input
=stdin
;
622 NewDTDesc
->OutputName
=NewDTDesc
->Name
;
624 if(!ParseArgs(argc
, argv
, NewDTDesc
))
629 if(!OpenInput(NewDTDesc
))
637 int OpenInput(struct DTDesc
*TheDTDesc
)
645 if(TheDTDesc
->InputName
)
647 TheDTDesc
->Input
=fopen(TheDTDesc
->InputName
, "r");
648 if(!TheDTDesc
->Input
)
650 TheDTDesc
->Input
=stdin
;
659 int ParseArgs(int argc
, char **argv
, struct DTDesc
*TheDTDesc
)
663 if(!(argv
&& TheDTDesc
))
668 for(i
=1; i
<argc
; i
++)
670 if(strcmp(argv
[i
], "-o") == 0)
674 Usage(TheDTDesc
->ProgName
);
679 TheDTDesc
->OutputName
=argv
[i
];
683 if(strcmp(argv
[i
], "-h") == 0)
685 Usage(TheDTDesc
->ProgName
);
691 TheDTDesc
->InputName
=argv
[i
];
699 void Usage(char *ProgName
)
701 char DefaultName
[]="createdtdesc";
704 NamePtr
= ProgName
? ProgName
: DefaultName
;
707 "usage: %s [-o <Output-Name>] <Input-Name>\n"
712 void Cleanup(struct DTDesc
*TheDTDesc
)
716 if((TheDTDesc
->Input
!=NULL
) && (TheDTDesc
->Input
!=stdin
))
718 fclose(TheDTDesc
->Input
);
721 free((void *) TheDTDesc
);