|
4 | 4 | _ "embed" |
5 | 5 | "fmt" |
6 | 6 | "io" |
| 7 | + "os" |
7 | 8 | "runtime/debug" |
8 | 9 | "sync" |
9 | 10 | "time" |
@@ -350,6 +351,60 @@ func NewCSTVBroadcastParserWithConfig(baseUrl string, config ParserConfig) (Pars |
350 | 351 | return NewParserWithConfig(r, config), nil |
351 | 352 | } |
352 | 353 |
|
| 354 | +type ConfigureParserCallback func(Parser) error |
| 355 | + |
| 356 | +// ParseWithConfig parses a demo from the given io.Reader with a custom configuration. |
| 357 | +// The handler is called with the Parser instance. |
| 358 | +// |
| 359 | +// Returns an error if the parser encounters an error. |
| 360 | +func ParseWithConfig(r io.Reader, config ParserConfig, configure ConfigureParserCallback) error { |
| 361 | + p := NewParserWithConfig(r, config) |
| 362 | + defer p.Close() |
| 363 | + |
| 364 | + err := configure(p) |
| 365 | + if err != nil { |
| 366 | + return fmt.Errorf("failed to configure parser: %w", err) |
| 367 | + } |
| 368 | + |
| 369 | + err = p.ParseToEnd() |
| 370 | + if err != nil { |
| 371 | + return fmt.Errorf("failed to parse demo: %w", err) |
| 372 | + } |
| 373 | + |
| 374 | + return nil |
| 375 | +} |
| 376 | + |
| 377 | +// Parse parses a demo from the given io.Reader. |
| 378 | +// The handler is called with the Parser instance. |
| 379 | +// |
| 380 | +// Returns an error if the parser encounters an error. |
| 381 | +func Parse(r io.Reader, configure ConfigureParserCallback) error { |
| 382 | + return ParseWithConfig(r, DefaultParserConfig, configure) |
| 383 | +} |
| 384 | + |
| 385 | +// ParseFileWithConfig parses a demo file at the given path with a custom configuration. |
| 386 | +// The handler is called with the Parser instance. |
| 387 | +// |
| 388 | +// Returns an error if the file can't be opened or if the parser encounters an error. |
| 389 | +func ParseFileWithConfig(path string, config ParserConfig, configure ConfigureParserCallback) error { |
| 390 | + f, err := os.Open(path) |
| 391 | + if err != nil { |
| 392 | + return fmt.Errorf("failed to open file: %w", err) |
| 393 | + } |
| 394 | + |
| 395 | + defer f.Close() |
| 396 | + |
| 397 | + return ParseWithConfig(f, config, configure) |
| 398 | +} |
| 399 | + |
| 400 | +// ParseFile parses a demo file at the given path. |
| 401 | +// The handler is called with the Parser instance. |
| 402 | +// |
| 403 | +// Returns an error if the file can't be opened or if the parser encounters an error. |
| 404 | +func ParseFile(path string, configure ConfigureParserCallback) error { |
| 405 | + return ParseFileWithConfig(path, DefaultParserConfig, configure) |
| 406 | +} |
| 407 | + |
353 | 408 | // ParserConfig contains the configuration for creating a new Parser. |
354 | 409 | type ParserConfig struct { |
355 | 410 | // MsgQueueBufferSize defines the size of the internal net-message queue. |
|
0 commit comments