0

I am a developer in C-like languages (Java/JavaScript/C#) and I am attempting to convert some Objective-C code into Java.

For the most part, it is relatively straightforward but I have hit a stumbling block with the following bit of code:

typedef struct {
    char *PAGE_AREA_ONE;
    char *PAGE_AREA_TWO;
    char *PAGE_AREA_THREE;
} CODES;

- (CODES*) getOpCode {
    CODES *result = NULL;
    result = malloc(sizeof(CODES));

    result->PAGE_AREA_ONE = "\x1b\x1b\x1b";
    result->PAGE_AREA_TWO = "\x2d\x2d\x2d";
    result->PAGE_AREA_THREE = "\x40\x40";

    return result;
}

What would the Java equivalent of this be? From what I can tell in other areas of the code, it is being used to store constants. But I am not 100% certain.

Thanks.

keldar
  • 6,152
  • 10
  • 52
  • 82
  • Pretty sure that `typedef` stuff at the top is C, not Objective-C – Max Woolf Dec 03 '13 at 16:44
  • 3
    @MaxWoolf Pretty sure that C is a proper subset of Objective-C – pjs Dec 03 '13 at 16:45
  • Yes... but that doesn't mean C = Objective-C. Just wanted to clarify the question. – Max Woolf Dec 03 '13 at 16:46
  • 3
    Setting `result = NULL` and then accessing `result->PAGE_AREA_ONE` looks more like a recipe for a crash ... – Martin R Dec 03 '13 at 16:46
  • @MaxWoolf Nobody said C = Objective-C, but any C code fragments in ObjC are perfectly valid so I don't see that your comment clarified anything. – pjs Dec 03 '13 at 16:48
  • 1
    Related: http://stackoverflow.com/q/5168144/620197 – Mike D Dec 03 '13 at 16:53
  • 1
    @MartinR apologies - I missed the malloc. – keldar Dec 03 '13 at 16:53
  • 2
    @MaxWoolf The declaration `- (CODES*) getOpCode` is what makes this Objective-C code. The `-` indicates that `getOpCode` is an instance method of some class. – Caleb Dec 03 '13 at 16:59
  • @keldar Note that this code is stupid. Given that it is a small constant, it should be a static global struct with no allocation involved at all. Also, that method should not be prefixed with `get`. If this is from an example somewhere, it is a bad example and the rest should be held suspect. – bbum Dec 03 '13 at 18:01

2 Answers2

2

The typedef is just creating a structure that contains three string properties. The getOpCode method is apparently trying to create a new structure and assign values to those three properties. C# code would be:

public class Codes
{
    public string PageAreaOne;
    public string PageAreaTwo;
    public string PageAreaThree;
}

public Codes GetCodes()
{
    Codes result = new Codes();
    result.PageAreaOne = "\x1b\x1b\x1b";  // three ESC characters
    result.PageAreaTwo = "---";
    result.PageAreaThree = "@@";
    return result;
}
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
1

The code in question is allocating a block of memory that the size of the CODES structure, filling it with some data, and returning a pointer to the new block. The data is apparently some operation codes (that is, instructions) for something, so perhaps the data is being sent to some other device where the instructions will be executed.

Caleb
  • 124,013
  • 19
  • 183
  • 272