2 Copyright 2015, The AROS Development Team. All rights reserved.
7 #include <aros/debug.h>
9 #include <proto/exec.h>
11 #include <exec/memory.h>
12 #include <aros/symbolsets.h>
16 #include "storage_intern.h"
18 //#include LC_LIBDEFS_FILE
20 int FindFirstDigit(char *inString
)
22 int inStr_len
= strlen(inString
);
25 for (i
= 0; i
< inStr_len
; i
++)
27 if ((inString
[i
] >= 0x30) && (inString
[i
] <= 0x39))
33 struct Storage_IDFamily
*FindIDFamily(char *ID
, struct StorageBase_intern
*StorageBase
)
35 struct Storage_IDFamily
*_IDFamiily
= NULL
;
38 firstDigit
= FindFirstDigit(ID
);
40 ForeachNode(&StorageBase
->libb_NameSpaceFamilies
, _IDFamiily
)
44 if ((strcmp(_IDFamiily
->SIDF_Node
.ln_Name
, ID
)) == 0)
49 if ((strlen(_IDFamiily
->SIDF_Node
.ln_Name
) == firstDigit
) && ((strncmp(_IDFamiily
->SIDF_Node
.ln_Name
, ID
, firstDigit
)) == 0))
56 /*****************************************************************************
60 AROS_LH1(struct Storage_IDNode
*, AllocateID
,
63 AROS_LHA(char *, ID
, A0
),
66 APTR
*, StorageBase
, 10, Storage
)
69 Allocate a unit/volume ID.
72 ID - base ID to allocate. If no unit is specified (e.g. ID = "CD"), a new
73 ID for that family will be allocated (e.g "CD0", "CD1", etc). If an ID unit _is_
74 specified, and it is allready allocated - a new ID will be created (e.g. "CD0_1")
88 ******************************************************************************/
92 struct Storage_IDFamily
*_IDFamiily
= NULL
;
93 struct Storage_IDNode
*_IDNode
= NULL
;
97 D(bug("[StorageRes] %s('%s')\n", __PRETTY_FUNCTION__
, ID
));
99 if ((_IDFamiily
= FindIDFamily(ID
, (struct StorageBase_intern
*)StorageBase
)) == NULL
)
101 if ((_IDFamiily
= AllocVec(sizeof(struct Storage_IDFamily
), MEMF_CLEAR
|MEMF_PUBLIC
)) != NULL
)
103 int baseNameLength
= strlen(ID
);
105 NEWLIST(&_IDFamiily
->SIDF_IDs
);
106 _IDFamiily
->SIDF_Node
.ln_Name
= AllocVec(baseNameLength
+ 1, MEMF_CLEAR
|MEMF_PUBLIC
);
107 CopyMem(ID
, _IDFamiily
->SIDF_Node
.ln_Name
, baseNameLength
);
108 AddTail(&((struct StorageBase_intern
*)StorageBase
)->libb_NameSpaceFamilies
, &_IDFamiily
->SDNF_Node
);
109 D(bug("[StorageRes] AllocateID: New FamilyNode @ 0x%p for '%s'\n", _IDFamiily
, _IDFamiily
->SIDF_Node
.ln_Name
));
113 D(bug("[StorageRes] AllocateID: ERROR - No Mem to allocate FamilyNode\n"));
118 D(bug("[StorageRes] AllocateID: using FamilyNode @ 0x%p (family '%s')\n", _IDFamiily
, _IDFamiily
->SIDF_Node
.ln_Name
));
120 #warning "TODO: We should use locking when manipulating the name list !"
122 ForeachNode(&_IDFamiily
->SIDF_IDs
, _IDNode
)
127 firstDigit
= FindFirstDigit(_IDNode
->SIDN_Node
.ln_Name
);
129 for (i
= 0; i
< (strlen(_IDNode
->SIDN_Node
.ln_Name
) - firstDigit
); i
++)
131 nodeValue
= nodeValue
* 10;
132 nodeValue
= nodeValue
+ (_IDNode
->SIDN_Node
.ln_Name
[firstDigit
+ i
] - 0x30);
134 //find the index of the value in the name
135 //convert it to a real value
136 if (nodeValue
>= freeValue
)
138 freeValue
= nodeValue
+1;
141 D(bug("[StorageRes] AllocateID: Free Device ID : %d\n", freeValue
));
145 if ((_IDNode
= AllocVec(sizeof(struct Storage_IDNode
), MEMF_CLEAR
|MEMF_PUBLIC
)) != NULL
)
147 int baseNameLength
= strlen(ID
);
148 int freeValue_len
= 1;
149 if (freeValue
>= 10) freeValue_len
++;
150 if (freeValue
>= 100) freeValue_len
++;
152 _IDNode
->SIDN_Node
.ln_Name
= AllocVec(baseNameLength
+ freeValue_len
+ 2, MEMF_CLEAR
|MEMF_PUBLIC
);
154 D(bug("[StorageRes] AllocateID: Name Node Allocated @ 0x%p, Name Storage @ 0x%p [%d bytes]\n", _IDNode
, _IDNode
->SIDN_Node
.ln_Name
, (baseNameLength
+ freeValue_len
+ 2)));
156 CopyMem(_IDFamiily
->SIDF_Node
.ln_Name
, _IDNode
->SIDN_Node
.ln_Name
, baseNameLength
);
157 int startnum
= freeValue
;
159 if (freeValue
>= 100)
161 _IDNode
->SIDN_Node
.ln_Name
[baseNameLength
+ count
] = (char)(0x30 + (freeValue
/ 100));
162 freeValue
= freeValue
- ((freeValue
/ 100) * 100);
167 _IDNode
->SIDN_Node
.ln_Name
[baseNameLength
+ count
] = (char)(0x30 + (freeValue
/ 10));
168 freeValue
= freeValue
- ((freeValue
/ 10) * 10);
171 _IDNode
->SIDN_Node
.ln_Name
[baseNameLength
+ count
] = (char)(0x30 + freeValue
);
172 freeValue
= startnum
;
174 _IDNode
->SIDN_Node
.ln_Name
[baseNameLength
+ count
] = 0x00;
176 Enqueue(&_IDFamiily
->SIDF_IDs
, &_IDNode
->SDNN_Node
);
177 D(bug("[StorageRes] AllocateID: Node '%s' Done\n", _IDNode
->SIDN_Node
.ln_Name
));
188 AROS_LH1(BOOL
, FreeID
,
189 AROS_LHA(struct Storage_IDNode
*, _IDNode
, A0
),
190 APTR
*, StorageBase
, 11, Storage
)
193 Free a device/volume ID.
210 ******************************************************************************/
214 struct Storage_IDFamily
*_IDFamiily
= NULL
;
216 D(bug("[StorageRes] %s('%s')\n", __PRETTY_FUNCTION__
, _IDNode
->SIDN_Node
.ln_Name
));