File: ScoreBundle.cpp

package info (click to toggle)
primrose 6%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,232 kB
  • ctags: 3,287
  • sloc: cpp: 27,302; php: 765; ansic: 636; objc: 272; makefile: 139; sh: 136; perl: 67
file content (98 lines) | stat: -rw-r--r-- 2,104 bytes parent folder | download | duplicates (5)
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
#include "ScoreBundle.h"

#include "minorGems/util/stringUtils.h"

#include <string.h>



ScoreBundle::ScoreBundle( char *inName, 
                          unsigned int inScore, unsigned int inSeed,
                          char *inMoveHistory )
    : mScore( inScore ), mSeed( inSeed ), 
      mMoveHistory( stringDuplicate( inMoveHistory ) ),
      mNumMoves( strlen( inMoveHistory ) ) {
    

    int nameLength = strlen( inName );

    if( nameLength > 8 ) {
        nameLength = 8;
        }
    
    memcpy( mName, inName, nameLength );
    mName[ nameLength ] = '\0';
    }



ScoreBundle::ScoreBundle( char *inEncoded )
        : mScore( 0 ), mSeed( 0 ), mMoveHistory( NULL ),
          mNumMoves( 0 ) {
    
    mName[0] = '\0';
    

    int numParts;
    
    char **parts = split( inEncoded, "#", &numParts );
    

    if( numParts == 4 ) {
        
        char *name = parts[0];
        

        int nameLength = strlen( name );
        
        if( nameLength > 8 ) {
            nameLength = 8;
            }
        
        memcpy( mName, name, nameLength );
        mName[ nameLength ] = '\0';
        
        
        int numRead = sscanf( parts[1],
                              "%u", &mScore );
        if( numRead != 1 ) {
            printf( "Failed to decode score bundle: %s\n", inEncoded );
            }
        
        numRead = sscanf( parts[2],
                          "%u", &mSeed );
        
        if( numRead != 1 ) {
            printf( "Failed to decode score bundle: %s\n", inEncoded );
            }
        

        mMoveHistory = stringDuplicate( parts[3] );
        mNumMoves = strlen( mMoveHistory );
        }
    else {
        printf( "Failed to decode score bundle: %s\n", inEncoded );
        }
    
    

    for( int i=0; i<numParts; i++ ) {
        delete [] parts[i];
        }
    delete [] parts;
    }

    
        
ScoreBundle::~ScoreBundle() {
    if( mMoveHistory != NULL ) {
        delete [] mMoveHistory;
        }
    }



ScoreBundle *ScoreBundle::copy() {
    return new ScoreBundle( mName, mScore, mSeed, mMoveHistory );
    }