das2C
das core C utilities (v3)
Loading...
Searching...
No Matches
uri.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Chris Piker <chris-piker@uiowa.edu>
2 *
3 * This file is part of das2C, the Core Das2 C Library.
4 *
5 * das2C is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License version 2.1 as published
7 * by the Free Software Foundation.
8 *
9 * das2C is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * version 2.1 along with das2C; if not, see <http://www.gnu.org/licenses/>.
16 */
17
189#ifndef _das_uri_h_
190#define _das_uri_h_
191
192#include <stdbool.h>
193#include <stdint.h>
194
195#include <das3/defs.h>
196#include <das3/time.h>
197#include <das3/datum.h>
198
199#ifdef __cplusplus
200extern "C" {
201#endif
202
203
204/* ************************************************************************* */
205/* Limits */
206
208#define DURI_MAX_PATH 2048
209
210
211/* ************************************************************************* */
212/* Coordinate segment field and type definitions */
213
222typedef struct das_uri_field_t {
223 char cShort; /* single-char short token, e.g. 'Y'; '\0' if none */
224 char sLong[32]; /* long name used in qualified tokens, e.g. "year" in $(time.year) */
225 int nWidth; /* rendered field width (zero-padded); 0 = variable */
226 int nMin; /* valid decoded-value range, minimum (inclusive) */
227 int nMax; /* valid decoded-value range, maximum (inclusive) */
229
230
254typedef struct das_uri_seg_def_t {
255 char sCoord[32]; /* coordinate name, e.g. "time", "sclk", "orbit" */
256 int nFields; /* number of entries in pFields */
257 DasUriField* pFields; /* sub-field table; caller owns until register() */
259
260
261/* ************************************************************************* */
262/* Coordinate query range */
263
312typedef struct das_range_t {
313 char sCoord[32]; /* coordinate or sub-field: "time", "sclk.mod64k" */
314 das_datum dBeg; /* inclusive range begin */
315 das_datum dEnd; /* exclusive range end */
316} das_range;
317
318
319/* ************************************************************************* */
320/* Range initializers */
321
334 das_range* pRng, const char* sBeg, const char* sEnd
335);
336
337
350 das_range* pRng, const das_time* tBeg, const das_time* tEnd
351);
352
353
372 das_range* pRng, const char* sCoord, int64_t nBeg, int64_t nEnd
373);
374
375
390 das_range* pRng, const char* sCoord,
391 const das_datum* dmBeg, const das_datum* dmEnd
392);
393
394
395/* ************************************************************************* */
396/* Protocol enumeration */
397
401typedef enum das_uri_proto_e {
402 DURI_PROTO_FILE = 0, /* no prefix, or explicit file:// */
403 DURI_PROTO_HTTP, /* http:// */
404 DURI_PROTO_HTTPS, /* https:// */
406
407
408/* Opaque segment type — full definition in uri.c */
409typedef struct das_uri_seg_t DasUriSeg;
410
411/* Opaque level plan type — full definition in uri.c.
412 * A "level" is one path component (one directory name, or the filename).
413 * The plan is built from pSegs at DasUriTplt_pattern() time and is read-only
414 * thereafter; iterators hold only runtime directory-walk state, not plan state. */
415typedef struct das_uri_level_t DasUriLevel;
416
417
418/* ************************************************************************* */
419/* A parsed URI template */
420
421typedef struct das_uri_tplt_t {
422 bool bHasWild; /* true if template contains $x or $v */
423 bool bLiteral; /* true if template contains no coordinate fields; */
424 /* ranges are ignored and one path is yielded */
425 DasUriProto eProto; /* protocol derived from leading scheme, or */
426 /* DURI_PROTO_FILE if no prefix is present */
427 int nSegs; /* number of entries in pSegs */
428 DasUriSeg* pSegs; /* heap-allocated segment array; freed by del_ */
429 int nDefs; /* number of registered coordinate definitions */
430 DasUriSegDef* pDefs; /* deep-copied def array; freed by del_ */
431 char* sBase; /* fixed path prefix up to the first variable; */
432 /* "." for CWD-relative templates; root ("/" on */
433 /* POSIX, "C:\\" on Windows) if only the root is */
434 /* fixed. No trailing separator otherwise. */
435 int nLevels; /* number of directory + filename levels */
436 DasUriLevel* pLevels; /* level plan built in DasUriTplt_pattern(); */
437 /* freed by del_DasUriTplt() */
438} DasUriTplt;
439
440
441/* ************************************************************************* */
442/* A streaming iterator over a URI template and coordinate ranges */
443
444typedef struct das_uri_iter_t {
445 const DasUriTplt* pTplt;
446 int nRanges;
447 const das_range* pRanges; /* caller owns; must outlive iterator */
448 bool bDone;
449 char sCurrent[DURI_MAX_PATH]; /* path returned by _next() */
450 /* For file:// the file:// prefix is stripped; the path */
451 /* is usable directly with fopen() etc. */
452 /* For http/https the full URL is stored here; the caller*/
453 /* is responsible for downloading before use. */
454 void* pState; /* internal heap-allocated scan state; */
455 /* managed by init_/fini_/new_/del_ */
456} DasUriIter;
457
458
459/* ************************************************************************* */
460/* API */
461
480DAS_API const DasUriSegDef* das_time_uridef(void);
481
482
494DAS_API DasUriTplt* new_DasUriTplt(void);
495
496
518DAS_API DasErrCode DasUriTplt_register(DasUriTplt* pThis, const DasUriSegDef* pDef);
519
520
545DAS_API DasErrCode DasUriTplt_pattern(DasUriTplt* pThis, const char* sTemplate);
546
547
551DAS_API void del_DasUriTplt(DasUriTplt* pTplt);
552
553
563DAS_API char* DasUriTplt_toStr(const DasUriTplt* pThis, char* sBuf, int nLen);
564
565
595DAS_API DasErrCode init_DasUriIter(
596 DasUriIter* pThis, const DasUriTplt* pTplt,
597 int nRanges, const das_range* pRanges
598);
599
600
608DAS_API void fini_DasUriIter(DasUriIter* pThis);
609
610
635DAS_API DasUriIter* new_DasUriIter(
636 const DasUriTplt* pTplt, int nRanges, const das_range* pRanges
637);
638
639
656DAS_API const char* DasUriIter_next(DasUriIter* pThis);
657
658
662DAS_API void del_DasUriIter(DasUriIter* pThis);
663
664
665/* ************************************************************************* */
666/* Convenience functions */
667
690DAS_API char* DasUriTplt_render(
691 const DasUriTplt* pThis, int nRanges, const das_range* pRanges,
692 char* sBuf, int nLen
693);
694
695
746DAS_API char** das_uri_list(
747 const char* sTemplate, const DasUriSegDef* pDef,
748 int nRanges, const das_range* pRanges,
749 size_t* pCount
750);
751
752
753#ifdef __cplusplus
754}
755#endif
756
757#endif /* _das_uri_h_ */
Minimal definitions for das2 utilities that can safely be run without calling das_init().
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition defs.h:184
One sub-field within a named coordinate type.
Definition uri.h:222
Definition of one coordinate type for use by the URI template parser.
Definition uri.h:254
A constraint on one coordinate (or sub-field) used to select matching files.
Definition uri.h:312
Basic date-time structure used throughout the Das1 & Das2 utilities.
Definition time.h:43
Das Time Utilities.
#define DURI_MAX_PATH
Maximum length of a rendered URI / file path.
Definition uri.h:208
DAS_API const DasUriSegDef * das_time_uridef(void)
Return the built-in coordinate definition for the time coordinate.
DAS_API DasErrCode das_range_fromTime(das_range *pRng, const das_time *tBeg, const das_time *tEnd)
Initialize a das_range for the "time" coordinate from das_time structs.
DAS_API DasErrCode das_range_fromUtc(das_range *pRng, const char *sBeg, const char *sEnd)
Initialize a das_range for the "time" coordinate from ISO-8601 UTC strings.
DAS_API DasErrCode das_range_fromDatum(das_range *pRng, const char *sCoord, const das_datum *dmBeg, const das_datum *dmEnd)
Initialize a das_range from pre-built das_datum values.
DasUriProto
Transport protocol detected from the leading scheme of a URI template.
Definition uri.h:401
DAS_API char ** das_uri_list(const char *sTemplate, const DasUriSegDef *pDef, int nRanges, const das_range *pRanges, size_t *pCount)
Collect all paths yielded by a template and coordinate ranges into a heap array.
DAS_API DasErrCode das_range_fromInt(das_range *pRng, const char *sCoord, int64_t nBeg, int64_t nEnd)
Initialize a das_range for a named integer coordinate or sub-field.