PrevNextUpHome SophiaFramework UNIVERSE 5.3

13.8. Sample code

13.8.1. Processing the CSV File

The code to read data in the CSV(Comma Separated Values) format from the file(SFXFile) and then store them into the array(SFXArray) is as follows.

Reference: SFXFile

Example 13.7. Example of CVS Data ("/data.csv")

6,8,15,21,4,5

Example 13.8. Reading CSV Data from File into Array

SFCError error;                    // error value
SFXFile file;                      // file which is in the CVS format
SFXAnsiStringStreamReader reader;  // input stream of file
SFXAnsiString stringFromFile;      // variable into which data read from file will be stored
SFXAnsiString csvElement;
SInt32 startOfComma, endOfComma;   // position of comma
SInt32 sum;                        // sum of the elements
SFXArray<SInt32> array;            // array to store elements of SInt32
SFXArray<SInt32>::Enumerator etor; // enumerator of array

// open the file in read-only mode
if ((error = file.OpenReadOnly(SFXPath("/data.csv"))) == SFERR_NO_ERROR) {

    // get the input stream (buffer size: 1024)
    if ((error = file.GetStreamReader(1024, &reader)) == SFERR_NO_ERROR) {

        // fetch
        if ((error = reader.Fetch()) == SFERR_NO_ERROR) {

            // read data from the input stream into stringFromFile
            if ((error = reader.ReadSFXAnsiString(&stringFromFile))
                == SFERR_NO_ERROR) {

                startOfComma = 0;

                // seek the position of comma
                endOfComma = stringFromFile.FirstIndexOf(',');

                while (endOfComma >= 0) {   // repeat as long as comma is found
                    
                    // get the string between two commas
                    csvElement = stringFromFile.Substring(startOfComma, endOfComma);

                    // convert string into number and store it into array
                    if ((error = array.InsertLast(csvElement.AsSInt32()))
                        != SFERR_NO_ERROR) { // error has occured
                        break;
                    }

                    // set startOfComma to seek next comma
                    startOfComma = endOfComma + 1;
                    // seek the position of comma starting from startOfComma
                    endOfComma = stringFromFile.FirstIndexOf(',', startOfComma);
                }

                if (error == SFERR_NO_ERROR) {
                    // get the last string
                    csvElement = stringFromFile.Substring(startOfComma,
                        stringFromFile.GetLength());

                    // convert string into number and store it into array
                    if ((error = array.InsertLast(csvElement.AsSInt32()))
                        == SFERR_NO_ERROR) {

                        // here, stop reading

                        // get the enumerator
                        etor = array.GetFirstEnumerator();
                        sum = 0;

                        while (etor.HasNext()) { 
                            // process each element
                            SInt32 c;
                            c = etor.GetNext(); // get next element
                            sum += c;           // add the value of the element to sum
                        }

                        TRACE("%d", sum);
                    }
                }
            }
        }
        reader.Release();
    }
    file.Close();
}

Example 13.9. Execution Result

*dbgprintf*:0 - 59

13.8.2. Sorting Elements

The code below is to sort elements using the "Insertion sort" algorithm.

[Tip] Tip
In SFXList, elements are inserted and deleted faster than in SFXArray.

Example 13.10. Sorting Elements

SFCError error;                     // error value
SFXList<SInt32> list;               // list to store elements of SInt32
SFXList<SInt32>::Enumerator etor;   // enumerator of list
SInt32 number[] = {14, 3, 22, -5, 8, 16, 1, 0, -7, -2}; // data to be sorted
SInt32 i, j, ithValue, jthValue;


for (i = 0; i < lengthof(number); ++i) {

    error = list.InsertLast(number[i]);// append the element from "number" onto "list"
    if (error != SFERR_NO_ERROR) { 

        // if an error occurs
        // error processing

        ...

        return;
    }
}

// insertion sort
for (i = 1; i < list.GetSize(); ++i) {

    ithValue = list.Get(i);           // store the value of the i-th element into ithValue

    etor = list.GetFirstEnumerator(); // get the enumerator of "list"

    for (j = 0; j < i; ++j) {  // (*) 0 ≦ j < i

        jthValue = etor.GetNext();    // store the value of the j-th element into jthValue and advance the enumerator next

        if (jthValue > ithValue) { 
            // if the value of the j-th element is bigger than that of the i-th element

            // move the i-th element befre the j-th element
            list.Remove(i);                   // delete the i-th element
            error = list.Insert(j, ithValue); // insert the former i-th element before the j-th element

            if (error != SFERR_NO_ERROR) {

                // if an error occurs
                // error processing

                ...

                return;
            }
            break;
        }
    }
}

// display all elements of list in the BREW Output Window

etor = list.GetFirstEnumerator();  // get the enumerator of "list"

while (etor.HasNext()) {

    TRACE("%d", etor.GetNext());
}

Example 13.11. Execution Result

*dbgprintf*:0 - -7
*dbgprintf*:0 - -5
*dbgprintf*:0 - -2
*dbgprintf*:0 - 0
*dbgprintf*:0 - 1
*dbgprintf*:0 - 3
*dbgprintf*:0 - 8
*dbgprintf*:0 - 14
*dbgprintf*:0 - 16
*dbgprintf*:0 - 22

13.8.3. List Processing of the Class Instances

Data bigger than 4 bytes such a class instance cannot be stored into the collection class as an element. Even in such a case, the pointer to it should be stored there as an element since the size of the pointer is 4 bytes.

The code below is to process a line(SFXLine) as an element of the list(SFXList) and sort lines by their X coordinates of the starting points in increasing order.

[Note] Note
Note that the pointer to SFXLine is stored into the element of SFXList and the SFXLine instance itself is explicitly deleted before its pointer as an element is cleared.

Example 13.12. List Processing of the SFXLine instances

SFCError error;                       // error value
SFXList<SFXLinePtr> list;             // list to store elements of SFXLinePtr
SFXList<SFXLinePtr>::Enumerator etor; // enumerator of list
SFXMTRandom random;                   // random number
SFXLinePtr line, ithLine, jthLine;    // pointer to lines
SInt32 i, j;

// make 10 lines and store them into "list"
for (i = 0; i < 10; ++i) {

    // make a line by generating the coordinates of the line's starting point and ending point using the random function
    if ((line = new SFXLine(random.GetSInt16(), random.GetSInt16(),
                            random.GetSInt16(), random.GetSInt16())) != null) {
        
        // append its pointer onto "list"
        error = list.InsertLast(line);
    }
    else { 
        // if fail to make a line
        error = SFERR_NO_MEMORY;
    }
    if (error != SFERR_NO_ERROR) {    // break loop if an error occurs
        break;
    }
}

// sort the line elements stored in list by the X coordinate of the starting point
if (error == SFERR_NO_ERROR) {

    // insertion sort (compare with the X coordinate of the starting point)
    for (i = 1; i < list.GetSize(); ++i) {

        ithLine = list.Get(i);            // store pointer to the i-th line into "ithLine"

        etor = list.GetFirstEnumerator(); // get the enumerator of "list"

        for (j = 0; j < i; ++j) {

            jthLine = etor.GetNext();     // store pointer to the j-th line into "jthLine", and advance the enumerator next

            if (jthLine->GetStartX() > ithLine->GetStartX()) {
                // if the X coordinate of the starting point of "jthLine" is bigger than that of "ithLine"

                // move "ithLine" before "jthLine"
                list.Remove(i);                  // delete the i-th element (note: the line itself will not be deleted)
                error = list.Insert(j, ithLine); // insert "ithLine" as an element before the j-th element into "list"

                break;
            }
        }
        if (error != SFERR_NO_ERROR) {

            // break loop if an error occurs
            break;
        }
    }
}

// instances that elements of list point to must be deleted before cleared
// * otherwise, memory leakage will occur

etor = list.GetFirstEnumerator();  // get the enumerator of "list"

while (etor.HasNext()) {

    line = etor.GetNext();
    delete line;
}

// clear list
list.Clear();

13.8.4. Counting Number of Words

The code below is to count the occurrences of words in a string using the SFXLinkedHashMap class. The pair element of the SFXLinkedHashMap class is (word: key, occurrence: value).

Example 13.13. Counting the Number of Words

SFXAnsiString string("abc def def def ghi ghi");     // string to be counted
SFXLinkedHashMap<SFXAnsiString, SInt32> hashmap;    // hashmap of (key: SFXAnsiString, value: SInt32)
SFXAnsiString word;                                  // word in string
Bool isAlphabet = false;                             // true while the alphabetical character is being processed
SInt32 startOfWord, endOfWord;
SInt32 i;

// insert the dummy non-alphabetic character at the end of string
string += '.';

for (i = 0; i < string.GetLength(); ++i) {
    // check whether string[i] is alphabetical or not
    if (SFXAscii::IsAlphaDigit(string[i])) {
        if (!isAlphabet) {   
            // when the alphabetical character is not being processed
            startOfWord = i; // set the starting position of word
            isAlphabet = true;
        }
    } else {
        // when the alphabetical character is being processed
        if (isAlphabet) { 
            endOfWord = i;
            // get word from string
            word = string.Substring(startOfWord, endOfWord);
            if (hashmap.ContainsKey(word)) { 
                // if word is included
                // get the current occurrence for this word
                SInt32 count = hashmap.Get(word);
                // increase count by 1
                hashmap.Set(word, count + 1);
            } else {
                // set count to 1 for the first occurrence
                hashmap.Set(word, 1);
            }
            isAlphabet = false;
        }
    }
}

// display the result in BREW Output Window
TRACE("\"def\": %d, \"ghi\": %d, \"abc\": %d, \"xyz\": %d", hashmap.Get("def"), hashmap.Get("ghi"), hashmap.Get("abc"), hashmap.Get("xyz"));

Example 13.14. Execution Result

*dbgprintf*:0 - "def": 3, "ghi": 2, "abc": 1, "xyz": 0