aboutsummaryrefslogtreecommitdiff
path: root/src/lib/hd24project.cpp
blob: 1111003ec895e85f72e529e5eb82531e93e2a6dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#define PROJDEBUG 0
#define PROJINFO_PROJECTNAME_8 0x0
#define PROJINFO_SONGCOUNT 0x0c
#define PROJINFO_LASTSONG 0x10
#define PROJINFO_SONGLIST 0x20
#define PROJINFO_PROJECTNAME 0x1b8
#define INVALID_SONGENTRY 0xFFFFFFFF
#define RESULT_SUCCESS 0
#define RESULT_FAIL 1
#include <hd24utils.h>
#include "memutils.h"
void hd24project::initvars(hd24fs* p_parent,__sint32 p_myprojectid)
{
	this->myprojectid=p_myprojectid;
	this->parentfs = p_parent;
	return;
}

__sint32 hd24project::projectid()
{
	return this->myprojectid;
}

hd24project::hd24project(hd24fs* p_parent, __sint32 p_myprojectid,
			__uint32 p_projectsector, const char* p_projectname,
			bool isnew) 
{
#if (PROJDEBUG == 1) 
	cout << "CONSTRUCT hd24project " << p_myprojectid << endl;
#endif
	// initialize a new project.
	// Boolean is obligatory to prevent accidents;
	// one can still create a project and not save it immediately.
	this->myprojectid = p_myprojectid;
	buffer = (unsigned char*)memutils::mymalloc("hd24project(1)",1024,1);
	parentfs = p_parent;
	if (!isnew) {
		p_parent->readsector(p_parent->devhd24,
			p_parent->getprojectsectornum(myprojectid),
			buffer); // fstfix follows
			
		p_parent->fstfix(buffer, 512);
		projectname(p_projectname);
	} else {
#if (PROJDEBUG == 1) 
		cout << "Create new project " <<projectname << " with id "
		     << p_myprojectid << " on sec " <<p_projectsector << endl;
#endif

		for (int i=0;i<1024;i++) {
			buffer[i]=0;
		}
		projectname(p_projectname);
		this->save(p_projectsector);		
	}
}

hd24project::hd24project(hd24fs* p_parent, __sint32 p_myprojectid) 
{
	// project id is 1-based
#if (PROJDEBUG == 1) 
	cout << "Here we are. time to create the project object for proj. "
	<< p_myprojectid << endl;
#endif
	this->myprojectid = p_myprojectid;
	buffer = (unsigned char*)memutils::mymalloc("hd24project(2)",1024,1);
	if (buffer==NULL) {
		return;
	}
	parentfs = p_parent;
	__uint32 projsecnum=p_parent->getprojectsectornum(myprojectid);
	if (projsecnum==0) {
		for (int i=0;i<512;i++) {
			buffer[i]=0;
		}
	} else {
		p_parent->readsector(p_parent->devhd24,
			projsecnum,
			buffer); // fstfix follows
			
		p_parent->fstfix(buffer, 512);
		//this->sort();
	}

}

hd24project::~hd24project() 
{
#if (PROJDEBUG == 1) 
	cout << "DESTRUCT hd24project " << myprojectid << endl;
#endif
	if (buffer != NULL) memutils::myfree("hd24project::buffer",buffer);
}

string* hd24project::projectname() 
{
	string* ver=parentfs->version();
	if (*ver == "1.00") 
	{
		delete ver;	
		// version 1.0 filesystem.
		string* tmp = new string("");
		string* dummy = Convert::readstring(buffer, PROJINFO_PROJECTNAME_8, 8);
		
		*tmp += *dummy;
		delete dummy;
                
		if (tmp->length() == 8) 
		{
			dummy = Convert::readstring(buffer, PROJINFO_PROJECTNAME_8 + 10, 2);
			*tmp += *dummy;
			delete dummy;
		}
		
		return tmp;
	}
	delete ver;	
	return Convert::readstring(buffer, PROJINFO_PROJECTNAME, 64);
}

void hd24project::projectname(string newname)
{
	hd24fs::setname(this->buffer,newname,PROJINFO_PROJECTNAME_8,PROJINFO_PROJECTNAME);	
	return;
}

unsigned long hd24project::maxsongs() 
{
	return parentfs->maxsongsperproject();
}

unsigned long hd24project::getsongsectornum(int i) 
{
	unsigned int songsec = Convert::getint32(buffer, 
		PROJINFO_SONGLIST + ((i - 1) * 4));
	// FIXME: Check for sensible values.		
	return songsec;
}

void hd24project::setsongsectornum(int i,__uint32 sector) 
{
	Convert::setint32(buffer, 
		PROJINFO_SONGLIST + ((i - 1) * 4),sector);
	// FIXME: Check for sensible values.		
	return;
}

void hd24project::lastsongid(signed long songid)
{
	if (songid<1)
	{
		return;

	}
	int maxsongsperproj = maxsongs();	
	if (songid>maxsongsperproj)
	{
		return;
	}
	if (songid==lastsongid())
	{
		/* same 'last song id' as before-
		   nothing changes.
		*/
		return;
	}
	Convert::setint32(buffer, PROJINFO_LASTSONG,getsongsectornum(songid));
	this->save();
	return;
}

signed long hd24project::lastsongid() 
{
	unsigned int lastsongsec = Convert::getint32(buffer, PROJINFO_LASTSONG);
	
	if (lastsongsec == 0)
	{
		return -1;
	}

	int maxsongsperproj = maxsongs();
	for (int i = 1; i <= maxsongsperproj; i++) 
	{
		unsigned int songsec = getsongsectornum(i);

		if (songsec == lastsongsec)
		{
			return i;
		}
	}
	
	if (maxsongsperproj >= 1)
	{
		return 1;
	}
	
	return -1;
}

__uint32 hd24project::songcount() 
{
	if (this==NULL)
	{
		return 0;
	}
	if (myprojectid == -1)
	{
		return 0;
	}
	if (buffer==NULL) {
		return 0;
	}
	__uint32 scount = Convert::getint32(buffer, PROJINFO_SONGCOUNT);
	
	if (scount > 99)
	{
		return 99;
	}
	
	return scount;
}

hd24song* hd24project::getsong(unsigned long songid) 
{
#if (PROJDEBUG == 1) 
	cout << "get song " << songid << endl;
#endif	
	return new hd24song(this,songid);
}

void hd24project::save(__uint32 projsector)
{
#if (PROJDEBUG == 1) 
	cout << "save project info to sector " << projsector << endl;
#endif
	if (buffer==NULL) 
	{
		return;	 // nothing to do!
	}
	parentfs->fstfix(buffer,512); // sector is now in native format again 
	parentfs->setsectorchecksum(buffer,0,projsector,1);     
	parentfs->writesectors(parentfs->devhd24,
			projsector,
			buffer,1);
	
	parentfs->fstfix(buffer,512); // sector is now in 'fixed' format
	parentfs->commit();
}

void hd24project::save()
{
	// This is capable of handling only 1-sector-per-project projects.
	// save(projsector) is only intended for new projects.
	__uint32 projsector=parentfs->getprojectsectornum(this->myprojectid);
#if (PROJDEBUG == 1) 
	cout << "save project info to sector " << projsector << endl;
#endif	
	if (projsector==0) 
	{
		return; // safety measure
	}
	save(projsector);
}

__uint32 hd24project::getunusedsongslot()
{
	// Find the first unused song slot in the current project.
	__uint32 unusedsongslot=INVALID_SONGENTRY;

	int maxsongcount=this->maxsongs();
	for (int j=1; j<=maxsongcount;j++) 
	{
		// get song sector info.
		__uint32 songsector = getsongsectornum(j);
		if (songsector==0) 
		{
			unusedsongslot=(j-1);
			break;
		}
	}
	
	return unusedsongslot;
}

hd24song* hd24project::createsong(const char* songname,__uint32 trackcount,
				  __uint32 samplerate)
{
	/* This creates a new song (with given songname)
	   on the drive (if possible).
           NULL is returned when unsuccessful, a pointer to the
           new song object otherwise.
        */
	__uint32 songslot=getunusedsongslot();
	if (songslot==INVALID_SONGENTRY) 
	{
		// project is full.
		return NULL;
	}

	// Project is not full so there must be unused song sectors
	// (if not, something seriously fishy is going on).
	__uint32 newsongsector=this->parentfs->getunusedsongsector();
	if (newsongsector==0)
	{
		// no songsector found (0 is not a valid songsector)
		return NULL;
	}

	// So, we have got a free songsector and a free song slot.
	unsigned char songbuf[512*7];
	for (int i=0;i<(7*512);i++) 
	{
		songbuf[i]=(unsigned char)(0);
	}
	hd24song::sectorinit(songbuf);	// put some default info in there

	hd24song::songname((unsigned char*)songbuf,songname);
	string* setsongname=hd24song::songname(parentfs,songbuf);
	delete setsongname;

	hd24song::samplerate((unsigned char*)songbuf,samplerate); 
	hd24song::logical_channels((unsigned char*)songbuf,trackcount) ;// depends on samplerate
	__uint32 songsector=newsongsector;

	// - Create the empty song at the songsector 
	// (2 sectors in size)
 	// - Create empty allocation info at songsector+2
	// (5 sectors in size)
	// write song info to drive

        parentfs->fstfix(songbuf,TOTAL_SECTORS_PER_SONG*512); // sector is now once again in native format
        parentfs->setsectorchecksum(songbuf,0,songsector,2);
        parentfs->setsectorchecksum(songbuf,2*512,songsector+2,5);
	parentfs->writesectors(parentfs->devhd24,songsector,songbuf,TOTAL_SECTORS_PER_SONG);
        parentfs->fstfix(songbuf,TOTAL_SECTORS_PER_SONG*512); // sector is now once again in fixed format

	/////////////////////////////
	// -Update the project slot info to point at that sector
        Convert::setint32(buffer,PROJINFO_SONGLIST+(songslot*4),songsector);
	this->lastsongid(songslot+1);
        Convert::setint32(buffer,PROJINFO_SONGCOUNT,
                Convert::getint32(buffer,PROJINFO_SONGCOUNT)+1);
	this->save();
	parentfs->refreshsongusage();

	////////////////////////
	// Now commit the FS
	parentfs->commit();
	
	return getsong(songslot+1);
}

__uint32 hd24project::deletesong(__uint32 songid) 
{
	hd24song* songtodel=this->getsong(songid);
#if (PROJDEBUG == 1)
	cout << "del song with id " << songid << endl;
#endif
	__uint32 songsector=getsongsectornum(songid);
#if (PROJDEBUG == 1)
	cout << "sector " << songsector << endl;
#endif
	songsector++; songsector--; // prevent 'variable not used' warning

	if (songtodel==NULL) 
	{
#if (PROJDEBUG == 1)
	cout << "song to del==null " << endl;
#endif
		return RESULT_FAIL;
	}
	songtodel->songlength_in_samples(0,false);
	songtodel->save();

	// delete song from project list by shifting left
	// all other songs...
	for (__uint32 i=songid;i<99;i++) 
	{
#if (PROJDEBUG == 1)
	cout << "set new sector for song id " << i <<" to " << getsongsectornum(i+1) << endl;
#endif
		setsongsectornum(i,getsongsectornum(i+1));
	}
	setsongsectornum(99,0); // ...and clearing the last entry.
	__uint32 scount = Convert::getint32(buffer, PROJINFO_SONGCOUNT);
	Convert::setint32(buffer,PROJINFO_SONGCOUNT,scount-1);
#if (PROJDEBUG == 1)
	cout << "set new song count to " << (scount-1) << endl;
#endif

	// set 'last accessed song' to first song in list
	// (if there are no songs anymore, this automatically
	// results in 'last song' to be on sector zero.
#if (PROJDEBUG ==1)
	cout << "set default project song to song 1 with sectornum " << getsongsectornum(1) << endl;
#endif
	Convert::setint32(buffer, PROJINFO_LASTSONG,getsongsectornum(1));
#if (PROJDEBUG ==1)
	cout << "save project." << endl;
#endif

	this->save();
#if (PROJDEBUG ==1)
	cout << "refresh song usage table (+superblock) " << endl;
#endif
	parentfs->refreshsongusage(); // or calcsongusage?
	
	unsigned char* sectors_driveusage=parentfs->getsectors_driveusage();

	// now, we still have the song object songtodel,
	// plus its original sector number.
#if (PROJDEBUG ==1)
	cout << "Reset song length" << endl;
#endif
#if (PROJDEBUG ==1)
	cout << "Unmark used clusters " << endl;
#endif
	songtodel->unmark_used_clusters(sectors_driveusage);
#if (PROJDEBUG ==1)
	cout << "Destruct song object." << endl;
#endif
	delete(songtodel);
#if (PROJDEBUG ==1)
	cout << "Save drive usage." << endl;
#endif
	parentfs->savedriveusage();	
#if (PROJDEBUG ==1)
	cout << "Commit FS" << endl;
#endif
	parentfs->commit();
	return RESULT_SUCCESS;
}

void hd24project::sort()
{
	// a max. of 99 elements is still doable by bubble sort.
	int songs=this->songcount();
	if (songs<2) return; // nothing to sort!
	string** songnames=(string**)memutils::mymalloc("hd24project::sort",99,sizeof(string*));
	hd24song* song1=NULL;

      	int i, j;
     	int arrayLength = songs;
	for (i=1;i<=arrayLength;i++)
	{
		song1=getsong(i);
		songnames[i-1]=song1->songname();
		delete song1;
		song1=NULL;
	}
	
      	for(i = 1; i <= arrayLength ; i++)
     	{
          	for (j=0; j < (arrayLength -1); j++)
         	{
			int compare=strncmp(songnames[j]->c_str(),songnames[j+1]->c_str(),64);
			if (compare>0)
              		{
				// swap entries
				__uint32 a=getsongsectornum(j+1);
				__uint32 b=getsongsectornum(j+2);
				setsongsectornum(j+2,a);
				setsongsectornum(j+1,b);
				string* tmp=songnames[j];
				songnames[j]=songnames[j+1];
				songnames[j+1]=tmp;
			}
		}
	}
	
	for (i=1;i<=arrayLength;i++)
	{
		free(songnames[i-1]);
	}
	free(songnames);
	return;
}