Measurement of charm production at central rapidity in proton-proton collisions at $\sqrt{s} = 7$ TeV