download _ParName.m
Language: ObjectiveC
Copyright: (c) 1988-1992, NeXT Computer, Inc. (c) 1999-2004 The MusicKit Project. (c) 1994 NeXT Computer, Inc. and reproduced under license from NeXT (c) 1994 Stanford University
LOC: 589
Project Info
The MusicKit(musickit)
Server: SourceForge
Type: cvs
...sicKit\Frameworks\MusicKit\
   _ArielQP.h
   _DSPMK.h
   _error.h
   _midi.h
   _MKAppProxy.h
   _MKAppProxy.m
   _MKNameTable.h
   _MKNameTable.m
   _MKParameter.h
   _MTCHelper.h
   _MTCHelper.m
   _musickit.h
   _noteRecorder.h
   _OrchloopbeginUG.h
   _OrchloopbeginUG.m
   _ParName.h
   _ParName.m
   _scorefile.h
   _ScorefileVar.h
   _ScorefileVar.m
   _SharedSynthInfo.h
   _SharedSynthInfo.m
   _synthElementMethods.m
   _time.h
   ArielQP.h
   ArielQP.m
   classFuncs.h
   ConductorPrivate.h
   dsp_types.h
   DSPSerialPortDevice.h
   dspwrap.h
   EnvelopePrivate.h
   equalTempered.m
   errors.h
   fastFFT.c
   fastFFT.h
   GNUmakefile.postamble
   GNUmakefile.preamble.in
   InstrumentPrivate.h
   keynums.h
   libMusicKit.def
   Localized.strings
   make.sh
   Makefile.postamble
   Makefile.preamble
   midi_spec.h
   midifile.h
   midifile.m
   MidiPrivate.h
   midiTranslation.h
   MKConductor.h
   MKConductor.m
   MKConductorDelegate.h
   MKDeviceStatus.h
   MKEnvelope.h
   MKEnvelope.m
   MKFilePerformer.h
   MKFilePerformer.m
   MKFileWriter.h
   MKFileWriter.m
   MKInstrument.h
   MKInstrument.m
   MKMidi.h
   MKMidi.m
   MKMixerInstrument.h
   MKMixerInstrument.m
   MKMTCPerformer.h
   MKMTCPerformer.m
   MKNote.h
   MKNoteFilter.h
   MKNoteFilter.m
   MKNoteReceiver.h
   MKNoteReceiver.m
   MKNoteSender.h
   MKNoteSender.m
   MKOrchestra.h
   MKPart.m
   MKPartials.h
   MKPartials.m
   ...tialsWaveshapingTable.m
   MKPartPerformer.h
   MKPartPerformer.m
   MKPartRecorder.h
   MKPartRecorder.m
   MKPatch.h
   MKPatch.m
   MKPatchConnection.h
   MKPatchConnection.m
   MKPatchEntry.h
   MKPatchEntry.m
   MKPatchTemplate.h
   MKPatchTemplate.m
   MKPerformer.h
   MKPerformer.m
   MKPerformerDelegate.h
   MKPlugin.h
   MKSamplerInstrument.h
   MKSamplerInstrument.m
   MKSamples.h
   MKScore.h
   MKScore.m
   MKScorefileObject.h
   MKScorefilePerformer.h
   MKScorefilePerformer.m
   MKScorefileWriter.m
   MKScorePerformer.h
   MKScorePerformer.m
   MKScoreRecorder.h
   MKScoreRecorder.m
   MKSynthData.h
   MKSynthData.m
   MKSynthInstrument.h
   MKSynthPatch.h
   MKTimbre.h
   MKTimbre.m
   MKTuningSystem.h
   MKTuningSystem.m
   MKUnitGenerator.h
   MKUnitGenerator.m
   MKWaveTable.h
   MKWaveTable.m
   mtcMidi.m
   mtcMidiPrivate.m
   MusicKit-Info.plist
   MusicKit.h
   MusicKitConfig.h.in
   names.h
   noDVal.h
   noteDispatcherMethods.m
   NotePrivate.h
   noteRecorderCFuncs.m
   noteRecorderMethods.m
   orch.h
   OrchestraPrivate.h
   OrchloopbeginUG.h
   OrchloopbeginUG.m
   orchloopbeginUGInclude.m
   params.h
   parNames.m
   partialsDBInclude.m
   PartialsPrivate.h
   PartPerformerPrivate.h
   PartPrivate.h
   PartRecorderPrivate.h
   PatchTemplatePrivate.h
   PB.project
   PerformerPrivate.h
   pitches.h
   platform.make
   ...efilePerformerPrivate.h
   ScorePerformerPrivate.h
   ScorePrivate.h
   ScoreRecorderPrivate.h
   SynthDataPrivate.h
   synthElementCFuncs.m
   synthElementMethods.m
   SynthInstrumentPrivate.h
   SynthPatchList.h
   SynthPatchList.m
   SynthPatchPrivate.h
   timetagInclude.m
   timeunits.h
   tokenNames.m
   tokens.h
   trigonometry.c
   trigonometry.h
   TuningSystemPrivate.h
   UnitGeneratorPrivate.h
   win32-def.top
   writeBinary.m
   writeMidi.m
   writeScore.m

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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/*
 $Id: _ParName.m,v 1.20 2006/02/05 17:57:10 leighsmith Exp $
 Defined In: The MusicKit

 Description:
   See _ParName.h
 
 Copyright (c) 1988-1992, NeXT Computer, Inc.
 Portions Copyright (c) 1994 NeXT Computer, Inc. and reproduced under license from NeXT
 Portions Copyright (c) 1994 Stanford University
 Portions Copyright (c) 1999-2004 The MusicKit Project.
 */
/*
 Modification history prior to commit to CVS:
 
 09/18/89/daj - Changes to accomodate new way of doing parameters (structs
                                                                   rather than objects).
 09/22/89/daj - Changes to accomodate changes in _MKNameTable.
 parameter.m:
 09/22/89/daj - Added _MK_BACKHASH bit or'ed in with type when adding name,
 to accommodate new way of handling back-hashing.
 10/06/89/daj - Type changes for new _MKNameTable implementation.
 10/15/89/daj - Merged _ParName.m and parameter.m and flushed global functions
 that are no longer needed.
 10/20/89/daj - Added binary scorefile support.
 Changed writeData() to give anonymous data a default name
 of the form class-name# where # is an integer.
 02/01/90/daj - Fixed trace message.
 Fixed bug in anonymous data naming.
 Added comments.
 03/05/90/daj - Added _MKSymbolize() call in _MKGetPar() to insure that
 parameter names are valid scorefile symbols. Changed
 scorefile string-printing routine (_MKParWriteStdValueOn)
 to encode chars such as \n as character escape codes.
 03/21/90/daj - Small changes to quiet -W compiler warnings.
 08/13/90/daj - Added _MKParNameStr().
 09/02/90/daj - Changed MAXDOUBLE references to noDVal.h way of doing things
 10/04/92/daj - Fixed bug that would cause a crash if you try and write a
 parameter with a nil value (a rather bizarre thing to do, admitedly).
 Currently, I'm writing a zero now.  This is not quite right either.
 I should probably define a Scorefile symbol "nil".
 1/30/96/daj -  Changed _MKParWriteStdValueOn to use _MKParAsStringNoCopy().
 Also changed default of _MKParAsStringNoCopy to do no copy.
 */
#import <ctype.h>

#define MK_INLINE 1
#import "_musickit.h"
#import "tokens.h"
#import "TuningSystemPrivate.h"
#import "_error.h"
#import "_MKNameTable.h"
#import "PartialsPrivate.h"
#import "MKEnvelope.h"
#import "_ParName.h"
/* First, here's how parameter names are represented: */

@implementation _ParName

#define BINARY(_p) (_p && _p->_binary) /* writing a binary scorefile? */

static id newParName(const NSString * name,int param)
/* Make a new one */
{
  register _ParName *self = [_ParName new];
  self->s = [(const id)name retain]; // no need to make copy?
  _MKNameGlobal((const id)name,self,_MK_param | _MK_BACKHASHBIT,YES,YES);
  self->par = param;
  self->printfunc = NULL;
  return self;
}

unsigned _MKGetParNamePar(_ParName *self)
/* Return parameter */
{
  return self->par;
}


/* Here's a lot of initialization. */

static _ParName **parIds = NULL; /* Par name array. */

/* Define parameter names */
#import "parNames.m"

static void initParNames(unsigned int fromPar, unsigned int toPar)
/* Initialize all NeXT parameters */
{
  register unsigned i;
  register _ParName **parId = &(parIds[fromPar]);
  const NSString * *parNamePtr = (const NSString **) &(parNames[fromPar]);
  for (i = fromPar; i < toPar; i++)
    *parId++ = newParName((NSString *) *parNamePtr++, i);
}

static BOOL keywordsPrintfunc(_MKParameter *parameter, NSMutableData *aStream, _MKScoreOutStruct *p)
/* Used to write parameters with keyword-valued arguments. */
{
  int i = _MKParAsInt(parameter);
  if (BINARY(p))
    _MKWriteIntPar(aStream,i);
  else if (!_MK_VALIDTOKEN(i))
    [aStream appendData:[[NSString stringWithFormat:@"%d", i] dataUsingEncoding:NSNEXTSTEPStringEncoding]];
  else [aStream appendBytes:_MKTokName(i) length:strlen(_MKTokName(i))];
  return YES;
}

/* For keeping track of application-defined parameters: */
static int parArrSize = (int)MK_appPars;
static int highestPar = _MK_FIRSTAPPPAR - 1;

id _MKParNameObj(int aPar)
/* Returns _ParName object of specified parameter. aPar must be a valid
parameter */
{
  return parIds[aPar];
}

NSString *_MKParNameStr(int aPar)
/* Returns _ParName object of specified parameter. aPar must be a valid
parameter */
{
  return parIds[aPar]->s;
}

int MKHighestPar()
/* Returns the number of the most recently-created parameter name */
{
  return highestPar;
}

int _MKGetPar(NSString *aName,_ParName **aPar)
/* Gets parameter name, installing if necessary. Also returns _ParName
object by reference. */
{
  unsigned short type;
  if (aName == nil) return MK_noPar;
  if (![aName cStringLength]) {
    return MK_noPar; /* DAJ - Jan 27, 96 */
  }
  *aPar = _MKGetNamedGlobal(aName,&type);
  if (*aPar)
    return (int)_MKGetParNamePar(*aPar);
  else {
    /* Allocates a new parameter number and sets obj's instance variable
	   to that number. */
#       define EXPANDAMT 5
    BOOL wasChanged;
    aName = _MKSymbolize(aName,&wasChanged); /* Make valid sf symbol */
    *aPar = newParName(aName,++highestPar);
    if (wasChanged) /* _MKSymbolize copied the string */
      [aName autorelease];//sb: was free(aName);
      if (highestPar >= parArrSize) {
        _MK_REALLOC(parIds, _ParName *, (highestPar + EXPANDAMT));
        parArrSize = highestPar + EXPANDAMT;
      }
      parIds[highestPar] = *aPar;
      if (MKIsTraced(MK_TRACEPARS))
        NSLog(@"Adding new parameter %@\n",(*aPar)->s);
      return highestPar;
  }
}

static void initSyns()
/* Install synonyms. */
{
    register unsigned int i;
    register const NSString * *parNamePtr = (const NSString **) &(parSynNames[0]);
    register int *parSynPtr = (int *) &(parSyns[0]);
    _ParName *oldObj, *newObj;
    
    for (i = 0; i < SYNS; i++) {
	oldObj = _MKParNameObj(*parSynPtr);
	newObj = newParName((NSString *) *parNamePtr++, *parSynPtr++);
	newObj->printfunc = oldObj->printfunc;
	/* Same printfunc */
    }
}


#define INT(_x) ((int)_x)

BOOL
_MKParInit(void)
/* Sent once from Note. Returns YES. Initializes the world */
{
  _MK_CALLOC(parIds, id, _MK_maxPrivPar);
  initParNames(MK_noPar, MK_privatePars);
  initParNames(MK_privatePars + 1, _MK_maxPrivPar);
  initSyns();
  parIds[INT(MK_keyNum)]->printfunc = _MKKeyNumPrintfunc;
  parIds[INT(MK_freq)]->printfunc = _MKFreqPrintfunc;
  parIds[INT(MK_freq0)]->printfunc = _MKFreqPrintfunc;
  parIds[INT(MK_chanMode)]->printfunc = keywordsPrintfunc;
  parIds[INT(MK_sysRealTime)]->printfunc = keywordsPrintfunc;
  return YES;
}


/* Primitives for creating, freeing and copying _MKParameters */

#define CACHESIZE 100
static _MKParameter * _cache[100]; /* Avoid unnecessary malloc/free */
static unsigned _cachePtr = 0;

static _MKParameter * newPar(parNum)
int parNum;
/* Returns new _MKParameter struct */
{
  register _MKParameter *param;
  // we assume that the cached param has been previously nullified
  if (_cachePtr > 0)
    param = _cache[--_cachePtr];
  else _MK_CALLOC(param,_MKParameter,1);
  param->parNum = parNum;
  return param;
}

static inline void releaseParIfNecessary(_MKParameter *param)
{
  switch (param->_uType) {
      case MK_string:
      case MK_envelope:
      case MK_waveTable:
      case MK_object:
        [param->_uVal.symbol release];
      default:
      ;
  }
}

#define DEBUG_CACHE 0

_MKParameter *_MKFreeParameter(_MKParameter *param)
/* Frees object and string datum, if any. */
{
  if (!param)
    return NULL;
#   if DEBUG_CACHE
  {
    int i;
    for (i=0; i<_cachePtr; i++)
      if (_cache[i] == param) {
        NSLog(@"Attempt to free freed parameter.\n");
        return NULL;
      }
  }
#   endif
    releaseParIfNecessary(param);
    param->_uVal.symbol = nil; // it's a union, so this does the int and double too.
    if (_cachePtr < CACHESIZE)
      _cache[_cachePtr++] = param;
    else {
      free(param);
      param = NULL;
    }
    return NULL;
}

BOOL _MKIsParPublic(_MKParameter *param)
/* Returns whether this is a public parameter. */
{
  return _MKIsPar(parIds[param->parNum]->par);
}

_MKParameter *_MKCopyParameter(_MKParameter *param)
/* Creates a copy of the _MKParameter */
{
  _MKParameter *newOne;
  if (!param)
    return NULL;
  newOne = newPar(param->parNum);
  newOne->_uVal = param->_uVal;
  newOne->_uType = param->_uType;
  switch (param->_uType) {
      case MK_string:
      case MK_envelope:
      case MK_waveTable:
      case MK_object:
        [newOne->_uVal.symbol retain];
      default:
      ;
  }
return newOne;
}


/* All of the following return a new _MKParameter struct initialized as
specified */

_MKParameter * _MKNewDoublePar(double value, int parNum)
{
  register _MKParameter *param = newPar(parNum);
  param->_uType = MK_double;
  param->_uVal.rval = value;
  return param;
}

_MKParameter * _MKNewStringPar(NSString * value, int parNum)
{
  register _MKParameter *param = newPar(parNum);
  param->_uType = MK_string;
  param->_uVal.sval = [value retain]; // no need to release old val as we assume nil
    return param;
}

_MKParameter * _MKNewIntPar(int value, int parNum)
{
  register _MKParameter *param = newPar(parNum);
  param->_uType = MK_int;
  param->_uVal.ival = value;
  return param;
}

_MKParameter * _MKNewObjPar(id value, int parNum, _MKToken type)
{
  register _MKParameter *param = newPar(parNum);
  param->_uType = type;
  param->_uVal.symbol = [value retain]; // no need to release old val as we assume nil
  return param;
}


/* All of the following set the parameter struct to the value indicated */

_MKParameter * _MKSetDoublePar(_MKParameter * param, double value)
/* Set the Parameter to type and double and assign doubleVal. */
{
  releaseParIfNecessary(param);
  param->_uType = MK_double;
  param->_uVal.rval = value;
  return param;
}

_MKParameter * _MKSetIntPar(_MKParameter *param, int value)
/* Set the Parameter to type int and assign intVal. */
{
  releaseParIfNecessary(param);
  param->_uType = MK_int;
  param->_uVal.ival = value;
  return param;
}

_MKParameter * _MKSetStringPar(_MKParameter *param, NSString *value)
/* Set the Parameter to type string and add a reference to stringVal. */
{
  releaseParIfNecessary(param);
  param->_uVal.sval = [value retain];
  param->_uType = MK_string;
  return param;
}

_MKParameter * _MKSetObjPar(_MKParameter *param, id value, _MKToken type)
/* Sets obj field and type */
{
  releaseParIfNecessary(param);
  param->_uVal.symbol = [value retain];
  param->_uType = type;
  return param;
}

static double firstEnvValue(param)
_MKParameter *param;
/* If receiver is not type MK_envelope, returns MK_NODVAL. Otherwise,
returns the first value of the envelope multiplied by yScale plus
yOffset. If the receiver is envelope typed but the object is not
an envelope, returns MK_NODVAL. */
{
  double rtn,dummy,dummy2;
  id env;
  if (param->_uType == MK_envelope)
    env = param->_uVal.symbol;
  else return MK_NODVAL;
  if ((![env isKindOfClass:_MKClassEnvelope()]) ||
      ([env getNth:0 x:&dummy y:&rtn smoothing:&dummy2] <MK_noEnvError))
    return MK_NODVAL;
  return rtn;
}

int _MKParAsInt(_MKParameter *param)
/* Get the current value as an integer. If the receiver is envelope-valued,
returns truncated firstEnvValue(param). If the receiver is
waveTable-valued, returns MAXINT.
*/
{
  switch (param->_uType) {
    case MK_double:
      return ((int) param->_uVal.rval);
    case MK_string:
      return (_MKStringToInt(param->_uVal.sval));
    case MK_int:
      return param->_uVal.ival;
    case MK_envelope:
      return (int)firstEnvValue(param);
    default:
      break;
  }
  return MAXINT;
}

double _MKParAsDouble(_MKParameter *param)
/* Get the current value as a double. If the receiver is envelope-valued,
returns firstEnvValue(param). If the receiver is waveTable-valued
returns MK_NODVAL. */
{
  switch (param->_uType) {
    case MK_double:
      return param->_uVal.rval;
    case MK_string:
      return (_MKStringToDouble(param->_uVal.sval));
    case MK_int:
      return ((double) param->_uVal.ival);
    case MK_envelope:
      return (double)firstEnvValue(param);
    default:
      break;
  }
  return MK_NODVAL;
}

NSString * _MKParAsString(_MKParameter *param)
/* Returns a copy of the datum as a string. If the receiver is envelope-
valued, returns a description of the envelope. */
{
  switch (param->_uType) {
    case MK_double:
      return _MKDoubleToString(param->_uVal.rval);
    case MK_int:
      return _MKIntToString(param->_uVal.ival);
    case MK_string:
      return param->_uVal.sval;//sb: was _MKMakeStr(param->_uVal.sval);
    case MK_envelope:
      return [param->_uVal.symbol description];//@" (envelope) ";//sb: was _MKMakeStr(" (envelope) "); /*** FIXME ***/
    case MK_waveTable:
      return [param->_uVal.symbol description];//@" (waveTable) ";//sb: was _MKMakeStr(" (waveTable) ");  /*** FIXME ***/
    default:
      return [param->_uVal.symbol description];// NSStringFromClass([param->_uVal.symbol class]);//sb: was _MKMakeStr([NSStringFromClass([param->_uVal.symbol class]) cString]); /*** FIXME ***/
  }
  return @"";//sb: was _MKMakeStr("");
}

NSString * _MKParAsStringNoCopy(_MKParameter *param)
/* Returns the value as a string , but does
not copy that string. Application should not
delete the returned value and should not rely on its
staying around for longer than the Parameter object.
This method is provided as a speed optimzation.
*/
{
  switch (param->_uType) {
    case MK_double:
      return _MKDoubleToStringNoCopy(param->_uVal.rval);
    case MK_int:
      return _MKIntToStringNoCopy(param->_uVal.ival);
    case MK_string:
      return param->_uVal.sval;
    case MK_envelope:
      return [param->_uVal.symbol description];//@" (envelope) ";   /*** FIXME ***/
    case MK_waveTable:
      return [param->_uVal.symbol description];//@" (waveTable) ";  /*** FIXME ***/
    default:
      return [param->_uVal.symbol description];//(NSString *)NSStringFromClass([param->_uVal.symbol class]); /*** FIXME ***/
  }
  return @"";
}

id _MKParAsEnv(_MKParameter *param)
/* Returns envelope if any, else nil. The envelope is not copied. */
{
  if (param->_uType == MK_envelope)
    return param->_uVal.symbol;
  return nil;
}

id _MKParAsObj(_MKParameter *param)
/* If type is MK_envelope, MK_waveTable or MK_object, returns the object.
Otherwise, returns nil. */
{
  switch (param->_uType) {
    case MK_envelope:
    case MK_waveTable:
    case MK_object:
      return param->_uVal.symbol;
    default:
      return nil;
  }
}

id _MKParAsWave(_MKParameter *param)
/* Returns waveTable if any, else nil. The waveTable is not copied. */
{
  if (param->_uType == MK_waveTable)
    return param->_uVal.symbol;
  else return nil;
}

_MKParameterUnion *_MKParRaw(_MKParameter *param)
/* Returns the raw _MKParameterUnion *. */
{
  return &param->_uVal;
}


/* The following is used for printing parameters to scorefiles and such */

static void parNameWriteValueOn(_ParName *parNameObj,NSMutableData *aStream,
                                _MKParameter *aVal,_MKScoreOutStruct *p)
{
  if (!parNameObj->printfunc || (!parNameObj->printfunc(aVal,aStream,p)))
    _MKParWriteStdValueOn(aVal,aStream,p);
}

void _MKParWriteOn(_MKParameter *param,NSMutableData *aStream,
                   _MKScoreOutStruct *p)
/* Called by Note's _MKWriteParameters() */
{
  register _ParName *self = parIds[param->parNum];
  if (_MKIsPrivatePar(self->par))
    return;
  if (BINARY(p)) {
    BOOL appPar = (self->par >= (int)(_MK_FIRSTAPPPAR));
    if (appPar)
      _MKWriteShort(aStream,MK_appPars);
    else _MKWriteShort(aStream,self->par);
    parNameWriteValueOn(self,aStream,param,p);
    if (appPar)
      _MKWriteNSString(aStream,self->s);
  }
  else {
    [aStream appendData:[[NSString stringWithFormat:@"%@:", self->s] dataUsingEncoding:NSNEXTSTEPStringEncoding]];
    parNameWriteValueOn(self,aStream,param,p);
  }
}

void _MKParWriteValueOn(_MKParameter *param,NSMutableData *aStream,
                        _MKScoreOutStruct *p)
/* Private function used by ScorefileVars to write their values */
{
  parNameWriteValueOn(parIds[param->parNum],aStream,param,p);
}

/*
 Strategy for writing objects:
 If we're not writing a scorefile, just print object definition.
 If a name isn't in there and the obj isn't named, name it.
 If a name isn't in the local table and the obj is named,
 it needs to have a decl generated.
 If a name is in there and the obj's name is not pointing to the same object,
 make a new name to point to the new obj.
 */

static NSString *genAnonName(id obj)
/* Generate default anonymous name */ /*sb: as a temporary NSString */
{
  //    return _MKMakeStrcat([NSStringFromClass([obj class]) cString],"1");
  return [NSString stringWithFormat:@"%@1",NSStringFromClass([obj class])];
}

static void writeObj(id dataObj,NSMutableData *aStream,_MKToken declToken,BOOL
                     binary)
{
  if (!binary)
    [aStream appendBytes:"[" length:1];
  if (declToken == MK_object) {
    if (![dataObj respondsToSelector:@selector(writeASCIIStream:)]) {
      MKErrorCode(MK_notScorefileObjectTypeErr, NSStringFromClass([dataObj class]));
      if (binary)
        _MKWriteChar(aStream,'\0');
      else [aStream appendBytes:"]" length:1];
      return;
    }
    [aStream appendData:[[NSString stringWithFormat:@"%@ ", NSStringFromClass([dataObj class])] dataUsingEncoding:NSNEXTSTEPStringEncoding]];
    [dataObj writeASCIIStream:aStream];
  }
  else
    if (binary)
      [dataObj writeBinaryScorefileStream: aStream];
  else {
    [dataObj writeScorefileStream:aStream];
    [aStream appendBytes:"]" length:1];
  }
}

static void writeData(NSMutableData *aStream,_MKScoreOutStruct *p, id dataObj,int type)
{
  id hashObj;
  NSString * name;
  BOOL binary = BINARY(p);
  unsigned short tmp;
  _MKToken declToken;
  switch (type) {
    case MK_envelope:
      declToken = _MK_envelopeDecl;
      break;
    case MK_waveTable:
      declToken = _MK_waveTableDecl;
      break;
    default:
      declToken = _MK_objectDecl;
      break;
  }
  if (binary) {
    int val = (int)NSMapGet(p->_binaryIndecies, dataObj);
    if (val) {
      _MKWriteShort(aStream,type);
      _MKWriteShort(aStream,val);
      return;
    }
  }
  if (!p) {         /* We're not writing a scorefile so don't give
			 it a name. */
    if (binary) {
      _MKWriteShort(aStream,type);
      _MKWriteChar(aStream,'\0');
    }
    writeObj(dataObj,aStream,declToken,binary);
    return;
  }
  name = (NSString *)MKGetObjectName(dataObj);
  if (!name) {
    name = genAnonName(dataObj);
    MKNameObject(name,dataObj);
    name = (NSString *)MKGetObjectName(dataObj);
  }
  /* If we've gotten here, it's named and we're writing a scorefile. */
  hashObj = _MKNameTableGetObjectForName(p->_nameTable,name,nil,&tmp);
  if (hashObj && (hashObj != dataObj)) {     /* Resolve name collisions. */
    name = _MKUniqueName(name,p->_nameTable,dataObj,&hashObj); /*sb: was _MKMakeStr(name) */
  }
  if (hashObj == dataObj)          /* It's already declared in file. */
    [aStream appendData:[name dataUsingEncoding:NSNEXTSTEPStringEncoding]];        /* Just write name.
      (If we got here, we must be
      writing an ascii file) */
  else {                           /* It's not been declared yet. */
   if (binary) {
	    _MKWriteShort(aStream,declToken);
	    _MKWriteNSString(aStream,name);
      NSMapInsert(p->_binaryIndecies,dataObj,(void *)(++(p->_highBinaryIndex)));
   }
   else {
     [aStream appendData: [[NSString stringWithFormat:@"%s %@ = ", _MKTokName(declToken),name]
                                    dataUsingEncoding: NSNEXTSTEPStringEncoding]];
   }
   writeObj(dataObj,aStream,declToken,binary);
   _MKNameTableAddName(p->_nameTable,name,nil,dataObj,
                     type | _MK_BACKHASHBIT,YES);
  }
}


static void sfWriteStrPar(register NSMutableData *aStream, NSString *strBuf)
/* Print str to aStream such that, when read in again as a scorefile,
it will look identical. */
{
  char c;
  const char *tmp;
  const register char *str = [strBuf cString];
#   define WRITECHAR(_c) c = (_c); [aStream appendBytes:&c length:1]
  WRITECHAR('"');
  for (tmp = str; *str; str++)
    switch (*str) {
      case BACKSPACE:  case FORMFEED:  case NEWLINE:  case CR:  case TAB:
      case VT:  case BACKSLASH:  case QUOTE:  case '"':
        [aStream appendBytes:tmp length:str - tmp]; /* Write string up to here. */
        tmp = str + 1;                /* update tmp */
        /* Translate char. */
        WRITECHAR(BACKSLASH);
        WRITECHAR(*(char *)(strrchr((char *)_MKTranstab(),*str)-1));
        break;
      default:
        break;
    }
  [aStream appendBytes:tmp length:str - tmp];  /* Write remainder of string. */
  WRITECHAR('"');
}

void _MKParWriteStdValueOn(_MKParameter *param,NSMutableData *aStream,
                           _MKScoreOutStruct *p)
/* Private method that writes value in a standard way.
If the_ParName for this parameter has a non-NULL print function,
that function is used instead. That function may call
_MKParWriteStdValueOn, if desired. */
{
  switch (param->_uType) {
    case MK_double:
      if (BINARY(p)) {
        _MKWriteDoublePar(aStream,_MKParAsDouble(param));
      }
      else {
        [aStream appendData:[[NSString stringWithFormat:@"%.5f", _MKParAsDouble(param)]
	                              dataUsingEncoding:NSNEXTSTEPStringEncoding]];
      }
      break;
    case MK_string:
      if (BINARY(p))
        _MKWriteStringPar(aStream,_MKParAsStringNoCopy(param));
      else sfWriteStrPar(aStream,_MKParAsStringNoCopy(param));
      break;
    case MK_envelope:
    case MK_waveTable:
    case MK_object:
      if (param->_uVal.symbol)
        writeData(aStream,p,param->_uVal.symbol,param->_uType);
      else /* parameter with value of nil! Write it as 0.  This is not
        * really the right thing.  Should have a scorefile symbol
        * "nil".   FIXME
        */
        if (BINARY(p))
          _MKWriteIntPar(aStream,0);
      else [aStream appendBytes:"0" length:1];
      break;
    default:
    case MK_int:
      if (BINARY(p))
        _MKWriteIntPar(aStream,_MKParAsInt(param));
      else [aStream appendData:[[NSString stringWithFormat:@"%d", _MKParAsInt(param)] dataUsingEncoding:NSNEXTSTEPStringEncoding]];
      break;
  }
}


void _MKArchiveParOn(_MKParameter *param,NSCoder *aTypedStream) /*sb: originally ocnverted as NSArchiver */
{
// note: the parameter name was originally a cstring (type "*"). If versionning can
// be used here then it would be good to catch this.

  BOOL isMKPublicPar = (param->parNum<(int)(MK_privatePars));
  /*  Version3 and earlier: (param->parNum<(int)(_MK_FIRSTAPPPAR)) */
  [aTypedStream encodeValueOfObjCType:"c" at:&isMKPublicPar];
  if (isMKPublicPar) /* Write parameter number */
    [aTypedStream encodeValueOfObjCType:"s" at:&param->parNum];
  else        /* Write parameter name */
    [aTypedStream encodeValueOfObjCType:"@" at:&(parIds[param->parNum]->s)];
  [aTypedStream encodeValueOfObjCType:"s" at:&param->_uType];
  switch (param->_uType) {
    case MK_double:
      [aTypedStream encodeValueOfObjCType:"d" at:&param->_uVal.rval];
      break;
    case MK_string:
      [aTypedStream encodeValueOfObjCType:"@" at:&param->_uVal.sval];//sb: type was "%"
      break;
    case MK_envelope:
    case MK_waveTable:
    case MK_object:
      [aTypedStream encodeValueOfObjCType:"@" at:&param->_uVal.symbol];
      break;
    default:
    case MK_int:
      [aTypedStream encodeValueOfObjCType:"i" at:&param->_uVal.ival];
      break;
  }
}

void _MKUnarchiveParOn(_MKParameter *param,NSCoder *aTypedStream) /*sb: NSCoder originally converted as NSArchiver */
{
  BOOL isMKPublicPar;
  NSString *strVar;
  [aTypedStream decodeValueOfObjCType:"c" at:&isMKPublicPar];
  /* See fix for bug in MKNote.m's read: method */
  if (isMKPublicPar) /* Write parameter number */
    [aTypedStream decodeValueOfObjCType:"s" at:&param->parNum];
  else {       /* Write parameter name */
    id aParNameObj;
    [aTypedStream decodeValueOfObjCType:"@" at:&strVar];
    param->parNum = _MKGetPar(strVar,&aParNameObj);
    [strVar release];
    strVar = nil;
  }
  [aTypedStream decodeValueOfObjCType:"s" at:&param->_uType];
  switch (param->_uType) {
    case MK_double:
      [aTypedStream decodeValueOfObjCType:"d" at:&param->_uVal.rval];
      break;
    case MK_string:
      [aTypedStream decodeValueOfObjCType:"@" at:&param->_uVal.sval];//sb: type was "%"
      break;
    case MK_envelope:
    case MK_waveTable:
    case MK_object:
      [aTypedStream decodeValueOfObjCType:"@" at:&param->_uVal.symbol];
      break;
    default:
    case MK_int:
      [aTypedStream decodeValueOfObjCType:"i" at:&param->_uVal.ival];
      break;
  }
}

@end

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us